Surface area of a cylinder

Python Practice › Basic Programs › Program 18

Surface area of a cylinder

EasyBasic Programs · 18 of 40

Write a Python program to calculate the surface area of a cylinder. Area = 2πrh + 2πr².

Input
r = 3, h = 10
Output
Surface area: 245.04

Approach

  1. Curved surface = 2πrh; the two circular ends = 2πr².
  2. Add them.

Solution

Python
import math r = 3h = 10curved = 2 * math.pi * r * hends = 2 * math.pi * r ** 2print("Surface area:", round(curved + ends, 2))
Output
Surface area: 245.04

Line by line

Lines 5-6

Splitting the formula into named parts makes each step easy to check.

Line 7

The total is rounded to two decimals.

Learn the concept

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

More basic programs

← All 40 Basic Programs

Surface area of a cube

Python Practice › Basic Programs › Program 17

Surface area of a cube

EasyBasic Programs · 17 of 40

Write a Python program to calculate the surface area of a cube. Surface area = 6 × a².

Input
a = 4
Output
Surface area of the cube: 96

Approach

  1. A cube has 6 square faces, each of area a².

Solution

Python
a = 4surface_area = 6 * a ** 2print("Surface area of the cube:", surface_area)
Output
Surface area of the cube: 96

Line by line

Line 2

Six faces times the area of one face, a ** 2.

Learn the concept

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

More basic programs

← All 40 Basic Programs

Area of a circle

Python Practice › Basic Programs › Program 16

Area of a circle

EasyBasic Programs · 16 of 40

Write a Python program to calculate the area of a circle. Area = π × r².

Input
r = 7
Output
Area: 153.94

Approach

  1. Use math.pi for an accurate value of π.
  2. Multiply π by the radius squared.

Solution

Python
import math r = 7area = math.pi * r ** 2print("Area:", round(area, 2))
Output
Area: 153.94

Line by line

Line 4

math.pi is π to 15 decimal places, more accurate than 3.14.

Line 5

round(area, 2) keeps two decimals for display.

Learn the concept

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

More basic programs

← All 40 Basic Programs

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