Python Practice › Basic Programs › Program 13

Formula (a + b)³ = a³ + 3ab(a + b) + b³

MediumBasic Programs · 13 of 40

Write a Python program to solve the formula (a + b)³ = a³ + 3ab(a + b) + b³.

Advertisement
Input
a = 2, b = 3
Output
125 125 True

Approach

  1. Calculate both sides and compare them.

Solution

Python
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 *.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs