Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
📘 Lesson  ·  Lesson 72

68 Python Practice Programs

Written and reviewed by · Senior IT Faculty · 15+ years’ experience

Easy Formula Programs with User Input

Start with these programs first. Every program takes values from the user, applies one clear formula and prints the result. Copy the code, change the input and practise.

1. Add two numbers entered by the user

Reads two numbers and displays their sum.

Formula: Sum = first number + second number
PYTHON
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
result = a + b
print("Sum =", result)
Sample Input and Output:
Enter first number: 25
Enter second number: 17
Sum = 42

2. Area of a rectangle

Uses length and breadth to calculate the rectangular area.

Formula: Area = length x breadth
PYTHON
length = float(input("Enter length: "))
breadth = float(input("Enter breadth: "))
result = length * breadth
print("Area =", result)
Sample Input and Output:
Enter length: 12
Enter breadth: 5
Area = 60

3. Area of a circle

Reads the radius and calculates the area of a circle.

Formula: Area = pi x radius x radius
PYTHON
radius = float(input("Enter radius: "))
result = 3.14159 * radius * radius
print("Area =", result)
Sample Input and Output:
Enter radius: 7
Area = 153.938

4. Metres to kilometres

Converts a distance from metres to kilometres.

Formula: Kilometres = metres / 1000
PYTHON
metres = float(input("Enter distance in metres: "))
result = metres / 1000
print("Kilometres =", result)
Sample Input and Output:
Enter distance in metres: 2500
Kilometres = 2.5

5. Kilometres to metres

Converts a distance from kilometres to metres.

Formula: Metres = kilometres x 1000
PYTHON
kilometres = float(input("Enter distance in kilometres: "))
result = kilometres * 1000
print("Metres =", result)
Sample Input and Output:
Enter distance in kilometres: 3.5
Metres = 3500

6. Rupees to paise

Converts an amount in rupees into paise.

Formula: Paise = rupees x 100
PYTHON
rupees = float(input("Enter amount in rupees: "))
result = rupees * 100
print("Paise =", result)
Sample Input and Output:
Enter amount in rupees: 125.50
Paise = 12550

7. Paise to rupees

Converts an amount in paise into rupees.

Formula: Rupees = paise / 100
PYTHON
paise = float(input("Enter amount in paise: "))
result = paise / 100
print("Rupees =", result)
Sample Input and Output:
Enter amount in paise: 8750
Rupees = 87.5

8. Celsius to Fahrenheit

Converts a temperature from Celsius to Fahrenheit.

Formula: F = (C x 9 / 5) + 32
PYTHON
celsius = float(input("Enter temperature in Celsius: "))
result = (celsius * 9 / 5) + 32
print("Fahrenheit =", result)
Sample Input and Output:
Enter temperature in Celsius: 30
Fahrenheit = 86

9. Fahrenheit to Celsius

Converts a temperature from Fahrenheit to Celsius.

Formula: C = (F - 32) x 5 / 9
PYTHON
fahrenheit = float(input("Enter temperature in Fahrenheit: "))
result = (fahrenheit - 32) * 5 / 9
print("Celsius =", result)
Sample Input and Output:
Enter temperature in Fahrenheit: 98.6
Celsius = 37

10. Simple interest

Calculates simple interest from principal, rate and time.

Formula: SI = principal x rate x time / 100
PYTHON
principal = float(input("Enter principal: "))
rate = float(input("Enter annual rate: "))
time = float(input("Enter time in years: "))
result = principal * rate * time / 100
print("Simple interest =", result)
Sample Input and Output:
Enter principal: 5000
Enter annual rate: 6
Enter time in years: 2
Simple interest = 600

11. Compound interest

Calculates annually compounded interest.

Formula: CI = P(1 + R/100)^T - P
PYTHON
principal = float(input("Enter principal: "))
rate = float(input("Enter annual rate: "))
time = float(input("Enter time in years: "))
result = principal * pow(1 + rate / 100, time) - principal
print("Compound interest =", result)
Sample Input and Output:
Enter principal: 10000
Enter annual rate: 10
Enter time in years: 2
Compound interest = 2100

12. Area of a triangle

Uses base and height to calculate the triangle area.

Formula: Area = base x height / 2
PYTHON
base = float(input("Enter base: "))
height = float(input("Enter height: "))
result = base * height / 2
print("Area =", result)
Sample Input and Output:
Enter base: 10
Enter height: 8
Area = 40

13. Perimeter of a rectangle

Adds all four sides of a rectangle.

Formula: Perimeter = 2 x (length + breadth)
PYTHON
length = float(input("Enter length: "))
breadth = float(input("Enter breadth: "))
result = 2 * (length + breadth)
print("Perimeter =", result)
Sample Input and Output:
Enter length: 12
Enter breadth: 5
Perimeter = 34

14. Circumference of a circle

Calculates the distance around a circle from its radius.

Formula: Circumference = 2 x pi x radius
PYTHON
radius = float(input("Enter radius: "))
result = 2 * 3.14159 * radius
print("Circumference =", result)
Sample Input and Output:
Enter radius: 7
Circumference = 43.9823

15. Total and percentage of five subjects

Reads five marks, then calculates total and percentage.

Formula: Percentage = total marks / 5
PYTHON
m1 = float(input("Enter marks in subject 1: "))
m2 = float(input("Enter marks in subject 2: "))
m3 = float(input("Enter marks in subject 3: "))
m4 = float(input("Enter marks in subject 4: "))
m5 = float(input("Enter marks in subject 5: "))
result = (m1 + m2 + m3 + m4 + m5) / 5
print("Total =", m1+m2+m3+m4+m5)
print("Percentage =", result)
Sample Input and Output:
Marks: 80 75 90 85 70
Total = 400
Percentage = 80

16. GST amount

Calculates GST and the final bill amount.

Formula: GST = amount x rate / 100
PYTHON
amount = float(input("Enter amount: "))
gstRate = float(input("Enter GST rate: "))
result = amount * gstRate / 100
print("GST =", result)
print("Final amount =", amount+result)
Sample Input and Output:
Enter amount: 2000
Enter GST rate: 18
GST = 360
Final amount = 2360

17. Body mass index (BMI)

Calculates BMI from weight in kilograms and height in metres.

Formula: BMI = weight / (height x height)
PYTHON
weight = float(input("Enter weight in kg: "))
height = float(input("Enter height in metres: "))
result = weight / (height * height)
print("BMI =", result)
Sample Input and Output:
Enter weight in kg: 72
Enter height in metres: 1.8
BMI = 22.2222

18. Square and cube of a number

Reads one number and prints both its square and cube.

Formula: Square = n x n; Cube = n x n x n
PYTHON
number = float(input("Enter a number: "))
result = number * number
print("Square =", result)
print("Cube =", number*number*number)
Sample Input and Output:
Enter a number: 5
Square = 25
Cube = 125

About

A collection of 50 important Python practice programs covering calculations, decisions, loops, patterns and lists. Each is complete with expected output.

Basic Calculation Programs

1. Add two numbers
a, b = 5, 3
print("Sum =", a + b)  # 8
2. Subtract two numbers
a, b = 10, 4
print("Diff =", a - b)  # 6
3. Multiply two numbers
a, b = 6, 7
print("Product =", a * b)  # 42
4. Divide two numbers
a, b = 10, 4
print("Result =", a / b)  # 2.5
5. Find remainder
a, b = 17, 5
print("Remainder =", a % b)  # 2
6. Area of rectangle
l, w = 8, 3
print("Area =", l * w)  # 24
7. Area of circle
r = 7
print("Area =", 3.14 * r * r)  # 153.86
8. Simple interest
p, r, t = 1000, 5, 2
print("SI =", (p * r * t) / 100)  # 100.0
9. Average of three numbers
a, b, c = 10, 20, 30
print("Avg =", (a + b + c) / 3)  # 20.0
10. Swap two numbers
a, b = 5, 9
a, b = b, a
print("a =", a, "b =", b)  # a = 9 b = 5

If-Else and If-Elif Programs

1. Check even or odd
n = 7
if n % 2 == 0:
    print("Even")
else:
    print("Odd")  # Odd
2. Largest of two numbers
a, b = 12, 20
print(a if a > b else b)  # 20
3. Largest of three numbers
a, b, c = 5, 9, 3
if a >= b and a >= c:
    print(a)
elif b >= c:
    print(b)
else:
    print(c)  # 9
4. Positive, negative or zero
n = -4
if n > 0:
    print("Positive")
elif n < 0:
    print("Negative")
else:
    print("Zero")  # Negative
5. Check pass or fail
marks = 45
if marks >= 33:
    print("Pass")
else:
    print("Fail")  # Pass
6. Grade using if-elif
m = 82
if m >= 90:
    print("A+")
elif m >= 75:
    print("A")
elif m >= 33:
    print("Pass")
else:
    print("Fail")  # A
7. Check leap year
y = 2024
if (y % 4 == 0 and y % 100 != 0) or y % 400 == 0:
    print("Leap")
else:
    print("Not Leap")  # Leap
8. Check vowel or consonant
ch = "e"
if ch in "aeiou":
    print("Vowel")
else:
    print("Consonant")  # Vowel
9. Divisible by 5 and 11
n = 55
if n % 5 == 0 and n % 11 == 0:
    print("Yes")
else:
    print("No")  # Yes
10. Largest using ternary
a, b = 7, 4
print(a if a > b else b)  # 7

Loop Programs

1. Print 1 to 10
for i in range(1, 11):
    print(i, end=" ")  # 1..10
2. Sum of first N numbers
n = 5
print("Sum =", sum(range(1, n + 1)))  # 15
3. Factorial
n, f = 5, 1
for i in range(1, n + 1):
    f *= i
print("Factorial =", f)  # 120
4. Multiplication table
n = 5
for i in range(1, 11):
    print(n, "x", i, "=", n * i)
5. Print even numbers 1-20
for i in range(2, 21, 2):
    print(i, end=" ")  # 2..20
6. Reverse a number
n, rev = 1234, 0
while n > 0:
    rev = rev * 10 + n % 10
    n //= 10
print(rev)  # 4321
7. Sum of digits
n, s = 123, 0
while n > 0:
    s += n % 10
    n //= 10
print("Sum =", s)  # 6
8. Count digits
n, c = 98765, 0
while n > 0:
    c += 1
    n //= 10
print("Digits =", c)  # 5
9. Check prime
n = 13
prime = all(n % i != 0 for i in range(2, n))
print("Prime" if prime else "Not Prime")  # Prime
10. Fibonacci series
a, b = 0, 1
for _ in range(7):
    print(a, end=" ")
    a, b = b, a + b  # 0 1 1 2 3 5 8

Pattern Programs

1. Square of stars
for i in range(4):
    print("* " * 4)
2. Right triangle
for i in range(1, 6):
    print("* " * i)
3. Inverted triangle
for i in range(5, 0, -1):
    print("* " * i)
4. Number triangle
for i in range(1, 6):
    for j in range(1, i + 1):
        print(j, end=" ")
    print()
5. Repeated number triangle
for i in range(1, 6):
    print((str(i) + " ") * i)
6. Right-aligned triangle
for i in range(1, 5):
    print("  " * (4 - i) + "* " * i)
7. Pyramid of stars
n = 4
for i in range(1, n + 1):
    print(" " * (n - i) + "*" * (2 * i - 1))
8. Alphabet triangle
for i in range(1, 6):
    for j in range(i):
        print(chr(65 + j), end=" ")
    print()
9. Diamond pattern
n = 3
for i in range(1, n + 1):
    print(" " * (n - i) + "*" * (2 * i - 1))
for i in range(n - 1, 0, -1):
    print(" " * (n - i) + "*" * (2 * i - 1))
10. Hollow square
n = 4
for i in range(n):
    for j in range(n):
        if i in (0, n - 1) or j in (0, n - 1):
            print("*", end=" ")
        else:
            print(" ", end=" ")
    print()

List (Array) Programs

1. Sum of list
a = [10, 20, 30, 40]
print("Sum =", sum(a))  # 100
2. Largest in list
a = [3, 7, 2, 9, 4]
print("Max =", max(a))  # 9
3. Smallest in list
a = [3, 7, 2, 9, 4]
print("Min =", min(a))  # 2
4. Average of list
a = [10, 20, 30]
print("Avg =", sum(a) / len(a))  # 20.0
5. Reverse a list
a = [1, 2, 3, 4, 5]
print(a[::-1])  # [5, 4, 3, 2, 1]
6. Count even numbers
a = [1, 2, 3, 4, 5, 6]
print("Even =", sum(1 for x in a if x % 2 == 0))  # 3
7. Linear search
a = [5, 8, 12, 3]
key = 12
print("Found at", a.index(key))  # 2
8. Copy a list
a = [1, 2, 3]
b = a.copy()
print(b)  # [1, 2, 3]
9. Sort ascending
a = [5, 2, 8, 1]
a.sort()
print(a)  # [1, 2, 5, 8]
10. Count positive/negative
a = [-1, 2, -3, 4, 5]
p = sum(1 for x in a if x >= 0)
n = len(a) - p
print("Pos =", p, "Neg =", n)  # Pos = 3 Neg = 2
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.