Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🏆 Revision & Practice  ·  Lesson 28

35 Important Python Programs

How to use these programs

These 35 programs cover every type of program in the Class XI Computer Science practical list. Each program uses fixed sample values so you can see the exact output; in your practical, replace the values with input() to make them interactive.

  1. 1Read the program and predict the output first.
  2. 2Type it yourself in IDLE (do not copy-paste) and run with F5.
  3. 3Change the values and check whether the output still makes sense.
  4. 4Write the program and output in your practical file.
Best way to practise

A. Numbers and conditions

Python
# 1. Simple interest and compound interest
p, r, t = 10000, 8, 3
si = p * r * t / 100
ci = p * (1 + r / 100) ** t - p
print('SI =', si, '| CI =', round(ci, 2))
SI = 2400.0 | CI = 2597.12
Python
# 2. Largest of three numbers
a, b, c = 42, 87, 63
print('Largest =', max(a, b, c))
if a >= b and a >= c:
    big = a
elif b >= c:
    big = b
else:
    big = c
print('Using if-elif =', big)
Largest = 87 Using if-elif = 87
Python
# 3. Roots of a quadratic equation ax^2 + bx + c = 0
a, b, c = 1, -7, 10
d = b * b - 4 * a * c
if d > 0:
    r1 = (-b + d ** 0.5) / (2 * a)
    r2 = (-b - d ** 0.5) / (2 * a)
    print('Real and different roots:', r1, r2)
elif d == 0:
    print('Equal roots:', -b / (2 * a))
else:
    print('Imaginary roots')
Real and different roots: 5.0 2.0
Python
# 4. Leap year check for several years
for year in (1900, 2000, 2024, 2026):
    leap = (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
    print(year, 'Leap' if leap else 'Not leap')
1900 Not leap 2000 Leap 2024 Leap 2026 Not leap
Python
# 5. Grade from percentage
for pct in (95, 81, 67, 45, 28):
    if pct >= 90:   g = 'A'
    elif pct >= 75: g = 'B'
    elif pct >= 60: g = 'C'
    elif pct >= 33: g = 'D'
    else:           g = 'E'
    print(pct, '->', g)
95 -> A 81 -> B 67 -> C 45 -> D 28 -> E

B. Loops and number programs

Python
# 6. Factorial using a loop
n = 7
f = 1
for i in range(1, n + 1):
    f *= i
print(n, '! =', f)
7 ! = 5040
Python
# 7. Fibonacci series up to 100
a, b = 0, 1
while a <= 100:
    print(a, end=' ')
    a, b = b, a + b
print()
0 1 1 2 3 5 8 13 21 34 55 89
Python
# 8. Prime or composite
for n in (2, 15, 29, 49, 97):
    is_prime = n > 1
    for d in range(2, int(n ** 0.5) + 1):
        if n % d == 0:
            is_prime = False
            break
    print(n, 'Prime' if is_prime else 'Composite')
2 Prime 15 Composite 29 Prime 49 Composite 97 Prime
Python
# 9. Armstrong numbers between 100 and 999
for n in range(100, 1000):
    s = sum(int(d) ** 3 for d in str(n))
    if s == n:
        print(n, end=' ')
print()
153 370 371 407
Python
# 10. Perfect numbers below 10000
for n in range(2, 10000):
    if sum(d for d in range(1, n) if n % d == 0) == n:
        print(n, end=' ')
print()
6 28 496 8128
Python
# 11. Reverse, digit sum and palindrome
n = 48584
rev, s, t = 0, 0, n
while t > 0:
    d = t % 10
    rev = rev * 10 + d
    s += d
    t //= 10
print('Reverse:', rev, '| Digit sum:', s, '| Palindrome:', rev == n)
Reverse: 48584 | Digit sum: 29 | Palindrome: True
Python
# 12. HCF (GCD) and LCM using Euclid's method
a, b = 84, 120
x, y = a, b
while y != 0:
    x, y = y, x % y
hcf = x
print('HCF =', hcf, '| LCM =', a * b // hcf)
HCF = 12 | LCM = 840
Python
# 13. Sum of series 1 + x + x^2/2! + x^3/3! ... (n terms)
x, n = 2, 8
term, total = 1, 1
for i in range(1, n):
    term = term * x / i
    total += term
print('Sum =', round(total, 4))
Sum = 7.381
Python
# 14. Sum of series 1 - 2 + 3 - 4 + ... n
n = 10
s = 0
for i in range(1, n + 1):
    s += i if i % 2 else -i
print('Sum =', s)
Sum = -5
Python
# 15. Decimal to binary without bin()
n = 45
b = ''
while n > 0:
    b = str(n % 2) + b
    n //= 2
print('Binary =', b)
Binary = 101101

C. Pattern programs

Python
# 16. Number pyramid
n = 5
for i in range(1, n + 1):
    print(' ' * (n - i) + ' '.join(str(j) for j in range(1, i + 1)))
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Python
# 17. Floyd's triangle
num = 1
for i in range(1, 5):
    for j in range(i):
        print(num, end=' ')
        num += 1
    print()
1 2 3 4 5 6 7 8 9 10
Python
# 18. Diamond of stars
n = 4
for i in list(range(1, n + 1)) + list(range(n - 1, 0, -1)):
    print(' ' * (n - i) + '*' * (2 * i - 1))
* *** ***** ******* ***** *** *
Python
# 19. Alphabet pattern
for i in range(5):
    print(' '.join(chr(65 + j) for j in range(i + 1)))
A A B A B C A B C D A B C D E

D. String programs

Python
# 20. Count vowels, consonants, uppercase, lowercase
s = 'Python Is FUN to Learn'
v = sum(1 for ch in s if ch.lower() in 'aeiou')
c = sum(1 for ch in s if ch.isalpha() and ch.lower() not in 'aeiou')
print('Vowels:', v, 'Consonants:', c)
print('Upper:', sum(ch.isupper() for ch in s), 'Lower:', sum(ch.islower() for ch in s))
Vowels: 6 Consonants: 12 Upper: 6 Lower: 12
Python
# 21. Palindrome string (ignore case and spaces)
s = 'Never odd or even'
clean = s.replace(' ', '').lower()
print(clean == clean[::-1])
True
Python
# 22. Longest word and word count
s = 'Practice makes a programmer perfect'
words = s.split()
print('Words:', len(words), '| Longest:', max(words, key=len))
Words: 5 | Longest: programmer
Python
# 23. Replace every vowel with '*' and toggle case
s = 'Computer Science'
print(''.join('*' if ch.lower() in 'aeiou' else ch for ch in s))
print(s.swapcase())
C*mp*t*r Sc**nc* cOMPUTER sCIENCE
Python
# 24. Character frequency in a string
s = 'banana'
freq = {}
for ch in s:
    freq[ch] = freq.get(ch, 0) + 1
print(freq)
{'b': 1, 'a': 3, 'n': 2}
Python
# 25. Caesar cipher (shift by 3)
s = 'HELLO WORLD'
enc = ''
for ch in s:
    if ch.isalpha():
        enc += chr((ord(ch) - 65 + 3) % 26 + 65)
    else:
        enc += ch
print(enc)
KHOOR ZRUOG

E. List and tuple programs

Python
# 26. Max, min, mean of a list
L = [34, 78, 12, 56, 90, 45]
print('Max:', max(L), 'Min:', min(L), 'Mean:', round(sum(L) / len(L), 2))
Max: 90 Min: 12 Mean: 52.5
Python
# 27. Linear search returning index
L = [12, 45, 7, 33, 81]
key = 33
pos = -1
for i in range(len(L)):
    if L[i] == key:
        pos = i
        break
print(key, 'found at index', pos)
33 found at index 3
Python
# 28. Swap elements at even and odd positions
L = [10, 20, 30, 40, 50, 60]
for i in range(0, len(L) - 1, 2):
    L[i], L[i + 1] = L[i + 1], L[i]
print(L)
[20, 10, 40, 30, 60, 50]
Python
# 29. Remove duplicates keeping order
L = [3, 5, 3, 8, 5, 9, 1, 8]
unique = []
for x in L:
    if x not in unique:
        unique.append(x)
print(unique)
[3, 5, 8, 9, 1]
Python
# 30. Separate even and odd, and double each odd
L = [5, 12, 7, 20, 3, 8]
even = [x for x in L if x % 2 == 0]
odd = [x for x in L if x % 2]
print('Even:', even, 'Odd:', odd)
print('Odd doubled in list:', [x * 2 if x % 2 else x for x in L])
Even: [12, 20, 8] Odd: [5, 7, 3] Odd doubled in list: [10, 12, 14, 20, 6, 8]
Python
# 31. Count elements greater than average in a tuple
t = (23, 67, 45, 89, 12, 56)
avg = sum(t) / len(t)
print('Average:', avg, '| Above average:', sum(1 for x in t if x > avg))
Average: 48.666666666666664 | Above average: 3
Python
# 32. Sum of each row and column of a matrix
M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print('Row sums:', [sum(r) for r in M])
print('Column sums:', [sum(M[i][j] for i in range(3)) for j in range(3)])
print('Diagonal sum:', sum(M[i][i] for i in range(3)))
Row sums: [6, 15, 24] Column sums: [12, 15, 18] Diagonal sum: 15

F. Dictionary programs

Python
# 33. Students with marks above 75 from a dictionary
marks = {'Asha': 82, 'Ravi': 67, 'Neha': 91, 'Ishan': 74, 'Tara': 88}
for name, m in marks.items():
    if m > 75:
        print(name, m)
Asha 82 Neha 91 Tara 88
Python
# 34. Count words in a sentence using a dictionary
s = 'to be or not to be'
count = {}
for w in s.split():
    count[w] = count.get(w, 0) + 1
print(count)
{'to': 2, 'be': 2, 'or': 1, 'not': 1}
Python
# 35. Employee salary dictionary: raise 10% and find highest
emp = {'E1': 32000, 'E2': 45000, 'E3': 28000}
for k in emp:
    emp[k] = round(emp[k] * 1.10)
print(emp)
top = max(emp, key=emp.get)
print('Highest paid:', top, emp[top])
{'E1': 35200, 'E2': 49500, 'E3': 30800} Highest paid: E2 49500

Frequently Asked Questions

Are these programs enough for the Class 11 practical file?
They cover all the program types in the CBSE Class XI CS practical list. Type each one yourself, run it, change the input values and write the output in your file.
Which Python version were these tested on?
Every program was run on Python 3 and the output shown is the real output. They work on Python 3.8 and newer.
← Class 11 Computer Science
🔗

Share this topic with a friend

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

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

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

Next topicImportant Questions & Viva Your progress: 28 / 29 (97%) · Lesson 29 / 29 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free