Arithmetic operations on two numbers

Python Practice › Basic Programs › Program 40

Arithmetic operations on two numbers

EasyBasic Programs · 40 of 40

Write a Python program to perform all the basic arithmetic operations on two numbers.

Input
a = 17, b = 5
Output
Addition        22
Subtraction     12
Multiplication  85
Division        3.4
Floor division  3
Remainder       2
Power           1419857

Approach

  1. Apply each arithmetic operator and print the results in a table.

Solution

Python
a, b = 17, 5operations = {    "Addition": a + b,    "Subtraction": a - b,    "Multiplication": a * b,    "Division": a / b,    "Floor division": a // b,    "Remainder": a % b,    "Power": a ** b,}for name, value in operations.items():    print(f"{name:<15} {value}")
Output
Addition        22
Subtraction     12
Multiplication  85
Division        3.4
Floor division  3
Remainder       2
Power           1419857

Line by line

Lines 2-10

A dictionary maps each operation’s name to its result.

Lines 11-12

The loop prints each pair; :<15 pads the name to 15 characters so the values line up.

Learn the concept

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

More basic programs

← All 40 Basic Programs

Volume of a sphere

Python Practice › Basic Programs › Program 39

Volume of a sphere

EasyBasic Programs · 39 of 40

Write a Python program to calculate the volume of a sphere. Volume = 4/3 × π × r³.

Input
r = 6
Output
Volume: 904.78

Approach

  1. Apply the formula with math.pi.

Solution

Python
import math r = 6volume = 4 / 3 * math.pi * r ** 3print("Volume:", round(volume, 2))
Output
Volume: 904.78

Line by line

Line 4

The radius is cubed (r ** 3).

Note

The original exercise wrote the formula as 4/3 × π × r² with π = 3. The correct formula uses r³, and math.pi gives an accurate π.

Learn the concept

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

More basic programs

← All 40 Basic Programs

Square root of a number

Python Practice › Basic Programs › Program 38

Square root of a number

EasyBasic Programs · 38 of 40

Write a Python program to find the square root of a number using the math library.

Input
144, 2
Output
12.0
1.4142
12
4.0

Approach

  1. Use math.sqrt(), or math.isqrt() for a whole-number root.

Solution

Python
import math print(math.sqrt(144))print(round(math.sqrt(2), 4))print(math.isqrt(150))print(16 ** 0.5)
Output
12.0
1.4142
12
4.0

Line by line

Line 3

sqrt always returns a float.

Line 5

isqrt returns the whole-number part of the root (12 for 150).

Line 6

Raising to the power 0.5 also gives the square root.

Learn the concept

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

More basic programs

← All 40 Basic Programs

LCM of two numbers

Python Practice › Basic Programs › Program 37

LCM of two numbers

EasyBasic Programs · 37 of 40

Write a Python program to find the LCM (lowest common multiple) of two numbers.

Input
12 and 18
Output
LCM: 36

Approach

  1. LCM × HCF = a × b, so LCM = a × b // HCF.

Solution 1: using the HCF

Python
import math a, b = 12, 18lcm = a * b // math.gcd(a, b)print("LCM:", lcm)
Output
LCM: 36

Line by line

Line 4

12 × 18 = 216 and the HCF is 6, so the LCM is 36.

Solution 2: math.lcm

Python
import mathprint(math.lcm(12, 18), math.lcm(4, 6, 10))
Output
36 60

Line by line

Line 2

math.lcm (Python 3.9+) accepts any number of values.

Learn the concept

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

More basic programs

← All 40 Basic Programs

HCF (GCD) of two numbers

Python Practice › Basic Programs › Program 36

HCF (GCD) of two numbers

EasyBasic Programs · 36 of 40

Write a Python program to find the HCF (highest common factor, also called GCD) of two numbers.

Input
48 and 36
Output
HCF: 12

Approach

  1. Euclid’s algorithm: replace (a, b) with (b, a % b) until b is 0; then a is the HCF.

Solution 1: Euclid’s algorithm

Python
a, b = 48, 36while b:    a, b = b, a % bprint("HCF:", a)
Output
HCF: 12

Line by line

Line 2

The loop runs while b is not zero.

Line 3

(48, 36) → (36, 12) → (12, 0), so the HCF is 12.

Solution 2: math.gcd

Python
import mathprint(math.gcd(48, 36))
Output
12

Learn the concept

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

More basic programs

← All 40 Basic Programs

Sum of natural numbers

Python Practice › Basic Programs › Program 35

Sum of natural numbers

EasyBasic Programs · 35 of 40

Write a Python program to find the sum of the first n natural numbers (1 + 2 + … + n).

Input
n = 100
Output
5050

Approach

  1. Loop and add, or use the formula n(n + 1)/2.

Solution 1: loop

Python
n = 100total = 0for i in range(1, n + 1):    total += iprint(total)
Output
5050

Line by line

Lines 3-4

Adds 1, 2, 3 … 100 one at a time.

Solution 2: formula

Python
n = 100print(n * (n + 1) // 2, sum(range(1, n + 1)))
Output
5050 5050

Line by line

Line 2

The formula gives the answer instantly; // keeps it an integer. sum(range(...)) is the one-line loop version.

Learn the concept

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

More basic programs

← All 40 Basic Programs

Convert decimal to binary

Python Practice › Basic Programs › Program 34

Convert decimal to binary

EasyBasic Programs · 34 of 40

Write a Python program to convert a decimal number to binary.

Input
25
Output
11001

Approach

  1. Repeatedly divide by 2 and collect the remainders, then read them in reverse. Or use bin().

Solution 1: by hand

Python
n = 25bits = ""while n > 0:    bits = str(n % 2) + bits    n //= 2print(bits)
Output
11001

Line by line

Line 4

The remainder after dividing by 2 is the next binary digit; adding it in front reverses the order for us.

Line 5

Integer division moves to the next digit.

Solution 2: built-in

Python
print(bin(25), format(25, "b"), int("11001", 2))
Output
0b11001 11001 25

Line by line

Line 1

bin() adds the 0b prefix, format(n, "b") does not, and int(text, 2) converts back.

Learn the concept

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

More basic programs

← All 40 Basic Programs

Get the current date and time

Python Practice › Basic Programs › Program 33

Get the current date and time

EasyBasic Programs · 33 of 40

Write a Python program to get the current date (and time).

Output
Today: 2026-11-02
Year: 2026 Month: 11 Day: 2
Weekday: Monday

Approach

  1. Use date.today() for the date and datetime.now() for date and time.

Solution

Python
from datetime import date, datetime print("Today:", date.today())now = datetime.now()print("Year:", now.year, "Month:", now.month, "Day:", now.day)print("Weekday:", now.strftime("%A"))
Output
Today: 2026-11-02
Year: 2026 Month: 11 Day: 2
Weekday: Monday

Line by line

Line 3

Prints the date in ISO format, year-month-day.

Line 5

A datetime exposes each part as an attribute.

Line 6

%A gives the full weekday name.

Note

The output shows the date on which this page was built.

Learn the concept

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

More basic programs

← All 40 Basic Programs

Random string of a given length

Python Practice › Basic Programs › Program 32

Random string of a given length

EasyBasic Programs · 32 of 40

Write a Python program to generate a random string of a specific length.

Input
length = 10
Output
u8jzPde0Ig 10

Approach

  1. Build a pool of characters with the string module.
  2. Pick characters at random and join them.

Solution

Python
import randomimport string random.seed(7)length = 10pool = string.ascii_letters + string.digitspassword = "".join(random.choice(pool) for _ in range(length))print(password, len(password))
Output
u8jzPde0Ig 10

Line by line

Line 6

ascii_letters is a-z and A-Z; adding digits gives 62 possible characters.

Line 7

random.choice picks one character per position and join builds the string.

Tip

For passwords and tokens use the secrets module (secrets.token_urlsafe(16)), which is cryptographically secure. random is fine for test data.

Learn the concept

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

More basic programs

← All 40 Basic Programs

Generate random numbers

Python Practice › Basic Programs › Program 31

Generate random numbers

EasyBasic Programs · 31 of 40

Write a Python program to generate random numbers.

Output
74
[1, 4, 4, 5, 1]
1.706
[32, 18, 42, 11, 3, 34]

Approach

  1. Use the random module: randint for whole numbers, random and uniform for decimals.
  2. Seed the generator to make the output repeatable.

Solution

Python
import random random.seed(10)print(random.randint(1, 100))print([random.randint(1, 6) for _ in range(5)])print(round(random.uniform(1.5, 2.5), 3))print(random.sample(range(1, 50), 6))
Output
74
[1, 4, 4, 5, 1]
1.706
[32, 18, 42, 11, 3, 34]

Line by line

Line 3

A fixed seed makes the sequence repeat on every run. Remove it to get different numbers each time.

Line 4

randint(1, 100) includes both ends.

Line 5

Five dice rolls.

Line 6

uniform returns a float between the two values.

Line 7

sample picks 6 different numbers, like a lottery draw.

Learn the concept

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

More basic programs

← All 40 Basic Programs