CSPracticalFile
CSPracticalFile
def area_of_circle(radius):
return 3.14*radius*radius
def perimeter_of_circle(radius):
return 2*3.14*radius
choice = int(input("Enter 1 for Area of Circle and 2 for Perimet
if choice == 1:
radius = float(input("Enter the radius of the circle: "))
print("Area of the Circle is: ", area_of_circle(radius))
elif choice == 2:
radius = float(input("Enter the radius of the circle: "))
print("Perimeter of the Circle is: ", perimeter_of_circle(ra
else:
print("Invalid Choice")
Output
Enter 1 for Area of Circle and 2 for Perimeter of Circle: 2
Enter the radius of the circle: 10
Perimeter of the Circle is: 62.800000000000004
Practical File 1
❔ Input any number from the user and calculate the factorial of a number.
Output
Enter a number: 5
The factorial of 5 is 120
❔ Input any number from the user and check if it is Prime no. or not.
Output
Practical File 2
Enter any number: 10
Not a Prime number
def sum_list(list):
if len(list) == 0:
return 0
else:
return list[0] + sum_list(list[1:])
print(sum_list([1,2,3,4,5]))
Output
15
Practical File 3
print("Word found at position: ", i+1)
break
Output
sentence = input("Enter a sentence: ")
word = input("Enter a word to search: ")
sentence = sentence.split()
for i in range(len(sentence)):
if sentence[i].lower() == word.lower():
print("Word found at position: ", i+1)
break
❔ Write a Program to enter the string and to check if it’s palindrome or not
using a loop.
Output
Enter the string: dad
The string is palindrome
Practical File 4
❔ Read a text file line by line and display each word separated by a #.
Output
L#o#r#e#m# #i#p#s#u#m# #d#o#l#o#r# #s#i#t# #a#m#e#t#,# #c#o#n#s#
#
#%
Practical File 5
vowels = 0
consonants = 0
uppercase = 0
lowercase = 0
for i in data:
if i in "aeiouAEIOU":
vowels += 1
elif i.isalpha():
consonants += 1
if i.isupper():
uppercase += 1
if i.islower():
lowercase += 1
print("Vowels:", vowels)
print("Consonants:", consonants)
print("Uppercase:", uppercase)
print("Lowercase:", lowercase)
Output
Vowels: 120
Consonants: 175
Uppercase: 6
Lowercase: 289
❔ Remove all the lines that contain the character 'a' in a file and write it to
another file.
Practical File 6
file_r = file.readlines()
for i in range(len(file_r)):
if "a" in file_r[i].lower():
file_a.write(file_r[i])
file_a.close()
file.close()
Output
❔ Create a binary file with the name and roll number. Search for a given roll
number and
display the name, if not found display the appropriate message.
Practical File 7
import pickle
Output
Enter the number of students: 2
Enter the roll number: 2
Enter the name: Pranshu
Enter the roll number: 1
Enter the name: Hridyanshu
Enter the roll number to search: 1
Name: Hridyanshu
Practical File 8
❔ Create a binary file with roll number, name, and marks. Input a roll
number and update
the marks.
import pickle
for i in range(n):
roll = int(input("Enter roll number: "))
name = input("Enter name: ")
marks = int(input("Enter marks: ")
data.append([roll, name, marks])
pickle.dump(data, file)
file.close()
Output
Enter number of students: 3
Enter roll number: 1
Enter name: Pranshu
Enter marks: 90
Enter roll number: 2
Enter name: Hridyanshu
Enter marks: 90
Enter roll number: 3
Enter name: Deepta
Enter marks: 100
Practical File 9
❔ Write a random number generator that generates random numbers
between 1 and 6
(simulates a dice).
import random
print(random.randint(1, 6))
Output
5
list1 = []
for i in range(0,5):
list1.append(i)
list1.pop()
print(list1)
Output
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
Practical File 10
❔ Create a CSV file by entering user-id and password, and read and
search for the
password for the given userid.
import csv
def write_csv():
with open('user.csv', 'w') as file:
writer = csv.writer(file)
writer.writerow(['user-id', 'password'])
while True:
user_id = input('Enter user-id: ')
password = input('Enter password: ')
writer.writerow([user_id, password])
choice = input('Do you want to enter more user-id an
if choice.lower() == 'n':
break
def read_csv():
with open('user.csv', 'r') as file:
reader = csv.reader(file)
user_id = input('Enter user-id to search password: ')
for row in reader:
if row[0] == user_id:
print(f'Password for user-id {user_id} is {row[1
break
else:
print(f'User-id {user_id} not found')
write_csv()
read_csv()
Output
Practical File 11
Enter user-id: 1
Enter password: hellp
Do you want to enter more user-id and password? (y/n): n
Enter user-id to search password: 1
Password for user-id 1 is hellp
import csv
def read_csv_file(filename):
with open(filename, 'r') as csv_file:
reader = csv.reader(csv_file)
for row in reader:
Practical File 12
print(row)
Output
['Name', 'John']
['Age', '25']
['City', 'New York']
Practical File 13