Python Practice › Basic Programs › Program 14

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

MediumBasic Programs · 14 of 40

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

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

Approach

  1. Calculate both sides and compare them.

Solution

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

Advertisement

Learn the concept

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

More basic programs

← All 40 Basic Programs