Math and Prime Number Examples

Key Takeaway: Practice Python with 110+ runnable examples covering strings, lists, dicts, math, dates, OOP, and file I/O — complete with expected terminal outputs.

Audience & Use Case: Designed for computer science students, data analysts, backend developers, and job seekers preparing for Python technical interviews.

Last updated: October 2026 110 Verified Examples

Quick Start & How to Use

  1. Select a Python example topic from the sidebar menu (or mobile dropdown).
  2. Examine the structured source code and corresponding expected output.
  3. Copy code directly or test it in our Online Python Compiler.
  4. Experiment by tweaking variables, loops, and conditions to deepen your understanding.

Who is this for: Beginners, interview candidates, and developers looking for reliable code snippets.

Credible Resources:Python Tutorial · Python Compiler · Python 3 Official Documentation · JavaScript Examples

Python Program to Check Prime Number

Trial division prime check.

Source Code
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')
Expected Output
Prime

Python Program to Print all Prime Numbers in an Interval

Lists primes in range.

Source Code
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)
Expected Output
[11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

Python Program to Find the Factorial of a Number

Iterative factorial.

Source Code
n = 5
f = 1
for i in range(2, n+1): f *= i
print(f)
Expected Output
120

Python Program to Display the multiplication Table

Prints table for a number.

Source Code
n = 5
for i in range(1, 11):
    print(f"{n} x {i} = {n*i}")
Expected 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.

Source Code
n = 10
print(n * (n + 1) // 2)
Expected Output
55

Python Program to Find HCF or GCD

Uses math.gcd.

Source Code
import math
print(math.gcd(54, 24))
Expected Output
6

Python Program to Find LCM

Computes LCM via GCD.

Source Code
import math
a, b = 12, 18
lcm = a * b // math.gcd(a, b)
print(lcm)
Expected Output
36

Python Program to Find the Factors of a Number

Lists divisors.

Source Code
n = 28
print([i for i in range(1, n+1) if n % i == 0])
Expected Output
[1, 2, 4, 7, 14, 28]

Python Program to Compute the Power of a Number

Uses pow.

Source Code
print(pow(2, 10))
Expected Output
1024

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

Counts decimal digits.

Source Code
print(len(str(abs(12345))))
Expected Output
5

Python Strings Basics

Concatenate, format, and slice strings.

Source Code
s = "Hello, Python"
print(s.lower())
print(s.upper())
print(s[0:5])
print(f"Length: {len(s)}")
Expected Output
hello, python
HELLO, PYTHON
Hello
Length: 13

Python Core Concepts & Decision Breakdown

CategoryCore ConceptsPrimary Use CaseOfficial Docs
Primitives & Basicsint, float, str, boolBasic arithmetic, logic checks, string formattingBuilt-in Types
Data Structureslist, tuple, dict, setCollections, lookup maps, data filteringData Structures
Control Flowif/else, for, while, try/exceptIteration, conditional execution, error handlingControl Flow
OOP & Modulardef, class, lambda, importObject orientation, modularization, reusabilityClasses & OOP

Keep Practicing

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

Frequently Asked Questions

Python is a high-level, interpreted programming language known for readable syntax. Practicing with runnable examples accelerates learning by reinforcing concepts through immediate feedback.

You can run all examples instantly using our integrated Online Python Compiler directly in your web browser without downloading any software.

Yes. The programs cover foundational topics including arithmetic, strings, lists, dictionaries, recursion, algorithms, and file operations commonly tested in coding interviews.

Yes. All snippets use standard Python 3 syntax and libraries, ensuring cross-platform compatibility across Windows, macOS, Linux, and web-based environments.

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, the Python Official Docs for language standards, 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.