2nd SemPython LabManual-25
2nd SemPython LabManual-25
Lab Manual
For
I Semester B E/II Semester BE
Prepared by
Dr. Rajshekhar .M. Patil , Professor , Dept of ISE
Smt. Yamunashree. Asst. Professor , Dept of ISE
Smt. Yashodhara , Asst. Professor Dept of ISE
Kirankumar Asst. Professor , Dept of ISE
Mission:
M1. To impart quality education in order to meet the needs of industry and society.
PEO2: Graduates will work individually and in team for software project management.
PSO2: An ability to design, develop and implement algorithms for real time applications.
PSO3: An ability to develop mobile and web applications.
PSO4: An ability to inculcate professional skills, entrepreneurship skills, and work in teams for
developing engineering solutions for societal needs with ethical practices.
Program Outcomes (POs)
Engineering Graduates will be able to:
• Engineering knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex engineering problems.
• Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.
• Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities
with an understanding of the limitations.
• The Engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.
• Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms
of the engineering practice.
• Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
CO# Description
Demonstrate proficiency in handling loops and creation of functions.
CO1
CO2 Identify the methods to create and manipulate lists, tuples and dictionaries.
Programming Exercises:
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.
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.
3. Read N numbers from the console and create a list. Develop a program to print
mean, variance and standard deviation with suitable messages.
4. Read a multi-digit number (as chars) from the console. Develop a program to print
the frequency of each digit with suitable message.
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]
6. Develop a program to sort the contents of a text file and write the sorted contents
into a separate text file. [Hint: Use string methods strip(), len(), list methods sort(),
append(), and file methods open(), readlines(), and write()].
8. Write a function named DivExp which takes TWO parameters a, b and returns a
value c (c=a/b). Write suitable assertion for a>0 in function DivExp and raise an
exception for when b=0. Develop a suitable program which reads two values from the
console and calls a function DivExp.
9. Define a function which takes TWO objects representing complex numbers and
returns new complex number with an 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.
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.]
The weightage of Continuous Internal Evaluation (CIE) is 50% and for Semester End
Exam (SEE) is 50%. The minimum passing mark for the CIE is 40% of the maximum
marks (20 marks out of 50). The minimum passing mark for the SEE is 35% of the
maximum marks (18 marks out of 50). A student shall be deemed to have satisfied the
academic requirements and earned the credits allotted to each subject/ course if the
student secures not less than 35% (18 Marks out of 50) in the semester-
endexamination (SEE), and a minimum of 40% (40 marks out of 100) in the sum total
of the CIE (Continuous Internal Evaluation) and SEE (Semester End Examination)
taken together. Continuous Internal Evaluation (CIE):The CIE marks for the theory
component of the IC shall be 30 marks and for the laboratory component 20 Marks.
Three Tests each of 20 Marks; after the completion of the syllabus of 35-40%,
65-70%, and 90-100% respectively.
Two Assignments/two quizzes/ seminars/one field survey and report
presentation/one course project totaling 20 marks.
Total Marks scored (test + assignments) out of 80 shall be scaled down to 30 marks
The CIE marks awarded in the case of the Practical component shall be based on the
continuous evaluation of the laboratory report. Each experiment report can be
evaluated for 10 marks. Marks of all experiments‟ write-ups are added and scaled
down to 15 marks.
The laboratory test (duration 03 hours) at the end of the 15th week of the semester
/after completion of all the experiments (whichever is early) shall be conducted for 50
marks and scaled down to 05 marks.
Scaled-down marks of write-up evaluations and tests added will be CIE marks for the
laboratory component of IC/IPCC for 20 marks.
The minimum marks to be secured in CIE to appear for SEE shall be 12 (40% of
maximum marks) in the theory component and 08 (40% of maximum marks) in the
practical component. The laboratory component of the IC/IPCC shall be for CIE only.
However, in SEE, the questions from the laboratory component shall be included. The
maximum of 05 questions is to be set from the practical component of IC/IPCC, the
total marks of all questions should not be more than 25 marks. The theory component
of the IC shall be for both CIE and SEE.
SEE for IC
Theory SEE will be conducted by university as per the scheduled time table, with
common question papers for the course (duration 03 hours)
1. The question paper will have ten questions. Each question is set for 20 marks.
2. There will be 2 questions from each module. Each of the two questions under a
module (with a maximum of 3 sub-questions), should have a mix of topics under that
module.
3. The students have to answer 5 full questions, selecting one full question from each
module. The theory portion of the Integrated Course shall be for both CIE and SEE,
whereas the practical portion will have a CIE component only. Questions mentioned
in the SEE paper shall include questions from the practical component).
Passing standard:
The minimum marks to be secured in CIE to appear for SEE shall be 12 (40% of
maximum marks-30) in the theory component and 08 (40% of maximum marks -20)
in the practical component. The laboratory component of the IPCC shall be for CIE
only. However, in SEE, the questions from the laboratory component shall be
included. The maximum of 04/05 questions to be set from the practical component of
IPCC, the total marks of all questions should not be more than 30 marks.
SEE will be conducted for 100 marks and students shall secure 35% of the maximum
marks to qualify for the SEE. Marks secured will be scaled down to 50.
2. Allen B. Downey, “Think Python: How to Think Like a Computer Scientist”, 2nd
Edition, Green Tea Press, 2015. (Available under CC-BY-NC license at
http://greenteapress.com/thinkpython2/thinkpython2.pdf(Chapters 13, 15, 16, 17, 18)
(Download pdf/html files from the above link)
Programming Exercises:
1a. 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.
Name: Raju
USN: 1JS22CSE001
Marks 1: 67
Marks 2: 77
Marks 3: 99
Total: 243
Percent : 81.00
1b. Develop a program to read the name and year of birth of a person. Display whether the
person is a senior citizen or not.
perName = input("Enter the name of the person : ")
perDOB = int(input("Enter his year of birth : "))
curYear = int(input("Enter current year : "))
perAge = curYear - perDOB
if (perAge > 60):
print(perName, "aged", perAge, "years is a Senior Citizen.")
else:
print(perName, "aged", perAge, "years is not a Senior Citizen.")
OUTPUT:
Enter the name of the person : Rani
Enter his year of birth : 1947
Enter current year : 2023
Rani aged 76 years is a Senior Citizen.
2a. Develop a program to generate Fibonacci sequence of length (N). Read N from the console.
num = int(input("Enter the Fibonacci sequence length to be generated : "))
firstTerm = 0
secondTerm = 1
print("The Fibonacci series with", num, "terms is :")
print(firstTerm, secondTerm, end=" ")
for i in range(2,num):
curTerm = firstTerm + secondTerm
print(curTerm, end=" ")
firstTerm = secondTerm
secondTerm = curTerm
OUTPUT:
Enter the Fibonacci sequence length to be generated : 9
The Fibonacci series with 9 terms is :
0 1 1 2 3 5 8 13 21
2b. Write a function to calculate factorial of a number. Develop a program to compute binomial
co- efficient (Given N and R).
def fact(num):
if num == 0:
return 1
else:
return num * fact(num-1)
OUTPUT:
Enter the value of N : 5
Enter the value of R (R cannot be negative or greater than N): 3
5C3 = 10.0
3. Read N numbers from the console and create a list. Develop a program to print mean,
variance and standard deviation with suitable messages.
from math import sqrt
myList = []
num = int(input("Enter the number of elements in your list : "))
for i in range(num):
val = int(input("Enter the element : "))
myList.append(val)
print('The length of list1 is', len(myList))
print('List Contents', myList)
total = 0
for elem in myList:
total += elem
mean = total / num
total = 0
for elem in myList:
total += (elem - mean) * (elem - mean)
variance = total / num
stdDev = sqrt(variance)
print("Mean =", mean)
print("Variance =", variance)
print("Standard Deviation =", "%.2f"%stdDev)
OUTPUT:
Enter the number of elements in your list : 5
Enter the element : 12
Enter the element : 55
Enter the element : 34
Enter the element : 78
Enter the element : 11
The length of list1 is 5
List Contents [12, 55, 34, 78, 11]
Mean = 38.0
Variance = 662.0
Standard Deviation = 25.73
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:
The number entered is : 123343
2 occurs 1 times
4 occurs 1 times
1 occurs 1 times
3 occurs 3 times
5. Develop a program to print 10 most frequently appearing words in a text file. [Hint: Use
dictio- nary 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]
fname=input('Enter file directory/name: ')
c=[]
d={}
with open(fname,'r') as f:
content=f.read()
word=content.split()
for i in word:
c.append(i)
for i in c:
count=0
for j in c:
if i==j:
count+=1
d[i]=count
OUTPUT:
Enter file directory/name: SORT.TXT
[('1', 1), ('2', 1), ('3', 1), ('8', 1), ('5', 1), ('6', 1), ('7', 1)]
6. Develop a program to sort the contents of a text file and write the sorted contents into a
separate text file. [Hint: Use string methods strip(), len(), list methods sort(), append(), and file
methods open(), readlines(), and write()].
fname=input('Enter file name/directory: ')
with open(fname,'r') as f:
read=f.read()
word=read.split()
l=[]
for i in word:
l.append(i)
l.sort()
wname=input('Enter file name to create: ')
with open(wname,'w') as w:
for j in l:
w.write(j)
w.write('\n')
OUTPUT:
Enter file name/directory: sort.txt
Enter file name to create: sort1.txt
7. Develop a program to backing Up a given Folder (Folder in a current working directory) into
a ZIP File by using relevant modules and suitable methods.
import shutil
name=input('Enter zip file name: ')
directory=input('Enter file directory: ')
shutil.make_archive(name,'zip',directory)
OUTPUT:
Enter zip file name: SORT.TXT
Enter file directory: python 1st year
8. Write a function named DivExp which takes TWO parameters a, b and returns a value c
(c=a/b). Write a suitable assertion for a>0 in function DivExp and raise an exception for when
b=0. Develop a suitable program which reads two values from the console and calls a function
DivExp.
def DivExp(a,b):
if b==0:
print('Error. Integer Overflow')
else:
c=a/b
return c
print(DivExp(a,b))
OUTPUT:
def add(self):
print(self.a+self.b)
OUTPUT:
Enter number: 12
Enter first complex number: 12.364
Enter second complex number: 15.486
(27.85+0
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.subjects=[]
def enterMarks(self):
for i in range(3):
sub=input('Enter subject: ')
self.subjects.append(sub)
mark=int(input('Enter marks of %s in %s: '%(self.name,sub)))
self.marks.append(mark)
def total(self):
total=self.marks[0]+self.marks[1]+self.marks[2]
return total
def per(self):
per=(self.marks[0]+self.marks[1]+self.marks[2])/3
return per
def disp(self):
print(self.name,'USN:',self.usn,'got',self.marks,'in',self.subjects)
print('Total marks:',self.total())
print('Percentage:',self.per())
OUTPUT: