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

Check for an anagram

Python Practice › Basic Programs › Program 30

Check for an anagram

MediumBasic Programs · 30 of 40

Write a Python program to check whether two words are anagrams: one is a rearrangement of all the letters of the other.

Input
"listen" and "silent", "apple" and "paple", "hello" and "world"
Output
True
True
False

Approach

  1. Ignore case and spaces.
  2. Two words are anagrams if their sorted letters are the same.

Solution 1: sorting

Python
def is_anagram(a, b):    clean_a = a.replace(" ", "").lower()    clean_b = b.replace(" ", "").lower()    return sorted(clean_a) == sorted(clean_b) print(is_anagram("listen", "silent"))print(is_anagram("Dormitory", "dirty room"))print(is_anagram("hello", "world"))
Output
True
True
False

Line by line

Lines 2-3

Remove spaces and lower-case both words so “Dormitory” and “dirty room” compare fairly.

Line 4

sorted() turns each word into a sorted list of letters; equal lists mean the same letters.

Solution 2: Counter

Python
from collections import Counterprint(Counter("apple") == Counter("paple"), Counter("abc") == Counter("abd"))
Output
True False

Line by line

Line 2

Counter counts each letter; anagrams have identical counts.

Learn the concept

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

More basic programs

← All 40 Basic Programs

Check a leap year

Python Practice › Basic Programs › Program 29

Check a leap year

EasyBasic Programs · 29 of 40

Write a Python program to check whether a year is a leap year: divisible by 4, except century years, which must be divisible by 400.

Input
1900, 2000, 2024, 2026
Output
1900 not leap
2000 leap
2024 leap
2026 not leap

Approach

  1. Combine the rules with and / or.

Solution 1: the rules

Python
for year in [1900, 2000, 2024, 2026]:    leap = (year % 4 == 0 and year % 100 != 0) or year % 400 == 0    print(year, "leap" if leap else "not leap")
Output
1900 not leap
2000 leap
2024 leap
2026 not leap

Line by line

Line 2

Divisible by 4 but not by 100, or divisible by 400.

Solution 2: calendar module

Python
import calendarprint([y for y in [1900, 2000, 2024, 2026] if calendar.isleap(y)])
Output
[2000, 2024]

Line by line

Line 2

calendar.isleap() implements the same rules.

Learn the concept

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

More basic programs

← All 40 Basic Programs