Top 10 Python Programs with Output (For Exams & Practice

0

Top 10 Python Programs with Output (For Exams & Practice)

This post contains important Python programs with output for students. Useful for exams, viva, and practice.



1) Hello World Program

print("Hello World")

Output: Hello World


2) Addition of Two Numbers

a = 10
b = 5
print(a + b)

Output: 15


3) Even or Odd Number

num = 7
if num % 2 == 0:
    print("Even")
else:
    print("Odd")

Output: Odd


4) Factorial of a Number

num = 5
fact = 1
for i in range(1, num+1):
    fact *= i
print(fact)

Output: 120


5) Reverse a String

text = "Python"
print(text[::-1])

Output: nohtyP


6) Largest of Three Numbers

a = 10
b = 20
c = 15

if a > b and a > c:
    print("A is largest")
elif b > c:
    print("B is largest")
else:
    print("C is largest")

Output: B is largest


7) Check Prime Number

num = 7
flag = True

for i in range(2, num):
    if num % i == 0:
        flag = False
        break

if flag:
    print("Prime")
else:
    print("Not Prime")

Output: Prime


8) Sum of Natural Numbers

n = 5
total = 0

for i in range(1, n+1):
    total += i

print(total)

Output: 15


9) Fibonacci Series

n = 5
a, b = 0, 1

for i in range(n):
    print(a, end=" ")
    a, b = b, a + b

Output: 0 1 1 2 3


10) Swap Two Variables

a = 5
b = 10

a, b = b, a

print("a =", a)
print("b =", b)

Output: a = 10, b = 5


These programs are very important for exams and interviews.

Post a Comment

0Comments

Please Select Embedded Mode To show the Comment System.*