Python Practice › Basic Programs › Program 13
Formula (a + b)³ = a³ + 3ab(a + b) + b³
Write a Python program to solve the formula (a + b)³ = a³ + 3ab(a + b) + b³.
Input
a = 2, b = 3
Output
125 125 True
Approach
- Calculate both sides and compare them.
Solution
a = 2b = 3 left = (a + b) ** 3right = a ** 3 + 3 * a * b * (a + b) + b ** 3print(left, right, left == right)Output
125 125 True
Line by line
Line 5
3ab(a + b) in maths becomes 3 * a * b * (a + b) in Python; every multiplication needs an explicit *.
Learn the concept
New to this topic? Read the lesson, then come back.