Class 12 Practicle File (Python Programs)
Class 12 Practicle File (Python Programs)
# CODE :
def nth_mult(a,b):
f1 = 0
f2 = 1
i = 2
while i != 0:
f3 = f1 + f2
f1 = f2
f2 = f3
if f2 % a == 0:
return b*i
i += 1
a = 5
b = 4
# CODE :
ascii_val = ord(a)
# OUTPUT :
PROGRAM 3 :
Python Program for Sum of squares of first n natural numbers.
# CODE :
s = 0
for i in range(1,num+1):
s = s + i*i
# OUTPUT :
PROGRAM 4 :
Python Program for cube Sum of first n natural numbers.
# CODE :
s = 0
for i in range(1,num+1):
s = s + i**3
# OUTPUT :
PROGRAM 5 :
Python Program to find the largest element in an array :
# CODE :
array = [12,34,64,25,77,23,53,56,2]
max_val = max(array)
# OUTPUT :
PROGRAM 6 :
Python Program to print all negative numbers in a range:
# CODE :
reasult = "found"
for i in range(start,end):
if i < 0:
print(i,end=" ")
if start > 0:
# OUTPUT :
PROGRAM 7 :
Python Program to find Cumulative sum of list.
# CODE :
list1 = []
list2 = [10,20,30,40,50]
a = 0
for i in list2 :
n = a + i
list1.append(n)
a = a + i
# OUTPUT :
PROGRAM 8 :
Python Program to check if a string is palindrome or not.
# CODE :
if string == string[::-1]:
print(string," is 'Palindrome'")
else:
# OUTPUT :
PROGRAM 9 :
Ways to remove i”th character from the string.
# CODE :
def strs(string,i):
list1 = string.partition(string[i])
string2 = list1[0]+list1[2]
return string2
s1 = strs(s,i)
# OUTPUT :
PROGRAM 10 :
Python Program to check if a Substring is present in a given string.
# CODE :
if substr in str1:
else:
# OUTPUT :
PROGRAM 11 :
Write a program that reads character from the keyboard one by one . All lower case character
get stored inside the file LOWER , all upper case character get stored inside the file UPPER and
other character get stored inside the file OTHERS.
# CODE :
f1 = open("LOWER.txt",'w')
f2 = open("UPPER.txt",'w')
f3 = open("OTHERS.txt",'w')
ch = 'y'
while ch == 'y':
if letter.islower():
f1.write(letter)
elif letter.isupper():
f2.write(letter)
else:
f3.write(letter)
f1.close()
f2.close()
f3.close()
# OUTPUT :
PROGRAM 12 :
Write a program to count words “to” and “the” present in text file “Poem.txt” .
# CODE :
fh = open("Poem.txt",'r')
to_count = 0
the_count =0
l = fh.readlines()
for line in l:
if word in ['to','To']:
to_count += 1
if word in ['the','The']:
the_count += 1
fh.close()
# OUTPUT :
PROGRAM 13 :
Write a program to perform read and write operations onto a student.csv file having
#------------WRITE operation--------------
import csv
fh = open("student.csv",'w')
stu_writer = csv.writer(fh)
stu_writer.writerow(['Roll_number','Name','Stream','Per
centage'])
ch = 'y'
while ch == 'y':
rec = [r,n,s,p]
stu_writer.writerow(rec)
fh.close()
#-------------READ operation---------------
stu_reader = csv.reader(fh1)
print(rec)
fh1.close()
# CODE :
import pickle
fh = open("stu_records.dat",'wb')
rec = {}
ch = 'y'
while ch == 'y':
rec['NAME'] = name
rec['ROLL_NUMBER'] = rollno
pickle.dump(rec,fh)
fh.close()
#---------------SEARCHING for records-----------------
fh1 = open("stu_records.dat",'rb')
rec ={}
found = False
try:
while True:
rec = pickle.load(fh1)
if rec['ROLL_NUMBER'] == n:
print(rec['NAME'])
found = True
except EOFError:
if found == False:
fh1.close()
# CODE :
#--------------SIZE of file-------------------
fh = open("file.txt",'r')
size = 0
s = fh.read()
l = list(s)
for character in l:
size += 1
fh.close()
fh = open("file.txt",'r')
num_lines = 0
l1 = fh.readlines()
num_lines += 1
fh.close()
#----------Number of words in file------------
fh = open("file.txt",'r')
num_words = 0
l2 = fh.readlines()
line = line.rstrip('\n')
l3 = line.split()
num_words += 1
fh.close()
fh = open("file.txt",'r')
num_char = 0
s1 = fh.read()
l4 = list(s)
num_char += 1
fh.close()
# OUTPUT :
PROGRAM 16 :
Read a text file and display number of vowel /consonants/uppercase/lowercase characters :-
#CODE :
fh = open("file.txt",'r')
vcount = 0
ccount = 0
ucount = 0
lcount = 0
lst = fh.readlines()
line = line.rstrip('\n')
#----------------VOWEL count-----------------
if letter in
['a','e','i','o','u','A','E','I','O','U']:
vcount += 1
#--------------CONSONANT count---------------
else:
ccount += 1
#--------------UPPERCASE count---------------
if letter.isupper():
ucount += 1
#--------------LOWERCASE count---------------
else:
lcount += 1
print("number of vowels :-",vcount)
fh.close()
# OUTPUT :
PROGRAM 17 :
Write a program to sort the elements of given list in ascending and descending order.
#CODE :
L = [2,10,5,24,30,3,8,6,0,1]
L.sort()
L.sort(reverse=True)
# OUTPUT :
PROGRAM 18 :
Write a program to copy odd lines of one file to another file.
fh1 = open("file.txt","r")
fh2 = open("file2.txt","w")
list1 = fh1.readlines()
list2 = []
line = list1[i-1]
list2.append(line)
fh2.writelines(list2)
print("written")
print(list2)
# OUTPUT :