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

CSPracticalFile

The document contains a series of Python programming tasks, including functions to calculate the area and perimeter of a circle, factorial calculation, prime number checking, recursive list summation, string palindrome checking, and file operations such as reading text files and managing CSV files. It also includes examples of using binary files for student data and generating random numbers. Each task is accompanied by code snippets and sample outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CSPracticalFile

The document contains a series of Python programming tasks, including functions to calculate the area and perimeter of a circle, factorial calculation, prime number checking, recursive list summation, string palindrome checking, and file operations such as reading text files and managing CSV files. It also includes examples of using binary files for student data and generating random numbers. Each task is accompanied by code snippets and sample outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Practical File

❔ Create Functions in Python to calculate the Area and Perimeter of the


Circle. Write a
Python program to ask user to input their choice 1 or 2. If the choice is 1
calculate the
Area of the Circle, if the choice is 2 calculate the Perimeter of the Circle
otherwise show
an error message.

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.

integer = int(input("Enter a number: "))


factorial = 1
for i in range(1, integer+1):
factorial = factorial * i
print("The factorial of", integer, "is", factorial)

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.

input_number = int(input("Enter any number: "))


for i in range(2, input_number):
if input_number % i == 0:
print("Not a Prime number")
break
else:
print("Prime number")
break

Output

Practical File 2
Enter any number: 10
Not a Prime number

❔ Write a program to find the sum of elements of the List recursively.

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

❔ Program to search any word in a given string/sentence.

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():

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.

input_string = input("Enter the string: ")


if input_string == input_string[::-1]:
print("The string is palindrome")

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 #.

file = open("test.txt", "r")


data = file.read()
for i in range(len(data)):
print(data[i], end="#")

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#
#
#%

❔ Read a text file and display the number of


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

file = open("test.txt", "r")


data = file.read()

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.

file = open("test.txt", "r")


file_a = open("text.txt", "w")

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

file = open("file.dat", "wb")


n = int(input("Enter the number of students: "))
data = []
for i in range(n):
int_roll = int(input("Enter the roll number: "))
int_name = input("Enter the name: ")
individual_data = [int_roll, int_name]
data.append(individual_data)
pickle.dump(data, file)
file.close()

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


data_s = pickle.load(file)
roll = int(input("Enter the roll number to search: "))
for i in range(len(data_s)):
if roll == data_s[i][0]:
print("Name: ", data_s[i][1])

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

file = open("student.dat", "wb")


n = int(input("Enter number of students: "))
data = []

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

❔ Write a Python program to implement a stack using the list.

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

❔ Write a Python program to write a Python dictionary to a csv file. After


writing the CSV
file read the CSV file and display the content.

import csv

def write_csv_file(data, filename):


with open(filename, 'w') as csv_file:
writer = csv.writer(csv_file)
for key, value in data.items():
writer.writerow([key, value])

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)

data = {'Name': 'John', 'Age': 25, 'City': 'New York'}


filename = 'data.csv'
write_csv_file(data, filename)
read_csv_file(filename)

Output
['Name', 'John']
['Age', '25']
['City', 'New York']

Practical File 13

You might also like