Area of a square

Python Practice › Basic Programs › Program 15

Area of a square

EasyBasic Programs · 15 of 40

Write a Python program to calculate the area of a square. Area = side × side.

Input
side = 6.5
Output
Area of the square: 42.25

Approach

  1. Multiply the side by itself.

Solution

Python
side = 6.5area = side * sideprint("Area of the square:", area)
Output
Area of the square: 42.25

Line by line

Line 2

side * side (or side ** 2) is the area.

Learn the concept

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

More basic programs

← All 40 Basic Programs

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

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³.

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

Learn the concept

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

More basic programs

← All 40 Basic Programs

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

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³.

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

Learn the concept

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

More basic programs

← All 40 Basic Programs

Formula a² – b² = (a – b)(a + b)

Python Practice › Basic Programs › Program 12

Formula a² – b² = (a – b)(a + b)

EasyBasic Programs · 12 of 40

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

Input
a = 10, b = 6
Output
64 64 True

Approach

  1. Calculate both sides and compare them.

Solution

Python
a = 10b = 6 left = a ** 2 - b ** 2right = (a - b) * (a + b)print(left, right, left == right)
Output
64 64 True

Line by line

Lines 4-5

100 – 36 and 4 × 16 both give 64. Python does not multiply brackets automatically, so the * between them is required.

Learn the concept

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

More basic programs

← All 40 Basic Programs

Formula (a – b)² = a² + b² – 2ab

Python Practice › Basic Programs › Program 11

Formula (a – b)² = a² + b² – 2ab

EasyBasic Programs · 11 of 40

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

Input
a = 9, b = 4
Output
25 25 True

Approach

  1. Calculate both sides and compare them.

Solution

Python
a = 9b = 4 left = (a - b) ** 2right = a ** 2 + b ** 2 - 2 * a * bprint(left, right, left == right)
Output
25 25 True

Line by line

Line 4

(a - b) 2 is 5 2.

Line 5

The expanded form gives the same value, 25.

Learn the concept

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

More basic programs

← All 40 Basic Programs

Formula (a + b)² = a² + b² + 2ab

Python Practice › Basic Programs › Program 10

Formula (a + b)² = a² + b² + 2ab

EasyBasic Programs · 10 of 40

Write a Python program to solve the formula (a + b)² = a² + b² + 2ab and show both sides are equal.

Input
a = 4, b = 5
Output
(a + b)^2 = 81
a^2 + b^2 + 2ab = 81
Equal: True

Approach

  1. Calculate the left side and the right side separately.
  2. Print both and compare them.

Solution

Python
a = 4b = 5 left = (a + b) ** 2right = a ** 2 + b ** 2 + 2 * a * bprint("(a + b)^2 =", left)print("a^2 + b^2 + 2ab =", right)print("Equal:", left == right)
Output
(a + b)^2 = 81
a^2 + b^2 + 2ab = 81
Equal: True

Line by line

Line 4

The brackets make Python add before squaring.

Line 5

binds tighter than and +, so a 2 + b * 2 + 2 * a * b follows normal maths order.

Line 8

== returns True, confirming the identity for these values.

Learn the concept

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

More basic programs

← All 40 Basic Programs

Pythagoras theorem

Pythagoras theorem

EasyBasic Programs · 9 of 40

Write a Python program to find the hypotenuse of a right-angled triangle using the Pythagoras theorem: a² + b² = c².

Input
a = 3, b = 4
Output
Hypotenuse c = 5.0
Check: True

Approach

  1. Compute a² + b².
  2. Take the square root to get c.

Solution

Python
import math a = 3b = 4c = math.sqrt(a ** 2 + b ** 2)print("Hypotenuse c =", c)print("Check:", a ** 2 + b ** 2 == c ** 2)
Output
Hypotenuse c = 5.0
Check: True

Line by line

Line 1

math provides sqrt().

Line 5

The hypotenuse is the square root of the sum of the squares of the other two sides.

Line 7

Squaring c again confirms the theorem holds.

Tip

math.hypot(a, b) computes the same value in one call.

Learn the concept

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

More basic programs

← All 40 Basic Programs

Swap two variables

Swap two variables

EasyBasic Programs · 8 of 40

Write a Python program to interchange (swap) the values of two variables.

Input
a = 10, b = 20
Output
a = 20
b = 10

Approach

  1. Use tuple unpacking a, b = b, a, or a temporary variable.

Solution 1: tuple unpacking

Python
a = 10b = 20 a, b = b, aprint("a =", a)print("b =", b)
Output
a = 20
b = 10

Line by line

Line 4

The right side b, a is evaluated first as the tuple (20, 10) and then unpacked into a and b, so no temporary variable is needed.

Solution 2: temporary variable

Python
a = 10b = 20 temp = aa = bb = tempprint("a =", a, "b =", b)
Output
a = 20 b = 10

Line by line

Lines 4-6

temp keeps the old value of a before it is overwritten. This is how swapping works in most other languages.

Learn the concept

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

More basic programs

← All 40 Basic Programs

Square and cube of a number

Square and cube of a number

EasyBasic Programs · 7 of 40

Write a Python program to print the square and cube of a given number.

Input
num1 = 9
Output
Square = 81
Cube = 729

Approach

  1. Use the power operator : n 2 is the square and n ** 3 is the cube.

Solution

Python
num1 = 9 square = num1 ** 2cube = num1 ** 3print("Square =", square)print("Cube =", cube)
Output
Square = 81
Cube = 729

Line by line

Lines 3-4

** raises a number to a power. num1 * num1 and num1 * num1 * num1 give the same results.

Learn the concept

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

More basic programs

← All 40 Basic Programs

Median of given numbers

Median of given numbers

MediumBasic Programs · 6 of 40

Write a Python program to find the median of given numbers. The median is the middle value after sorting; for an even count it is the average of the two middle values.

Input
[45, 60, 61, 66, 70, 77, 80]
Output
Median: 66

Approach

  1. Sort the numbers.
  2. If the count is odd, take the middle item at index n // 2.
  3. If it is even, average the two middle items.

Solution 1: by hand

Python
numbers = [45, 60, 61, 66, 70, 77, 80]values = sorted(numbers)n = len(values)mid = n // 2 if n % 2 == 1:    median = values[mid]else:    median = (values[mid - 1] + values[mid]) / 2 print("Median:", median)
Output
Median: 66

Line by line

Line 2

sorted() returns a new sorted list, so the original order is kept.

Line 4

For 7 values, 7 // 2 is 3, which is the index of the 4th (middle) item.

Lines 6-9

An odd count has one middle value; an even count averages the two middle values.

Solution 2: statistics module

Python
import statisticsprint(statistics.median([45, 60, 61, 66, 70, 77, 80]))print(statistics.median([3, 1, 4, 2]))
Output
66
2.5

Line by line

Lines 2-3

statistics.median() sorts and handles odd and even counts for you; for [3, 1, 4, 2] it averages 2 and 3.

Learn the concept

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

More basic programs

← All 40 Basic Programs