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

Computer Science Vikas Prajapati 12 A (Word)

The document is a Python programming practical file submitted by Vikas Prajapati, a class 12 student. It contains 20 questions related to Python programming. The questions cover topics like palindrome checking, word counting, menu driven programs, random number generation, arithmetic operations, string checking, area and circumference calculations, Fibonacci series, using math modules, stacks, reading and processing text files, binary files, lists, tuples, dictionaries and ASCII codes. For each question, code snippets with answers are provided.

Uploaded by

Vikas Prajapati
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)
88 views

Computer Science Vikas Prajapati 12 A (Word)

The document is a Python programming practical file submitted by Vikas Prajapati, a class 12 student. It contains 20 questions related to Python programming. The questions cover topics like palindrome checking, word counting, menu driven programs, random number generation, arithmetic operations, string checking, area and circumference calculations, Fibonacci series, using math modules, stacks, reading and processing text files, binary files, lists, tuples, dictionaries and ASCII codes. For each question, code snippets with answers are provided.

Uploaded by

Vikas Prajapati
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/ 44

PYTHON

PROGRAMMING
NOIDA PUBLIC
SCHOOL

Computer Science with


Python [083]
PRACTICAL FILE
Session: 2021-2022

Submitted To: Submitted By:


Mrs. Shweta Aapen Vikas Prajapati
PGT Computer Science Roll No.-_______
Acknowledgement
In the accomplishment of this practical successfully, many
people have best owned upon me their blessings and the
heart pledge support, this time I am utilizing to thank all the
people who have been concerned with this practical.
Primarily I would like thank God for being able to complete
this project with success. Then I would like to thank my
Principal Mrs. Charu Jain and my Computer science teacher
Mrs. Shweta Aapen whose valuable guidance has been the
ones that helped me patch this practical and make it full proof
success, their suggestions and instruction has served as the
major contribution towards the completion of this practical.
Then I would like to thank my parents who have helped me
with their valuable suggestions and guidance, has been very
helpful in various phases of the completion of the practical.
Certificate

This is to certify that Vikas Prajapati student of class 12th has


successfully completed the research on the below mentioned
practical under the guidance of Mrs. Shweta Aapen during
the year of 2020-21 in partial fulfillment of computer science
practical examination conducted by Noida Public School,
Noida.

________

Signature of External Signature of Internal


Examiner Examiner
INDEX
S.No. TOPIC Pg.No. Remarks
1. Write a program to check whether it is a
palindrome or not.
2. Write a program to count number of words
in a file.
3. Write a menu driven program to calculate:
Area of square[A=a*a] , area of
rectangle[A=l*b] and area of
triangle[A=1/2*B*H].
4. Write a random number generator
that generates a random no. between 1 to 6
(simulates a dice).
5. Write a program to enter two numbers and
perform arithmetic operations like
+,-,/,//,*and%.
6. Write a program that reads a string and
check whether it is a string or not.
7. Write a program to find the area and
circumference of a circle using its radius
8. Fibonacci Series program Using While Loop
9. Write a program to perform some functions
using maths module.
10. Write a program to display vowels present
in word using stack in python.

11. Read a text file by line and display the no. of


vowels/consonants/uppercase/lowercase.
12. Read a text file line by line and
display each word separated by #.
13. Read a text file by line and display the no. of
vowels /consonants/ uppercase /
lowercase.
14. 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..
15. Write a program that adds individual
elements of list 2 and list 3 into 1. Resultant
list should be in order of elements of list 3,
list 1, list 2.
16. Write a program that find an element
index/position in a tuple without using
index().
17. Write a program that checks for the
presence of a value inside a dictionary and
print its keys with ignore case.
18. Write a program to display ASCII code of
character and vice versa.
19. Write a program to create binary file to
store Roll no. and Name , Search and display
name if Roll no. found otherwise ‘Roll no.
not found’.
20. Write a program to perform read and write
operation with csv file.
Q1. Write a program to check whether it is a
palindrome or not.
Ans.
num=int(input('Enter a number:'))

n=num

res=0

while num>0:

rem=num%10

res=rem+res*10

num=num//10

if res==n:

print(n,'is a palindrome')

else:

print(n,'is not palindrome')


Q2. Write a program to count number of words in a
file.
Ans.
fin=open('file1.txt','r')

str=fin.read()

L=str.split()

count_words=0

print('Content of file:-',str)

for i in L:

count_words=count_words+1

print('Total no. of words:',count_words)


Q3. Write a menu driven program to calculate: Area
of square , area of rectangle and area of triangle.
Ans.
def square(a):

return a*a

def rectangle(l,b):

return l*b

def triangle(B,H):

return 1/2*B*H

while 1:

print('*'*43)

print('~~~~~~~~~~~~~~Main menu~~~~~~~~~~~~')

print('1 for calculate area of square[A=a*a]')

print('2 for calculate area of rectangle[A=l*b]')

print('3 for calculate area of triangle[A=1/2*B*H]')

print('*'*30)

n=int(input('Enter your choice:'))

if n==1:

s=float(input('Enter side of square:'))

area=square(s)

print('Area of square is:',area)

elif n==2:

l=float(input('Enter length of rectangle:'))


b=float(input('Enter breadth of rectangle:'))

area=rectangle(l,b)

print('Area of rectangle is:',area)

elif n==3:

B=float(input('Enter base of triangle:'))

H=float(input('Enter height of triangle:'))

area=triangle(B,H)

print('Area of triangle is:',area)

else:

print('Wrong choice')

chr=input('Y/N:')

if chr=='n' or chr=='N':

break
Q4. Write random number generator that generates
random no. between 1 to 6 (simulates a dice).
Ans.
import random
a=0
b=7
print('A random no. from range is:',end='')
print(random.randrange(a,b))
Q5. Write a program to enter two numbers and
perform arithmetic operations like +,-,/,//,*and%.
Ans.
val1=float(input('enter the first value:'))

val2=float(input('enter the second value:'))

op=input('enter anyone of the operator(+,-,*,/,//,%):')

if op=='+':

result=val1+val2

elif op=='-':

result=val1-val2

elif op=='*':

result=val1*val2

elif op=='/':

if val2==0:

print('Please enter v value other than 0')

else:

result=val1/val2

elif op=='//':

result=val1//val2

else:

result=val1%val2

print('The result is:',result)


Q6. Write a program that reads a string and check
whether it is a string or not.
Ans.
my_str='PraCtiCaL254'

my_str=my_str.casefold()

rev_str=reversed(my_str)

#check if the string is equal to its reverse

if list(my_str)==list(rev_str):

print('The string is a palindrome.')

else:

print('The string is not a palindrome.')


Q7. Write a program to find the area and the
circumference of the circle using its radius.
Ans.
#Python program to find area of circle using radius

Pi=3.14

radius=float(input('Please enter the radius of circle:'))

area=Pi*radius*radius

circumference=2*Pi*radius

print('Area of circle=%.2f'%area)

print('Circumference of circle=%.2f'%circumference)
Q8. Fibonacci Series program Using While Loop.
Ans.
Number=int(input('\nPlease enter the range number:'))

#initializing the first and second value of a series

i=0

first_value=0

second_value=1

#finding and displaying fibonacci series

while(i<Number):

if(i<=1):

Next=i

else:

Next=first_value+second_value

first_value=second_value

second_value=Next

print(Next)

i=i+1
Q9. Write a program to perform some functions using
maths module.
Ans.
import math

a=math.ceil(1.03)

b=math.sqrt(81.0)

c=math.fabs(1.0)

d=math.floor(1.03)

e=math.pow(4.0,2.0)

print(a,b,c,d,e)
Q10. Write a program to display vowels present in a
word using stack in python.
Ans.
vowels=['a','e','i','o','u']

word=str(input('Enter the word to search for vowels:'))

stack=[]

for letter in word:

if letter in vowels:

if letter not in stack:

stack.append(letter)

print(stack)

print("The number of different vowels present in",word,"is:",len(stack))


Q11. Read a text file by line and display the no. of
vowels/consonants/uppercase/lowercase.
Ans.
def cnt():

f=open(r"C:\Users\subha\Desktop\test2.txt","r")

cont=f.read()

print(cnt)

v=0

cons=0

l_c_l=0

u_c_l=0

for ch in cont:
if (ch.islower()):

l_c_l+=1

elif(ch.isupper()):

u_c_l+=1

ch=ch.lower()

if( ch in ['a','e','i','o','u']):

v+=1

elif (ch in ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z']):

cons+=1

f.close()

print("Vowels are : ",v)

print("consonants are : ",cons)

print("Lower case letters are : ",l_c_l)

print("Upper case letters are : ",u_c_l)

#main program

cnt()
Q12. Read a text file line by line and display each
word separated by #.
Ans.
file=open(r"C:\Users\subha\Desktop\test.txt","r")

doc=file.readlines()

for i in doc:

words=i.split()

for a in words:

print(a+"#")

file.close()
Q13. Read a text file by line and display the no. of
vowels/consonants/uppercase/lowercase.
Ans.
def cnt():

f=open(r"C:\Users\subha\Desktop\test2.txt","r")

cont=f.read()

print(cnt)

v=0

cons=0

l_c_l=0

u_c_l=0

for ch in cont:

if (ch.islower()):

l_c_l+=1

elif(ch.isupper()):

u_c_l+=1

ch=ch.lower()

if( ch in ['a','e','i','o','u']):

v+=1

elif (ch in ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z']):

cons+=1

f.close()
print("Vowels are : ",v)

print("consonants are : ",cons)

print("Lower case letters are : ",l_c_l)

print("Upper case letters are : ",u_c_l)

#main program

cnt()
Q14.Creat a binary file with name and roll number.
Search for a given roll number and display the name,
if not found display appropriate message.
Ans.
f=open('s1','w+b') # Create a binary file with name,

n=int(input('Enter number of students:'))

for i in range(n):

name=input('Enter name of student:')

rollno=input('Enter rollno:')

marks=input('Enter marks:')

bname=bytes(name,encoding='utf-8')

brollno=bytes(rollno,encoding='utf-8')

bmarks=bytes(marks,encoding='utf-8')

f.write(brollno)

f.write(bname)

f.write(bmarks)

f.write(b'\n')

f.seek(0)

data=f.read()

f.seek(0)

sk=input('Enter the roll no whose marks need updatation :')

bsk=bytes(sk,encoding='utf-8')

l=len(bsk)
loc=data.find(bsk)

if loc<0:

print('Details not present')

else:

f.seek(loc+l,0)

i=0

while f.read(1).isalpha():

i=i+1

f.seek(-1,1)

marksu=input('Enter updated marks:')

bmarksu=bytes(marksu,encoding='utf-8')

f.write(bmarksu)

print("Entire content of file after updation is :")

f.seek(0)

udata=f.read()

print(udata.decode())
Q15. Write a program that adds individual elements
of list2 and list3 into 1. Resultant list should be in
order of list3, list1, list2.
Ans.
l1=[11,34,45,56,67]

l2=[22,68,90,112,134]

l3=[21,34,55,76,88]

print('List1:-',l1)

print('List2:-',l2)

print('List3:-',l3)

l3.extend(l1)

l3.extend(l2)

print('After adding elements of two list:-',l3)


Q16. Write a program that find an element index
/position in a tuple without using index().
Ans.
tuple1=('p','y','t','h','o','n')

char=input('Enter a single letter without quotes:')

if char in tuple1:

count=0

for a in tuple1:

if a!=char:

count+=1

else:

break

print(char,'is an index',count,'in',tuple1)
Q17. Write a program that checks for the presence of
a value inside a dictionary and print its keys with
ignore case.
Ans.
info={'Subhash':'Engineer','Mohit':'BTech','Dhoni':'Cricketer','Ambani':'Businessman'}

inp=input('Enter value to be searched for:')

for a in info:

if(info[a].upper()==inp.upper()):

print('The key of given value is',a)

break

else:

print('Given value does not exist in the dictionary')


Q18. Write a program to display ASCII code for
character and vice versa.
Ans.
var=True

while var:

choice=int(input('Press-1 to find the oridnal value of a character\nPress-2 to find a


character of value\n'))

if choice==1:

ch=input('Enter a character:')

print(ord(ch))

elif choice==2:

val=int(input('Enter an integer value:'))

print(chr(val))

else:

print('You entered a wrong choice')

print('Do you wnat to continue?Y/N')

option=input()

if option=='y' or option=='Y':

var=True

var=False
Q19. Write a program to create binary file to store
Roll No. and Name , Search any Roll No. and display
name if roll no. found otherwise ‘Roll no. not found’.
Ans.
import pickle

student=[]

f=open('student.dat','wb')

ans='y'

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

roll=int(input('Enter Roll Number:'))

name=input('Enter Name:')

student.append([roll,name])

ans=input('Add More?(y)')

pickle.dump(student,f)

f.close()

f=open('student.dat','rb')

student=[]

while True:

try:

student=pickle.load(f)

except EOFError:

break

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

found=False

r=int(input('Enter Roll number to search:'))

for s in student:

if s[0]==r:

print('##Name is:',s[1],'##')

found=True

break

if not found:

print('###Sorry! Roll number not found ###')

ans=input('Search more?(y):')

f.close()
Q20. Write a program to perform read and write
operation with csv file.
Ans.
import csv

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

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

ans='y'

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

eno=int(input('Enter Employee Number:-'))

name=input('Enter Employee Name:-')

salary=int(input('Enter Employee Salary:-'))

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

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

ans=input('Add More?')

ans='y'

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

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

print(myreader)

while ans=='y':

found=False

e=int(input('Enter Employee Number To Search:'))

for row in myreader:

print(row)
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('============================')

ans=input('Search More?(y)')

You might also like