LatestPythonLabManual-2023 Batch
LatestPythonLabManual-2023 Batch
LatestPythonLabManual-2023 Batch
1.a) Develop a program to read the student details like Name, USN, and Marks in three subjects.
Display the student details, total marks and percentage with suitable messages.
Output
****************************************************************
Enter the name
Jyothy
Enter the usn of student
1JT23CS001
Enter the marks of first subject
65
Enter the marks of second subject
70
Enter the marks of third subject
72
-----------------Displaying student details---------
Student Name Jyothy
Student USN 1JT23CS001
------------------Marks in three subjects-----------
Marks of first subject: 65
Marks of second subject: 70
Marks of third subject: 72
-----Displaying total marks and percentage of the student-------
Total marks of the subject 207
Percentage of the student 69.00
**********************************************************************
1.b) Develop a program to read the name and year of birth of a person. Display whether the person
is a senior citizen or not.
Output
****************************************************************
Enter the name Enter the name
Jyothy Jyothy
Enter the year of birth Enter the year of birth
1990 1940
Jyothy Not a Senior citizen Jyothy is a Senior citizen
2.a) Develop a program to generate Fibonacci sequence of length (N). Read N from the console.
Output
****************************************************************
Enter length of Fibonacci sequence 10
0 1 1 2 3 5 8 13 21 34
2.b) Write a function to calculate factorial of a number. Develop a
program to compute binomial coefficient (Given N and R).
def find_factorial(number):
factorial=1
if(number<0):
print("Negative number cannot possible")
return
for i in range(1,number+1):
factorial=factorial*i
return factorial
Output
****************************************************************
import math
# Calculate mean
mean = sum(numbers) / n
# Calculate variance
variance = sum((x - mean) ** 2 for x in numbers) / n
# Print results
print("Numbers:", numbers)
print("Mean:", mean)
print("Variance:", variance)
print("Standard Deviation:", std_deviation)
Output
****************************************************************
Enter the number of elements: 5
Enter element 1: 3
Enter element 2: 6
Enter element 3: 9
Enter element 4: 12
Enter element 5: 15
Numbers: [3.0, 6.0, 9.0, 12.0, 15.0]
Mean: 9.0
Variance: 18.0
Standard Deviation: 4.242640687119285
4. Read a multi-digit number (as chars) from the console. Develop a
program to print the frequency of each digit with suitable message.
Output
****************************************************************
enter the number342111
6
1 digit 3 times
2 digit 1 times
3 digit 1 times
4 digit 1 times
5. Develop a program to print 10 most frequently appearing words in a
text file.[Hint: Use dictionary with distinct words and their
frequency of occurrences. Sort the dictionary in the reverse order of
frequency and display dictionary slice of first 10 items]
ifile=open("C:/Users/win-7/Documents/jit.txt")
dict_words={}
for line in ifile:
words=line.split()
for word in words:
dict_words[word]=dict_words.get(word,0)+1
list_words=[]
for key,val in dict_words.items():
list_words.append((val,key))
list_words.sort(reverse=True)
print("the slice of first 10 items of sorted dictionary are")
print(list_words[0:10])
Output
****************************************************************
jit.txt
The and and or operators always take two Boolean values (or
expressions),so considered binary operators. The and operator
evaluates an expression to True if both Boolean values are True;
otherwise, it evaluates to False.
****************************************************************
ifile=open("C:/Users/win-7/Documents/jit.txt")
ofile=open("C:/Users/win7/Documents/jit1.txt",mode=’w’)
word_list=[]
for line in ifile:
words=line.split()
for word in words:
word_list.append(word)
word_list.sort()
print(word_list)
for word in word_list:
ofile.write(word +" ")
ofile.close()
Output
****************************************************************
Jit8.txt
def DivExp(a,b):
try:
assert a>0
if b==0:
raise ZeroDivisionError
c=a/b
return c
except ZeroDivisionError:
print("zero division error")
except AssertionError:
print("Assertion failure: number is negative ")
a=int(input("enter a value"))
b=int(input("enter b value"))
result=DivExp(a,b)
print(result)
Output
****************************************************************
Enter a value 10
Enter b value 5
2.0
Enter a value -1
Enter b value 5
Assertion failure: number is negative
None
Enter a value 5
Enter b value 0
Zero division error
None
10.Develop a program that uses class Student which prompts the user to
enter marks in three subjects and calculates total marks, percentage
and displays the score card details.[Hint: Use list to store the marks
in three subjects and total marks.Use __init__() method to initialize
name, USN and the lists to store marks and total, Use getMarks()
method to read marks into the list, and display() method to display
the score card details.]
class Student:
def __init__(self, name, usn):
self.name = name
self.usn = usn
self.marks = []
self.total_marks = 0
def getMarks(self):
for i in range(3):
mark = float(input("Enter marks for subject {}:".format(i+1)))
self.marks.append(mark)
self.total_marks += mark
def display(self):
print("Name:",self.name)
print("USN:",self.usn)
print("Marks:",self.marks)
print("Total Marks:",self.total_marks)
percentage=(self.total_marks / 3)
print("Percentage:",percentage)
# Main program
name = input("Enter student's name: ")
usn = input("Enter student's USN: ")
****************************************************************
Enter student's name: jyothy
Enter student's USN: 1bg11cs421
Enter marks for subject 1:88
Enter marks for subject 2:56
Enter marks for subject 3:77
Name: Jyothy
USN: 1bg11cs421
Marks: [88.0, 56.0, 77.0]
Total Marks: 221.0
Percentage: 73.66666666666667
import os,sys,zipfile
print(os.getcwd())
foldername=input("enter the folder name")
if(os.path.exists(os.getcwd()+"//"+foldername)):
zipfilename=input("enter the zip filenmae .zip")
zf=zipfile.ZipFile(zipfilename,"w")
for dirname,subdirs,files in os.walk(foldername):
zf.write(dirname)
for filename in files:
zf.write(os.path.join(dirname,filename))
zf.close()
if(os.path.exists(os.getcwd()+"//"+zipfilename)):
print("zip file created sucessfully")
else:
print("zip not creates")
else:
print("file does not exists")
sys.exit(0)
Output
****************************************************************
enter the folder name:test
enter the zip filenmae .zip:test1.zip
zip file created successfully
9. Define a function which takes TWO objects representing complex numbers and returns
new complex number with a addition of two complex numbers. Define a suitable class
‘Complex’ to represent the complex number. Develop a program to read N (N >=2)
complex numbers and to compute the addition of N complex numbers.
import sys
class Complex:
def __init__(self, real, imaginary):
self.real = real
self.imaginary = imaginary
if N < 2:
print("Please enter a valid value for N (N >= 2).")
sys.exit("error")
complex_numbers = []
for i in range(N):
print(f"Enter complex number {i+1}:")
complex_number = read_complex()
complex_numbers.append(complex_number)
Output
****************************************************************