You are on page 1of 42

1

C.S.- PRACTICAL FILE

➡ By Arnav Khokhar (XII-A)


➡ Year - 2023-2024
➡ Roll No. -
2

Table Of Contents

Topic I.............................................................................................................................................................................. 2
Strings............................................................................................................................................................................. 2
Topic II............................................................................................................................................................................. 6
List................................................................................................................................................................................... 6
Topic III.......................................................................................................................................................................... 13
Tuples............................................................................................................................................................................ 13
Topic IV..........................................................................................................................................................................17
Dictionary......................................................................................................................................................................17
Topic V...........................................................................................................................................................................22
For - While Loops......................................................................................................................................................... 22
Topic VI..........................................................................................................................................................................26
If - else Statement.........................................................................................................................................................26
Topic VII.........................................................................................................................................................................30
Text Files....................................................................................................................................................................... 30
Topic VIII........................................................................................................................................................................34
Binary Files................................................................................................................................................................... 34
Topic IX..........................................................................................................................................................................36
User Defined Functions............................................................................................................................................... 36
3

Topic I
Strings
Ques 1. Write a program to input a string and perform the following operations on
the string
● Total number of characters

● Total number of alphabets

● Total number of digits

● Total number of vowels

● Total number of space

Ans.

2
Ques 2. Consider the string Str = “Global Warming”. Write statements in python
to implement the following:
● To display the last four characters

● To display the substring starting from index 4 and ending at index 8.

● To check whether the string has alphanumeric characters or not. ● To display the

starting index of the given string


4

● To change the case of the given string


● To check if the string is in the title case.

● To replace all the occurrences of letter ‘a’ in the string with ‘*’. Ans.

3
Ques 3. Write a function deleteChar() which takes two parameters one is a string
and other is a character. The function should create a new string after deleting all
occurrences of the character from the string and return the new string.
Ans.
5

Ques 4. Input a string having some digits. Write a function to return the sum of
digits present in this string.
Ans.

4
Ques 5. Write a program to enter a string and a character. Count the number of
times a letter appears in a string.
Ans.
6

Topic II
List
Ques1. Write a program to input a list from the user and reposition all its elements by
shifting each of them to one position before and by shifting the first element to the last
position. For example, If the entered list is [22, 25, 70, 32, 12], then the changed list
should be [25, 70, 32, 12, 22] (Do not use slicing in the program).
Ans.
7

6
Ques 2. Write a program to input a list P, from the user and modify its content in such
a way that the elements which are multiples of 10 swap with the value present in the
very next position in the list. For example If the content of list P is: 91, 50, 54, 22, 30,
54 ,then the content of list P should become: 91, 54, 50, 22, 54, 30.
Ans.
8

7
Ques 3. Write a menu driven program which creates an empty list and gives the
following options to the user to perform various operations on a list:
(i) Append an element to the list
(ii) Input an element from the user and remove it from the list
(iii) Count the number of occurrences of an element in the list
(iv) Sort the list
(v) Display the list
Ans.
9

Output
10

9
11

10
Ques 4. Write a menu driven program which creates an empty list and gives the
following options to the user to perform various operations on the list: (i) Input an
element and a location from the user and insert the element at the entered location in
the list.
(ii) Input a location from the user and remove the element from that location in the list.
(iii) Input an element from the user and find its location in the list.
(iv) Display the list
Ans.
12

11

Output
13

12
Ques 5. Write a program to input a string and check if it is a Palindrome or not. Ans.

13

Topic III
Tuples
Ques 1. Write a program to read email IDs of N number of students and store
them in a tuple. Create two new tuples, one to store only the usernames from
the email IDs and second to store domain names from the email ids. Print all
14

three tuples at the end of the program.


Ans.

14
Ques 2. Write a program to accept n numbers from the user and store it in a
tuple.
Ans.
15

Ques 3. Write a program to remove duplicates from a tuple. Ans.

15
Ques 4. Write a program to multiply the adjacent elements of a
tuple. Ans.
16

Ques 5. Consider the following tuples, tuple1 and


tuple2: tuple1=(23,1,45,67,45,9,55,45)
tuple2=(100,200)
i. print(tuple1.index(45))
a. 2
ii. print(tuple1.count(45))
b. 3
iii. print(tuple1 + tuple2)
c. (23,1,45,67,45,9,55,45,100,200)
iv. print(len(tuple2))
d. 2
v. print(max(tuple1))
e. 67
vi print(min(tuple1))
f. 1
vii. print(sum(tuple2))
g. 300

16
17

Topic IV
Dictionary
Ques 1. Write a program to enter names of employees and their salaries for N
number of employees and store them in a dictionary. Here N is to input by the
user.
Ans.
18

17
Ques 2. Write a program to convert a number entered by the user into its
corresponding number in words. For example if the input is 876 then the output
should be ‘Eight Seven Six’.
Ans.

Ques 3. Write a Python program to find the highest 2 values in a dictionary. Ans.
19

18
Ques 4. Write a Python program to create a dictionary from a string ‘w3resource’
such that each individual character matches a key and its index value for first
occurrence matches the corresponding value in the dictionary. Ans.

19

Ques 5. Write a program to input your friend’s, names and their phone numbers
and store them in the dictionary as the key-value pair. Perform the following
operations on the dictionary:
20

a) Display the Name and Phone number for all your friends.
b) Add a new key-value pair in this dictionary and display the
modified dictionary
c) Delete a particular friend from the dictionary
d) Modify the phone number of an existing friend
e) Check if a friend is present in the dictionary or not
Ans.

20
21

21
22

22

Topic V
For - While Loops
Ques 1. Write a program to display the following pattern. i) 1 ii) *
13**
135***
1357****
Ans.
(i)
23

(ii)

23
Ques 2. Write a program to calculate sum of the series upto N terms. s = (1) +
(1+2)+ (1+2+3) + .....N
Ans.

Ques 3. Write a program to input a positive integer and check whether it is an


Armstrong number or not.
24

Ans.

24
Ques 4. Write a program to input a positive integer (say n); calculate and display the
number of digits, sum of the even digits and product of the odd digits. Ans.

25
25

Ques 5. Write a program to input a positive integer (say n); generate and display
first n Fibonacci series and display the sum of first n Fibonacci numbers. Ans.

26
26

Topic VI
If - else Statement
Ques 1. Write a python program to find the roots of a quadratic equation and
also display the nature of roots.
Ans.

27

Ques 2. Write a program to find whether the given year is a leap year or not.
Ans.
27

28

Ques 3. Write a program to calculate the Income Tax which accepts the Taxable
income and calculates the Tax based on the following criteria:
Income(Rs) Tax

0 to 3,00,000 NIL

3,00,001 to 5,00,000 5%

5,00,001 to 10,00,000 20%

Above 10,00,000 30%

Ans.
28

29

Ques 4. Write a program to generate a simple calculator to find addition,


subtraction, multiplication and division of two numbers.
Ans.
29

30
Ques 5. Accept three sides of a triangle and check whether it is an equilateral,
isosceles or scalene triangle.
Ans.
30

31

Topic VII
Text Files
Ques 1. Create a file student.txt and write 5 student’s name and roll
number into it. WAP to perform append operation on student.txt file
and add 5 more records in the end of the file.
Ans. fp=open(‘student.txt’,’a’)
print(‘Appending 5 more records to student.txt’)
for i in range(5):
rec=input(“Enter student’s name :”)
fp.write(rec)
print(“All 5 records are appended”)
fp.close()

Ques 2. WAP to display only 1st 10 bytes/characters from file


student.txt .
31

Ans. fp=open(‘student.txt’,’r’)
str1=fp.read(10)
print(str1)
fp.close()

Ques 3. WAP to count the total number of characters from


student.txt
Ans. fp=open(‘student.txt’,’r’)
str1=fp.read()
chcount=len(str1)
print(‘Number of characters in file student.txt
is’,chcount)
fp.close()

32
Ques 4. WAP to count the total number of words from the file
student.txt .
Ans. fp=open(‘student.txt’,’r’)
s1=fp.read()
l1=s1.split()
wrdcount=len(l1)
print(‘Number of words in file student.txt =’,wrdcount)
fp.close()

Ques 5. WAP to count total number of vowels in file student.txt


. Ans. fp=open(‘student.txt’,’r’)
str1=fp.read()
vcount=0
for i in str1:
32

If i in ‘AaEeIiOoUu’:
vcount+=1
print(‘Number of vowels in file student.txt =’,vcount)
fp.close()

Ques 6. WAP to search for a specific word in text file and display
its occurrences.
Ans. def wordoccur(filename,word):
With open(filename,’r’) as file:
content=file.read()
wcount=content.count(word)
print(‘Occurrences of’,word,’are’,wcount)
#Function Call
wordoccur(‘Student.txt’,marks)

33
Ques 7. WAP to remove all blank lines from a text file using
UDF. Ans. def remove_blankline(filename):
With open(filename,’r’) as file:
lines=file.readlines()
lines=[line.strip() for line in lines of
line.strip()] with open(filename,’w’) as file:
file.writelines(lines)
#Function Call
remove_blankline(‘student.txt’)
Ques 8. WAP to read a text file and sort its contents in alphabetical
order. Ans. def sort(filename):
with open(filename,’r’) as file:
lines=file.readlines()
33

lines.sort()
with open(filename,’w’) as file:
file.writelines(lines)
#Function Call
sort(‘student.txt’)
Ques 9. WAP to replace a specific word with another word in a text file
using UDF.
Ans. def replace(filename,old,new):
with open(filename,’r’) as file:
content=file.read()
modified_content=content.replace(old,new)
with open(filename,’w’) as file:
file.write(modified_contant)
#Function Call
replace(‘Student.txt’,marks,total_marks)

34
Ques 10. WAP to merge 2 text files and write the merged content into a new
file using UDF.
Ans. def merge(file1,file2,merged_file):
with open(file1,’r’) as file:
content1=file.read()
with open(file2,’r’) as file:
content2=file.read()
merged_content=content1+’\n’+content2
with open(merged_content,’w’) as file:
file.write(merged_content)
#Function Call
merge(‘file1.txt’,’file2.txt’,’merged_file.txt’)
34

35

Topic VIII
Binary Files
Ques 1. WAP to write three lines of text to a binary file file1.dat
. Ans. import pickle
fp=open(‘C:\\file1.dat’,’wb’)
rno=’11’
name=’Jai’
l1=[rno,name]
35

pickle.dump(l1,fp)
l2=[‘13’,’dhairya’]
pickle.dump(l2,fp)
l3=[‘20’,’sunny’]
fp.close()

Ques 2. WAP to read and display the content of binary file file1.dat
. Ans. import pickle
fp=open(‘C:\\file1.dat’,’rb’)
listrec=pickle.load(fp)
print(listrec)
fp.close()

Ques 3. WAP to append two lines of text in file file1.dat


. Ans. import pickle
fp=open(‘C:\\file1.dat’,’ab’)
l1=[‘21’,’Chunky’]
l2=[‘22’,’Akshay’]
pickle.dump(l1,fp)
pickle.dump(l2,fp)

36
fp.close()
Ques 4. WAP to search a specific roll number from binary file
file1.dat
Ans. import pickle
fp=open(‘C:\\file1.dat’,’rb’)
listrec=pickle.load(fp)
rno=input(‘Enter the roll number :’)
found=0
36

For row in listrec:


if row[0]==rno:
found+=1
print(‘Record Found’)
print(row)
If found==0:
print(‘Record doesn’t exist’)
fp.close()

Ques 5. WAP to merge the content of binary file A.dat and B.dat into
C.dat .
Ans. import pickle
f1=open(‘C:\\A.dat’,’rb’)
l1=pickle.load(f1)
f2=open(‘C:\\B.dat’,’rb’)
l2=pickle.load(f2)
listrec=l1+l2
f3=open(‘C:\\C.dat’,’wb’)
l3=pickle.dump(listrec,f3)
f1.close()
f2.close()

37
f3.close()
Topic IX
User Defined Functions
Ques 1. Write a program using user defined function calcPow() that accepts
base and exponent as arguments and returns the value BaseExponent where Base
and exponent are integers.
37

Ans.

Ques 2. Write a User Defined function to add 2 numbers and display their sum.
Ans.

38
Ques 3. Write a User Defined function to calculate the Total Surface Area
and Curved Surface Area of a right circular cylinder.
Ans.
38

39
Ques 4. Write a program using UDF that accepts an integer and increments
the value by 5. Also display the id of argument before and after increment.
Ans.
39

Ques 5. Write a program using a user defined function myMean() to


calculate the mean of floating values stored in a list.
Ans.

40
Ques 6. Write a program using a user defined function calcFact() to
calculate and display the factorial of a number n passed as an argument.
Ans.
40

Ques 7. Write a program using User Defined Function Grader() to


compute Student’s grade according to
percentage of total marks as:-
Percentage Grade
>=90 A
>=80 B
>=70 C
>=60 D
>=50 E
< 50 F
Ans.

41
41

42

Ques 8. Write a program using UDF to find the total cost for wrapping a box
of sides l cm, b cm and h cm at the rate of 4 Rs. per cm2. Where l,b and h
are inputted by the user as an argument.Note : default value of sides are 1
cm. Ans.

Ques 9. Write a program using UDF even() to display only even numbers
from a list of numbers passed as an argument.
Ans.
42

43
Ques 10. Write a User Defined Function that displays the sum of first n natural
numbers where n is passed as an argument.
Ans.

44

You might also like