Python Manual
Python Manual
ENGINEERING COLLEGE
(Doddakallsandra, off. Kanakapura Road, Bangalore – 560061)
NAAC ACCREDITED
Affiliated to Visvesvaraya Technological University, Belagavi
Approved by AICTE, New Delhi
LAB MANUAL
For
NAME:
USN:
SEMESTER & SECTION:
MISSION
M1- To encourage Knowledge Acquisition and Foster Innovation & Research.
VISION:
To contribute to Global Development by producing Knowledgeable and Quality professionals
who are Innovative and Successful in advanced field of Computer Science & Engineering to
adapt the changing Employment demands and social needs.
MISSION:
M1: To provide Quality Education for students, to build Confidence by developing their
Technical Skills to make them Competitive Computer Science Engineers.
M2: To facilitate Innovation & Research for students and faculty and to provide Internship
opportunities
M3: To Collaborate with educational institutions and industries for Excellence in Teaching
and Research.
print("=========================")
OUTPUT1:
Enter the name of the student : Mark
Enter the USN of the student : 22sas
Enter marks in Subject 1 : 52
Enter marks in Subject 2 : 45
Enter marks in Subject 3 : 78
Student Details
=========================
Name : Mark
USN : 22sas
Marks 1 : 52
Marks 2 : 45
Marks 3 : 78
Total : 175
Percent : 58.333333333333336
Student Mark has passed
=========================
OUTPUT2:
Enter the name of the student : James
Enter the USN of the student : 22SBS
Enter marks in Subject 1 : 25
Enter marks in Subject 2 : 32
Enter marks in Subject 3 : 33
Student Details
=========================
Name : James
USN : 22SBS
Marks 1 : 25
Marks 2 : 32
Marks 3 : 33
Total : 90
Percent : 30.0
student James has failed
=========================
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.
OUTPUT1:
Enter the name of the person : MARK
Enter his year of birth : 1961
MARK aged 62 years is a Senior Citizen.
OUTPUT2:
Enter the name of the person : SCOTT
Enter his year of birth : 2014
SCOTT aged 9 years is not a Senior Citizen.
2. a. Develop a program to generate Fibonacci sequence of length (N). Read N from the
console.
OUTPUT:
Enter N? 5
Fibonacci sequence:
0
1
1
2
3
2.b. Write a function to calculate factorial of a number. Develop a program to compute
binomial coefficient (Given N and R).
OUTPUT:
Enter a number N: 5
Enter value of K: 2
The factorial of 5 is 120
Value of C(5,2) is (10)
3. Read N numbers from the console and create a list. Develop a program to print mean,
variance and standard deviation with suitable messages.
import numpy as np
lst = []
# number of elements as input
n = int(input("Enter number of elements : "))
# iterating till the range
for i in range(0, n):
ele = int(input())
lst.append(ele) # adding the element
print("list elements are",lst)
print("mean is" ,np.average(lst))
print("variance is" ,np.var(lst))
print("standars deviation is",np.std(lst))
OUTPUT:
Enter number of elements : 5
1
2
3
4
5
list elements are [1, 2, 3, 4, 5]
mean is 3.0
variance is 2.0
standars deviation is 1.4142135623730951
4. Read a multi-digit number (as chars) from the console. Develop a program to print the
frequency of each digit with suitable message.
def char_frequency(str1):
dict = {}
for n in str1:
keys = dict.keys()
if n in keys:
dict[n] += 1
else:
dict[n] = 1
return dict
str1=input("Enter the multi-digit number: ")
print(char_frequency(str1))
OUTPUT:
Enter the multi-digit number: 55667888
{'5': 2, '6': 2, '7': 1, '8': 3}
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]
text = open("sample.txt", "r")
def sorting(filename):
infile = open(filename)
words = []
for line in infile:
temp = line.split()
for i in temp:
words.append(i)
infile.close()
words.sort()
outfile = open("result.txt", "w")
for i in words:
outfile.writelines(i)
outfile.writelines(" ")
outfile.close()
sorting("sample.txt")
7. Develop a program to backing Up a given Folder (Folder in a current working directory) into
a ZIPFile by using relevant modules and suitable methods.
def main():
# path to folder which needs to be zipped
directory = './python_files'
def DivExp(a,b):
try:
c=a/b
print("Quotient:",c)
except ZeroDivisionError:
print("Division by Zero!")
a=int(input("Enter the value of a:"))
b=int(input("Enter the value of b:"))
result=(DivExp(a,b))
OUTPUT:
Enter the value of a:5
Enter the value of b:0
Division by Zero!
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.
class Complex:
# Constructor to accept
# real and imaginary part
def __init__(self, tempReal, tempImaginary):
self.real = tempReal;
self.imaginary = tempImaginary;
# Driver code
if __name__=='__main__':
OUTPUT:
Complex number 1 : 3 + i2
Complex number 2 : 9 + i5
Sum of complex number : 12 + i7
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
def getStudentDetails(self):
self.usn=input("Enter USN : ")
self.name = input("Enter Name : ")
self.subject1 =int(input("Enter subject1 marks : "))
self.subject2 = int(input("Enter subject2 Marks : "))
self.subject3 = int(input("Enter subject3 Marks : "))
def printResult(self):
self.total=(int)(self.subject1 + self.subject2 + self.subject3)
self.percentage = (int)( (self.subject1 + self.subject2 + self.subject3) / 300 * 100 );
print("USN Name total percentage")
print(self.usn,self.name,self.total,self.percentage)
if self.percentage<35:
print("student",self.name , "has failed")
else:
print("Student",self.name, "has passed")
S1=Student()
S1.getStudentDetails()
print("Result : ")
S1.printResult()
OUTPUT1:
Enter USN : 12
Enter Name : Mark
Enter subject1 marks : 87
Enter subject2 Marks : 66
Enter subject3 Marks : 55
Result :
USN Name total percentage
12 Mark 208 69
Student Mark has passed
OUTPUT2:
Enter USN : 11
Enter Name : James
Enter subject1 marks : 23
Enter subject2 Marks : 11
Enter subject3 Marks : 15
Result :
USN Name total percentage
11 James 49 16
student James has failed