0% found this document useful (0 votes)
45 views

XII Practical Program File Term1 2021 Zeeshan

The document contains source code for 8 programs written by a student named Zeeshan Mallick for their Computer Science Class XII. The programs include functions to read and manipulate text files, generate random numbers, search binary files, and calculate tent prices using user-defined functions. The code demonstrates the student's ability to write Python programs to perform various tasks like file input/output, string and data manipulation, and defining reusable functions.

Uploaded by

Zeeshan Malik
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)
45 views

XII Practical Program File Term1 2021 Zeeshan

The document contains source code for 8 programs written by a student named Zeeshan Mallick for their Computer Science Class XII. The programs include functions to read and manipulate text files, generate random numbers, search binary files, and calculate tent prices using user-defined functions. The code demonstrates the student's ability to write Python programs to perform various tasks like file input/output, string and data manipulation, and defining reusable functions.

Uploaded by

Zeeshan Malik
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
You are on page 1/ 25

Delhi Public School Panvel

Computer Science Class XII

Program File

Academic Year 2021-22

Name of the student: Zeeshan Mallick

Roll number: 8
Index
Sr no. Name of the Program Page Signature
number

1 Read a text file line by line and display each word separated by a
#

2 Read a text file and display the number of


vowels/consonants/uppercase/lowercase characters in the file.

3 Remove all the lines that contain the character 'a' in a file and
write it to another file

4 Create a binary file with name and roll number. Search for a
given roll number and display the name, if not found display
appropriate message.

5 Write a random number generator program that generates


random numbers between 1 and 6 (simulates a dice).

6 Write a program to search for name in binary file and display


the record number that contains the name.

7 Program to create CSV file and store empno, name, salary and
search any empno and display name, salary and if not found,
display appropriate message

8 Write a program to calculate the payable amount for the tent


using user defined functions.

9 Write a program using a user defined function that displays sum


of first n natural numbers, where n is passed as an argument.

10 Write a program using a user defined function that accepts the


first name and last name as arguments, concatenate them to get
full name and displays the output as: Hello full name

11 Write a program using user defined function that accepts length


and breadth of a rectangle and returns the area and perimeter of
the rectangle

12 Write a program that simulates a traffic light

13 Write a program to write following data in CSV file.

Employee number: Employee name: Employee salary:


Program1: Read a text file line by line and display each word separated by a #

SOURCE CODE:

file_object= open("C:\\Users\\Zeeshan\\Documents\\Laughnowcrylater.txt","r")

my_file=file_object.read(10)

for i in my_file:

words=i.split()

for n in words:

print(n+"#")

file_object.close()

Output:

W#

o#

a#

h#

,#

w#

o#

a#

h#
Program 2: Read a text file and display the number of vowels/consonants/uppercase/lowercase
characters in the file.

SOURCE Code:

file=open("C:\\Users\\Zeeshan\\Documents\\Laughnowcrylater.txt","r")

file_object=file.read()

word=(file_object.splitlines())

print(word)

n=len(word)

vowel=consonent=uppercase=lowercase=0

vowels=('a','e','i','o','u','A','E','U','I','O')

cons=("b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z",

"B","C","D","F","G","H","J","K","L","M","N","P","Q","R","S","T","V","W","X","Y","Z")

for n in range(0,n):

for i in word[n]:

for x in i:

if x.isupper():

uppercase+=1

elif x.islower():

lowercase+=1

if x in vowels:

vowel+=1

elif x in cons:

consonent+=1

print("number of vowels present is: ",vowel)

print("number of consonents: ",consonent)

print("number of upper case alphabets: ",uppercase)

print("number of lower case alphabets: ",lowercase)

Output
['Woah, woah', 'Yeah', 'Sometimes we laugh and sometimes we cry, but I guess you know now, baby', 'I took
a half and she took the whole thing, slow down, baby (baby)', "We took a trip, now we on your block and it's
like a ghost town, baby", "Where do these niggas be at when they say they doin' all this and all that?"]

number of vowels present is: 84

number of consonants: 142

number of upper case alphabets: 7

number of lower case alphabets: 219


Program 3: Remove all the lines that contain the character 'a' in a file and write it to another file

SOURCE Code:

file=open("C:\\Users\\Zeeshan\\Documents\\Laughnowcrylater.txt","r")

object1=file.read()

words=(object1.splitlines())

print(words)

p=len(words)

print(p)

for n in words:

if "a" in n:

file1=open("C:\\Users\\Zeeshan\\Documents\\Laughnowcrylater.txt","a")

file1.write(n)

file1.close()

file.close()

Output:

['Woah, woah', 'Yeah', 'Sometimes we laugh and sometimes we cry, but I guess you know now, baby', 'I took
a half and she took the whole thing, slow down, baby (baby)', "We took a trip, now we on your block and it's
like a ghost town, baby", "Where do these niggas be at when they say they doin' all this and all that?"]

6
Program 4: Create a binary file with name and roll number. Search for a given roll number and display
the name, if not found display appropriate message.

SOURCE Code:

import pickle

stu={}

def to_write():

Bfile=open("data.dat","ab")

num=int(input("ENTER NO OF STUDENTS: "))

for i in range(num):

stu["roll"]=int(input("Enter roll number: "))

stu["name"]=input("enter the name: ")

pickle.dump(stu,Bfile)

Bfile.close()

def search_data():

Bfile=open("data.dat","rb")

Z=int(input("enter the rollno to search: "))

found=0

try:

while True:

edata=pickle.load(Bfile)

if edata["roll"]==Z:

print("The rollno =",Z," record found")

print(edata)

found=1

break

except EOFError:

pass

if found==0:
print("The rollno =",Z," record is not found")

Bfile.close()

while True:

print("\n 1-Write in a file \n 2-search ")

ch=int(input("Enter your choice = "))

if ch==1:

to_write()

if ch==2:

search_data()

break

Output:

1-Write in a file

2-search

Enter your choice = 1

ENTER NO OF STUDENTS: 2

Enter roll number: 20

enter the name: XAXB

Enter roll number: 21

enter the name: NXAX

1-Write in a file
2-search

Enter your choice = 2

enter the rollno to search: 20

The rollno = 3 record found

{'roll': 20, 'name': 'XAXB'}


Program 5: Write a random number generator program that generates random numbers between 1 and 6
(simulates a dice).

SOURCE Code:

import random

print("Random Number Generator ")

i=(random.randint(1,6))

print("randomised number is: ",i)

Output:

Random Number Generator

randomised number is: 5


Program 6: Write a program to search for name in binary file and display the record number that
contains the name.

Source Code:

import pickle

data={}

def write():

file=open("name.dat","ab")

no=int(input("NO. of records?: "))

for i in range(no):

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

data["recordno"]=i+1

data["name"]=input("enter the name: ")

pickle.dump(data,file)

file.close()

def search():

file=open("name.dat","rb")

n=input("Enter name to be searched: ")

found=0

try:

while True:

dz=pickle.load(file)

if dz["name"]==n:

print("The name",n," record found")

print(dz)

found=1

break

except EOFError:

pass
if found==0:

print("The name ",n," record is not found")

file.close()

while True:

print("MENU \n 1-Write in a file \n 2-search ")

ch=int(input("Enter your choice = "))

if ch==1:

write()

if ch==2:

search()

break

Output:

ENU

1-Write in a file

2-search

Enter your choice = 1

NO. of records?: 2

Enter details of student 1

enter the name: zereshan

Enter details of student 2

enter the name: utfkgj

MENU

1-Write in a file
2-search

Enter your choice = 2

Enter name to be searched: zereshan

The name zereshan record found

{'recordno': 1, 'name': 'zereshan'}


Program 7: Program to create CSV file and store empno, name, salary and search any empno and display
name, salary and if not found, display appropriate message

Source Code:

import csv

with open('employee.csv',mode='a') as csvfile:

writer=csv.writer(csvfile,delimiter=',')

anss='y'

while anss.lower()=='y':

eno=int(input("Enter the Employee Number: "))

name=input("Enter Employee Name: ")

salary=float(input("Enter the Salary: "))

writer.writerow([eno,name,salary])

print("## Data Saved...#")

anss=input("ADD MOREE?? ")

anss="y"

with open('employee.csv',mode='r') as csvfile:

reader=csv.reader(csvfile,delimiter=',')

while anss=="y":

found=False

e=int(input("Enter the Employee Number to be found: "))

for row in reader:

if len(row)!=0:

if int(row[0])==e:

print("====================")

print("NAME: ",row[1])

print("SALARY: ",row[2])

found=True

break
if not found:

print("================")

print(" EMPNO NOT FOUND ")

print("================")

anss=input("Search More? (Y)")

Output:

Enter the Employee Number: 21

Enter Employee Name: were

Enter the Salary: 23330

## Data Saved...#

ADD MOREE?? n

Enter the Employee Number to be found: 21

====================

NAME: were

SALARY: 23330.0

Search More? (Y)n


Program 8: Write a program to calculate the payable amount for the tent using user defined functions.

SOURCE Code:

def cyl(h,r):

area_cyl=2*3.14*h*r

return area_cyl

def con(l,r):

area_con=3.14*r*l

return area_con

def tax_price(Cost):

tax=0.18*Cost

net_tax_price=Cost +tax

return net_tax_price

print("enter the values of cylindrical part of tent in metres ")

h=float(input("enter the height "))

rad=float(input("enter the radius "))

area1=cyl(h,rad)

print(area1)

l=float(input("enter the length of the conical part "))

area2=con(l,rad)

print(area2)

canvas_area=area1+area2

print("the canvas area is",canvas_area,'m^2')

unit_price=(float(input("enter the price of 1 m^s of the canvas in rupees ")))

price=unit_price*canvas_area

print("the total amount is: ",price)

net_price=tax_price(price)
print("the payable amount is: ",net_price)

Output:

enter the values of cylindrical part of tent in metres

enter the height 10

enter the radius 5

314.0

enter the length of the conical part 15

235.50000000000003

the canvas area is 549.5 m^2

enter the price of 1 m^s of the canvas in rupees 2

the total amount is: 1099.0

the payable amount is: 1296.82


Program 9: Write a program using a user defined function that displays sum of first n natural numbers,
where n is passed as an argument.

SOURCE CODE:

def sumsquare(num):

sum=0

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

sum= sum+i

print("the sum of first",num,"natural numbers are",sum)

num=int(input("enter the count: "))

sumsquare(num)

Output:

enter the count: 10

the sum of first 10 natural numbers are 55


Program 10: Write a program using a user defined function that accepts the first name and last name as
arguments, concatenate them to get full name and displays the output as: Hello full name

SOURCE Code:

def fullname(first,last):

fullname = first + " " + last

print("Hello",fullname)

first = input("Enter first name: ")

last = input("Enter last name: ")

fullname(first,last)

Output

Enter first name: XOXOXO

Enter last name: LOLOLO

Hello XOXOXOLOLOLO
Program 11: Write a program using user defined function that accepts length and breadth of a rectangle
and returns the area and perimeter of the rectangle

SOURCE Code:

def perimeter_area(L,B):

area = L*B

perimeter = 2 * (L + B)

return (area,perimeter)

l = float(input("Enter length of the rectangle: "))

b = float(input("Enter breadth of the rectangle: "))

area,perimeter =perimeter_area(l,b)

print("Area is:",area,"\nPerimeter is:",perimeter)

Output:

Enter length of the rectangle: 2

Enter breadth of the rectangle: 2

Area is: 4.0

Perimeter is: 8.0


Program 12: Write a program that simulates a traffic light. The program should consist of the following:

1. A user defined function trafficLight( ) that accepts input from the user, displays an error message if the
user enters anything other than red, yellow, and green. Function light() is called and following is
displayed depending upon return value from light().

a) “stop” if the value returned by light() is 0.

b) “go slow“ if the value returned by light() is 1

c) “go” if the value returned by light() is 2.

2. A user defined function light() that accepts a string as input and returns 0 when the input is red, 1
when the input is yellow and 2 when the input is green. The input should be passed as an argument.

3. Display “ SPEED THRILLS BUT KILLS” after the function trafficLight( ) is executed.

SOURCE Code:

def trafficlight():

signal=input('enter the colour of signal: ')

if signal not in ('red','green'',yellow'):

print('please enter the valid traffic colour in small letters')

else:

value=light(signal)

if value==0:

print('stop')

elif value==1:

print('go slow')

else:

print('go')

def light(colour):

if colour==('red'):

return(0)

elif colour==('yellow'):

return(1)
else:

return(2)

trafficlight()

print('SPEED THRILLS BUT KILLS')

Output:

enter the colour of signal: red

stop

SPEED THRILLS BUT KILLS


Program 13: Write a program to write following data in CSV file.

Employee number:

Employee name:

Employee salary:

SOURCE Code:

import csv

with open('ename.csv',mode='a') as csvfile:

writer=csv.writer(csvfile,delimiter=',')

anss='y'

while anss.lower()=='y':

eno=int(input("Enter the Employee Number: "))

name=input("Enter Employee Name: ")

salary=float(input("Enter the Salary: "))

writer.writerow([eno,name,salary])

print("## Data Saved...#")

anss=input("WANT TO ADD MOREE DATA (y/n)?? ")

Output:

Enter the Employee Number: 4

Enter Employee Name: tom

Enter the Salary: 2020

## Data Saved...#

WANT TO ADD MOREE DATA (y/n)?? y

Enter the Employee Number: 5

Enter Employee Name: jerry

Enter the Salary: 5900

## Data Saved...#
WANT TO ADD MOREE DATA (y/n)?? n

You might also like