Math and Prime Number Examples

Practice math-focused code: factorials, prime checks, divisors, GCD/LCM, and multiplication tables. These examples emphasize efficient loops and clean math utilities suitable for interview prep and fundamentals.

Python Program to Find the Factorial of a Number

Iterative factorial.

n = 5
f = 1
for i in range(2, n+1): f *= i
print(f)

Output

120

Python Program to Check Prime Number

Trial division prime check.

n = 29
is_prime = n > 1 and all(n % i for i in range(2, int(n**0.5)+1))
print('Prime' if is_prime else 'Not Prime')

Output

Prime

Python Program to Print all Prime Numbers in an Interval

Lists primes in range.

l, h = 10, 50
primes = []
for x in range(l, h+1):
    if x > 1 and all(x % i for i in range(2, int(x**0.5)+1)):
        primes.append(x)
print(primes)

Output

[11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

Python Program to Display the multiplication Table

Prints table for a number.

n = 5
for i in range(1, 11):
    print(f"{n} x {i} = {n*i}")

Output

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Python Program to Find the Sum of Natural Numbers

Sum of 1..n.

n = 10
print(n * (n + 1) // 2)

Output

55

Python Program to Find HCF or GCD

Uses math.gcd.

import math
print(math.gcd(54, 24))

Output

6

Python Program to Find LCM

Computes LCM via GCD.

import math
a, b = 12, 18
lcm = a * b // math.gcd(a, b)
print(lcm)

Output

36

Python Program to Find the Factors of a Number

Lists divisors.

n = 28
print([i for i in range(1, n+1) if n % i == 0])

Output

[1, 2, 4, 7, 14, 28]

Python Program to Compute the Power of a Number

Uses pow.

print(pow(2, 10))

Output

1024

Python Program to Count the Number of Digits Present In a Number

Counts decimal digits.

print(len(str(abs(12345))))

Output

5

Python Strings Basics

Concatenate, format, and slice strings.

s = "Hello, Python"
print(s.lower())
print(s.upper())
print(s[0:5])
print(f"Length: {len(s)}")

Output

hello, python
HELLO, PYTHON
Hello
Length: 13

Keep Practicing

Use the online Python compiler to run examples and test variations. Reinforce learning by building small scripts for each topic.

FAQ

You can use the online Python compiler linked above to run examples instantly. For larger projects, install Python locally.

Yes. Examples cover variables, control flow, data structures, functions, classes, and file I/O—ideal for beginners.

Yes. The programs are tested and produce consistent output across major platforms.

Learn Python by Practicing Examples

Hands-on practice is the fastest way to understand Python. Each example focuses on a single concept—from variables, strings, lists, and dictionaries to loops, functions, classes, exceptions, and file I/O. Modify examples, add new functions, and see how clean design leads to readable, maintainable code.

Use our tools to deepen learning: the Python Compiler to run snippets, and the Python Tutorial for structured lessons.

Beginner-Friendly

Start with variables, operators, and control flow. Build confidence with small, readable programs.

Practical Patterns

Practice lists, dicts, comprehensions, functions, classes, exceptions, and file handling.

Grow Skills

Explore generators and decorators to write expressive, reusable code.