📘 Lesson · Lesson 40
50 Python Practice Programs with Output
In this lesson: 50 Python practice programs with code and output — numbers, strings, lists, patterns — for school, college and interviews.
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
Frequently Asked Questions
How should I practise these Python programs?
Type each program yourself, run it, then change the input and predict the output before running again.
Are these programs useful for practical files?
Yes. They cover the usual practical-file topics: conditions, loops, strings, lists, dictionaries and functions.
Where can I find more advanced Python programs?
See the Python advanced practice programs page with sorting, searching, stack, queue, Pandas and NumPy programs and their real output.
💻 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.
