Python Practice › Basic Programs › Program 14
Formula (a – b)³ = a³ – 3a²b + 3ab² – b³
Write a Python program to solve the formula (a – b)³ = a³ – 3a²b + 3ab² – b³.
Input
a = 7, b = 2
Output
125 125 True
Approach
- Calculate both sides and compare them.
Solution
a = 7b = 2 left = (a - b) ** 3right = a ** 3 - 3 * a ** 2 * b + 3 * a * b ** 2 - b ** 3print(left, right, left == right)Output
125 125 True
Line by line
Line 5
3 * a 2 * b squares a first, because is evaluated before *.
Learn the concept
New to this topic? Read the lesson, then come back.