Introduction
This document contains a collection of 20+ Python programs based on the concepts taught in Class
12.
These programs cover various topics such as basic algorithms, object-oriented programming (OOP),
data structures,
file handling, and libraries such as NumPy, Pandas, and Matplotlib.
The programs are numbered for easy reference and demonstrate the core concepts required in
Class 12 computer science and informatics practices.
Certificate
This is to certify that Pranjal Agarwal, a student of Class 12, has successfully completed the
development
of 20+ Python programs based on the curriculum for Class 12 Computer Science and Informatics
Practices.
The programs have been created to demonstrate proficiency in various programming concepts.
Acknowledgement
I would like to express my sincere gratitude to my teachers and mentors who guided me throughout
the process of learning Python programming. Their support and encouragement were instrumental
in completing this project.
Special thanks to my classmates and family members for their constant motivation.
Program 1: Prime Number Check
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
print(is_prime(29))
Program 2: Fibonacci Series
def fibonacci(n):
fib_series = [0, 1]
for i in range(2, n):
fib_series.append(fib_series[-1] + fib_series[-2])
return fib_series
print(fibonacci(10))
Program 3: Sum of Digits
def sum_of_digits(n):
if n == 0:
return 0
return n % 10 + sum_of_digits(n // 10)
print(sum_of_digits(1234))
Program 4: Factorial Using Recursion
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
print(factorial(5))
Program 5: Simple Calculator
def calculator(a, b, operation):
if operation == '+':
return a + b
elif operation == '-':
return a - b
elif operation == '*':
return a * b
elif operation == '/':
return a / b
print(calculator(10, 5, '+'))
Program 6: Count Vowels and Consonants
def count_vowels_consonants(string):
vowels = "aeiouAEIOU"
v_count = sum(1 for char in string if char in vowels)
c_count = len([char for char in string if char.isalpha() and char not in vowels])
return v_count, c_count
print(count_vowels_consonants("Hello World"))
Program 7: Reverse a String
def reverse_string(s):
return s[::-1]
print(reverse_string("hello"))
Program 8: Palindrome Checker
def is_palindrome(s):
return s == s[::-1]
print(is_palindrome("radar"))
Program 9: Sorting a List
def sort_list(lst):
return sorted(lst)
print(sort_list([4, 3, 1, 2]))
Program 10: Remove Duplicates from List
def remove_duplicates(lst):
return list(set(lst))
print(remove_duplicates([1, 2, 2, 3, 4, 4, 5]))
Program 11: Frequency Counter in List
from collections import Counter
def frequency_counter(lst):
return Counter(lst)
print(frequency_counter([1, 2, 2, 3, 4, 4, 5]))
Program 12: File Read and Write
def read_write_file():
with open('input.txt', 'r') as infile, open('output.txt', 'w') as outfile:
data = infile.read()
outfile.write(data)
# Note: Ensure 'input.txt' file is present.
Program 13: Word and Line Count in File
def count_words_lines(filename):
with open(filename, 'r') as file:
lines = file.readlines()
word_count = sum(len(line.split()) for line in lines)
return len(lines), word_count
print(count_words_lines("input.txt")) # Note: Ensure 'input.txt' file is present.
Program 14: Student Class
class Student:
def __init__(self, name, age, grade):
self.name = name
self.age = age
self.grade = grade
student1 = Student("John", 16, 'A')
print(student1.name, student1.age, student1.grade)
Program 15: Bank Account Class
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount > self.balance:
return "Insufficient funds"
self.balance -= amount
account1 = BankAccount("Alice", 1000)
account1.deposit(500)
print(account1.balance)
Program 16: Stack Implementation
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
stack = Stack()
stack.push(1)
stack.push(2)
print(stack.pop())
Program 17: Binary Search
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
print(binary_search([1, 2, 3, 4, 5], 3))
Program 18: Array Creation and Manipulation
import numpy as np
array = np.array([1, 2, 3])
print(array * 2)
Program 19: Line Plot Example
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Line Plot')
plt.show()
Program 20: Matrix Multiplication in NumPy
import numpy as np
def matrix_multiplication():
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
result = np.dot(A, B)
return result
print(matrix_multiplication())