0% found this document useful (0 votes)
8 views8 pages

Python Programs for Class 12 Students

The document contains 16 Python programming exercises, each with a question and corresponding code implementation. The programs cover various topics such as checking for prime numbers, summing even numbers, palindrome checking, and file operations including reading and writing text and binary files. It also includes working with CSV files for employee data management.

Uploaded by

hohile9889
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Python Programs for Class 12 Students

The document contains 16 Python programming exercises, each with a question and corresponding code implementation. The programs cover various topics such as checking for prime numbers, summing even numbers, palindrome checking, and file operations including reading and writing text and binary files. It also includes working with CSV files for employee data management.

Uploaded by

hohile9889
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Class 12 - 16 Python Programs (Questions + Raw

Code)
1. Q1. Write a program using a function to check whether a number
is prime or not.

def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True

def main():
a = int(input())
if is_prime(a):
print("Prime number")
else:
print("Not prime")

if __name__ == '__main__':
main()

2. Q2. Write a program using a function to display the sum of


even numbers from 1 to N.

def sum_even(n):
s = 0
for i in range(2, n+1, 2):
s += i
return s

def main():
num = int(input())
print(sum_even(num))

if __name__ == '__main__':
main()

3. Q3. Write a program using a function to check whether a string


is a palindrome (ignore case and spaces).

def is_palindrome(s):
s = ''.join([Link]()).lower()
return s == s[::-1]

def main():
s = input()
if is_palindrome(s):
print("Palindrome")
else:
print("Not Palindrome")

if __name__ == '__main__':
main()

4. Q4. Write a program using a function to find the maximum


element in a list of integers.

def max_in_list(lst):
if not lst:
return None
max_val = lst[0]
for i in lst:
if i > max_val:
max_val = i
return max_val

def main():
data = [int(x) for x in input().split()]
print(max_in_list(data))

if __name__ == '__main__':
main()

5. Q5. Write a program using a function to count vowels in a


tuple of characters.

def count_vowels(tup):
count = 0
for ch in tup:
if [Link]() in 'aeiou':
count += 1
return count

def main():
t = tuple(input().strip())
print(count_vowels(t))
if __name__ == '__main__':
main()

6. Q6. Write a program using functions to find the key with the
highest value in a dictionary.

def max_key(d):
if not d:
return None
max_k = next(iter(d))
for k in d:
if d[k] > d[max_k]:
max_k = k
return max_k

def main():
n = int(input())
dic = {}
for _ in range(n):
k = input().strip()
v = int(input())
dic[k] = v
print(max_key(dic))

if __name__ == '__main__':
main()

7. Q7. Write a program using functions to implement a stack with


push, pop, peek and display operations (menu-driven).

stack = []

def push(item):
[Link](item)

def pop_item():
if not stack:
print("Stack empty")
else:
print([Link]())

def peek():
if not stack:
print("Stack empty")
else:
print(stack[-1])

def display():
if not stack:
print("Stack empty")
else:
for i in reversed(stack):
print(i)

def main():
while True:
ch = input().strip()
if ch == '1':
push(input().strip())
elif ch == '2':
pop_item()
elif ch == '3':
peek()
elif ch == '4':
display()
elif ch == '0':
break

if __name__ == '__main__':
main()

8. Q8. Write a program using functions to write user text to a


text file and then read and display it.

def write_text():
f = open('[Link]', 'w')
[Link](input())
[Link]()

def read_text():
f = open('[Link]', 'r')
print([Link]())
[Link]()

def main():
write_text()
read_text()

if __name__ == '__main__':
main()
9. Q9. Write a program to count number of words in a text file
using a function.

def count_words():
f = open('[Link]', 'r')
data = [Link]()
[Link]()
print(len([Link]()))

def main():
count_words()

if __name__ == '__main__':
main()

10. Q10. Write a program to display lines containing a specific


word from a text file.

def search_word(word):
f = open('[Link]', 'r')
for line in f:
if word in line:
print(line, end='')
[Link]()

def main():
search_word(input().strip())

if __name__ == '__main__':
main()

11. Q11. Write a program using functions to create and display


records in a binary file using pickle.

import pickle

def create_bin():
f = open('[Link]', 'wb')
n = int(input())
for _ in range(n):
rec = [input().strip(), int(input())]
[Link](rec, f)
[Link]()

def read_bin():
f = open('[Link]', 'rb')
try:
while True:
print([Link](f))
except EOFError:
[Link]()

def main():
create_bin()
read_bin()

if __name__ == '__main__':
main()

12. Q12. Write a program to search a record by name in a binary


file and display it.

import pickle

def search_bin(name):
f = open('[Link]', 'rb')
found = False
try:
while True:
rec = [Link](f)
if rec[0] == name:
print(rec)
found = True
break
except EOFError:
[Link]()
if not found:
print("Not found")

def main():
search_bin(input().strip())

if __name__ == '__main__':
main()

13. Q13. Write a program to append a record to an existing binary


file.

import pickle
def append_bin():
f = open('[Link]', 'ab')
rec = [input().strip(), int(input())]
[Link](rec, f)
[Link]()

def main():
append_bin()

if __name__ == '__main__':
main()

14. Q14. Write a program using functions to write employee


(name,salary) data to CSV and read it.

import csv

def write_csv():
f = open('[Link]', 'w', newline='')
w = [Link](f)
n = int(input())
for _ in range(n):
name = input().strip()
sal = input().strip()
[Link]([name, sal])
[Link]()

def read_csv():
f = open('[Link]', 'r')
r = [Link](f)
for row in r:
print(row)
[Link]()

def main():
write_csv()
read_csv()

if __name__ == '__main__':
main()

15. Q15. Write a program to display employees with salary above a


given amount from a CSV file.
import csv

def salary_filter(limit):
f = open('[Link]', 'r')
r = [Link](f)
for i in r:
if int(i[1]) > limit:
print(i)
[Link]()

def main():
salary_filter(int(input()))

if __name__ == '__main__':
main()

16. Q16. Write a program to add new employee records to an


existing CSV file.

import csv

def append_csv():
f = open('[Link]', 'a', newline='')
w = [Link](f)
name = input().strip()
sal = input().strip()
[Link]([name, sal])
[Link]()

def main():
append_csv()

if __name__ == '__main__':
main()

You might also like