Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views

Class 12 Practicle File (Python Programs)

The document contains 18 Python programs covering a variety of tasks like finding the nth multiple in a Fibonacci series, calculating ASCII values, summing squares and cubes of natural numbers, finding largest elements in arrays, checking for palindromes, sorting lists, copying odd lines between files, and more. Each program is presented with its code and sample output.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Class 12 Practicle File (Python Programs)

The document contains 18 Python programs covering a variety of tasks like finding the nth multiple in a Fibonacci series, calculating ASCII values, summing squares and cubes of natural numbers, finding largest elements in arrays, checking for palindromes, sorting lists, copying odd lines between files, and more. Each program is presented with its code and sample output.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

PROGRAM 1 :

Python Program for n”th multiple of a number in Fibonnacci Series.

# 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

print("position of nth multiple of 4 in fibonnaci series is :


",nth_mult(a,b))
PROGRAM 2 :
Python Program to print ASCII value of a character.

# CODE :

a = input("enter any value : ")

ascii_val = ord(a)

print("the ascii value of the given value is : ",ascii_val)

# OUTPUT :
PROGRAM 3 :
Python Program for Sum of squares of first n natural numbers.

# CODE :

s = 0

num = int(input("Enter the number : "))

for i in range(1,num+1):

s = s + i*i

print("SUM of Square of Natural numbers till ",num," is : ",s)

# OUTPUT :
PROGRAM 4 :
Python Program for cube Sum of first n natural numbers.

# CODE :

s = 0

num = int(input("Enter the number : "))

for i in range(1,num+1):

s = s + i**3

print("SUM of Cube of Natural numbers till ",num," is : ",s)

# 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)

print("Maxium value in array",array,"is:-",max_val)

# OUTPUT :
PROGRAM 6 :
Python Program to print all negative numbers in a range:

# CODE :

start = int(input("enter starting number : "))

end = int(input("enter ending number : "))

print("negative number between",start,"and",end,"are :- ")

reasult = "found"

for i in range(start,end):

if i < 0:

print(i,end=" ")

if start > 0:

print("no negatine value in spacified range")

# 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

print("Cumulative sum of list",list2,"is :- ",list1)

# OUTPUT :
PROGRAM 8 :
Python Program to check if a string is palindrome or not.

# CODE :

string = input("Enter a string : ")

if string == string[::-1]:

print(string," is 'Palindrome'")

else:

print(string," is not a 'Palindrome'")

# 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

s = input("Enter a string :- ")

i = int(input("Enter the index number :- "))

s1 = strs(s,i)

print("resultant string is ",s1)

# OUTPUT :
PROGRAM 10 :
Python Program to check if a Substring is present in a given string.

# CODE :

str1 = input("enter a string : ")

substr = input("enter another string : ")

if substr in str1:

print("YES! ",substr," is present in ",str1)

else:

print("NO! ",substr," is not present in ",str1)

# 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':

letter = input("Enter a word :- ")

if letter.islower():

f1.write(letter)

elif letter.isupper():

f2.write(letter)

else:

f3.write(letter)

ch = input("Wanna enter more words(y/n) :- ")

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:

for word in line.split():

if word in ['to','To']:

to_count += 1

if word in ['the','The']:

the_count += 1

print("Number of 'to' or 'To' is :- ",to_count)

print("Number of 'the' or 'The' is :- ",the_count)

fh.close()

# OUTPUT :
PROGRAM 13 :

Write a program to perform read and write operations onto a student.csv file having

field as roll number, name, stream and percentage.


# CODE :

#------------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':

r = int(input("Enter roll number :- "))

n = input("Enter name :- ")

s = input("Enter stream :- ")

p = float(input("Enter percentage :- "))

rec = [r,n,s,p]

stu_writer.writerow(rec)

ch = input("wanna enter more records(y/n) :- ")

fh.close()
#-------------READ operation---------------

with open("student.csv",'r',newline='\n') as fh1:

stu_reader = csv.reader(fh1)

for rec in stu_reader:

print(rec)

fh1.close()

-------------------- OUTPUT : --------------------------


PROGRAM 14 :
Creat a binary file with the name and roll number . Search for a given roll number and display
the name , if not found display appropriate message.

# CODE :

#---------------CREATING binary file-----------------

import pickle

fh = open("stu_records.dat",'wb')

rec = {}

ch = 'y'

while ch == 'y':

name = input("Enter name :- ")

rollno = int(input("Enter roll number :- "))

rec['NAME'] = name

rec['ROLL_NUMBER'] = rollno

pickle.dump(rec,fh)

ch = input("wanna enter more records(y/n) :- ")

fh.close()
#---------------SEARCHING for records-----------------

fh1 = open("stu_records.dat",'rb')

n = int(input("Enter roll number :- "))

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:

print("Roll number ",n," is NOT FOUND")

fh1.close()

-------------------- OUTPUT : --------------------------


PROGRAM 15 :
Write a python code to find size of file in bytes, number of lines, number of words, number of
characters.

# CODE :

#--------------SIZE of file-------------------

fh = open("file.txt",'r')

size = 0

s = fh.read()

l = list(s)

for character in l:

size += 1

print("size of file is",size,"bytes")

fh.close()

#----------Number of lines in file------------

fh = open("file.txt",'r')

num_lines = 0

l1 = fh.readlines()

for line in l1:

num_lines += 1

print("number of lines in file :- ",num_lines)

fh.close()
#----------Number of words in file------------

fh = open("file.txt",'r')

num_words = 0

l2 = fh.readlines()

for line in l2:

line = line.rstrip('\n')

l3 = line.split()

for word in l3:

num_words += 1

print("Number of words in file :- ",num_words)

fh.close()

#----------Number of characters in file------------

fh = open("file.txt",'r')

num_char = 0

s1 = fh.read()

l4 = list(s)

for character in l4:

num_char += 1

print("number of characters in file :- ",num_char)

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()

for line in lst:

line = line.rstrip('\n')

for word in line.split():

for letter in list(word):

#----------------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)

print("number of consonants :-",ccount)

print("number of uppercase characters :-",ucount)

print("number of lowercase characters :-",lcount)

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()

print("list in Ascending order :- ",L)

L.sort(reverse=True)

print("list in Descending order :- ",L)

# 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 = []

for i in range(1 , len(list1)+1 , 2):

line = list1[i-1]

list2.append(line)

fh2.writelines(list2)

print("written")

print(list2)

# OUTPUT :

You might also like