You are on page 1of 37

CLASS XII

COMPUTER SCIENCE (083)

PYTHON PRACTICAL FILE


(2023-24)

Name:
Section:
Python Basics (10 questions)

PROGRAM 1
Aim: Write a program to check if a number is negative, positive or zero.

Program:

Output:
PROGRAM 2
Aim: write a program to find the area of a triangle

Program:

Output:
PROGRAM 3
Aim: Write a program to find the largest among three numbers.

Program:

Output:
PROGRAM 4
Aim: Write a program to find the sum of n natural numbers using a while loop.

Program:

Output:
PROGRAM 5
Aim: Program to check whether a number is divisible by 2 or 3 using nested if.

Program:

Output:
PROGRAM 6
Aim: Menu based program to find sum, subtraction, multiplication and division of
values.

Program:

Output:
PROGRAM 7
Aim: Program to find factorial of a number.

Program:

Output:
PROGRAM 8
Aim: Program to print following pattern.
*
**
***
****
*****

Program:

Output:
PROGRAM 9
Aim: Program to print results depending upon the percentage.

Program:

Output:
PROGRAM 10

Aim: Write a program to check whether number is palindrome or


not

Program:

Output:
Python Functions programs
PROGRAM 1
Aim: Program to find largest among two numbers using a user defined function.

Program:
PROGRAM 2
Aim: Program to find simple interest using a user defined function with parameters
and with return value.

Program:
PROGRAM 3
Aim: Program to use default arguments in a function.

Program:
PROGRAM 4
Aim: Program to find sum of two numbers using a user defined function with
parameters.

Program:
PROGRAM 5
Aim: Python program to find H.C.F of two numbers

Program:
PROGRAM 6
Aim: Python program to print factors of a number

Program:
PROGRAM 7
Aim: Python program to calculate LCM

Program:
PROGRAM 8
Exception Handling
Aim: Python program to implement exception handling

Program:
Python Text file programs
PROGRAM 1
Aim: Program to read and display the contents in a file.

Program:
PROGRAM 2

Aim: Program to read and display those lines from a file that start with the alphabet
‘T’.

Program:
PROGRAM 3
Aim : Program to read and display those lines from file that start with alphabet ‘T’.

Program:
PROGRAM 4
Aim : Program to count number of words in data file data.txt.
Program:
PROGRAM 5
Aim : program to create a text file and add data
Program:
PROGRAM 6
Aim : Program to write rollno, name and marks of a student in a data file Marks.dat
Program:
count=int(input('How many students are there in the class'))

fileout=open("Marks.dat","a")

for i in range(count):

print("Enter details of student",(i+1),"below")

rollno=int(input("Enter rollno:"))

name=input("name")

marks=float(input('marks'))

rec=str(rollno)+","+name+","+str(marks)+"\n"

fileout.write(rec)

fileout.close()
PROGRAM 7
Aim : Write a Program to read data from data file in append mode and use
writeLines function utility in python.

Program:
PROGRAM 8
Aim :Program to read and display contents of file Marks.dat.
Program:
fileinp=open("Marks.dat","r")

while str:

str=fileinp.readline()

print(str)

fileinp.close()
PROGRAM 9
Aim : Program to read the content of file and display the total number of consonants,
uppercase, vowels and lower case characters.

Program:
PROGRAM 10
Aim : Program to read and display file content line by line with each word separated
by “ #”

Program:
PROGRAM 11
Aim : Program to read the content of a file line by line and write it to another file
except for the lines containing “a '' letters in it.

Program:
PROGRAM 12
Aim : Program to read the content of a file line by line and write it to another file
except for the lines containing “a '' letters in it.

Program:
Python Binary file programs

PROGRAM 1
f = open('C:\myimg.png', 'rb') # opening a binary file
content = f.read() # reading all lines
print(content) #print content
f.close() # closing file object
PROGRAM 2
import pickle

# store roll and name of 5 students in a binary file record.txt


with open(r'D:\record.txt', 'ab+') as fileobj:

print('Input roll and name of 5 students\n')


for i in range(5):
roll = int(input('Roll: '))
name = input('Name: ')
data = [roll, name]

# write the object data into binary file using the dump() method
pickle.dump(data, fileobj)

print('All data stored successfully')


PROGRAM 3
Aim: to create binary file to store Rollno and Name, Searchany Rollno and display name if
Rollno found otherwise “Rollno not found”

Program:
import pickle

def write():

D={}

f=open("Studentdetails.dat","wb")

while True:

r = int(input ("Enter Roll no : "))

n = input("Enter Name : ")

D['Roll No'] = r

D['Name'] = n

pickle.dump(D,f)

ch = input("More ? (Y/N)")

if ch in 'Nn':

break

f.close()

def Search() :

found = 0

rollno= int(input("Enter Roll no Whose name you want to display :"))

f = open("Studentdetails.dat", "rb")
try:

while True:

rec = pickle.load(f)

if rec['Roll No']==rollno:

print(rec['Name'])

found = 1

break

except EOFError:

if found == 0:

print("Sorry not Found....")

f.close()

write()

Search()

OUTPUT:

Enter Roll no : 1

Enter Name : jayna

More ? (Y/N)y

Enter Roll no : 2

Enter Name : Daksh

More ? (Y/N)y

Enter Roll no : 3
Enter Name : vyom

More ? (Y/N)N

Enter Roll no Whose name you want to

display :3

vyom
PROGRAM 4
Aim : Unpickling data in python
Program:
import pickle

print("The data that were stored in file are: ")

fileobject=open("mybinary.dat","rb")

objectvar=pickle.load(fileobject) fileobject.close()

print(objectvar)

You might also like