Python lab record_CSE_IT (1) (1)12
Python lab record_CSE_IT (1) (1)12
For
MVSREC Page : 1
B.E-I/IV, SEM-2 Python Programming Lab
LIST OF EXPERIMENTS
2 Display two random numbers to add, and ask the user to guess the 5
correct answer.
3 Read a date and check whether the date is valid or not, if it is valid 6
print incremented date.
4 Read x,y and print all prime numbers between x and y where x<=y 8
5 Accept Three Digits and Print all Possible Combinations from the 9
Digits
9 Read a List of Words and Return the Length of the Longest One 15
12 Store some elements in the dictionary and remove a given key from 19
the dictionary
13 To display which Letters are in the First String but not in the 21
Second
16 Create a class and compute the Area and the Perimeter of the Circle 26
MVSREC Page : 2
B.E-I/IV, SEM-2 Python Programming Lab
24 Read .csv file to print the statistical summary of each attribute and 37
visualize the data
MVSREC Page : 3
B.E-I/IV, SEM-2 Python Programming Lab
1. Read a set of numbers from the command line, add & print those numbers
Solution:
Output:
MVSREC Page : 4
B.E-I/IV, SEM-2 Python Programming Lab
python3 cla.py 1 2 3 4 5
2. Display two random numbers that are to be added, the program should allow the
student to enter the answer. if the answer is correct, a message of congratulations
should be displayed, if the answer is wrong the correct answer should be displayed.
Solution:
Output:
MVSREC Page : 5
B.E-I/IV, SEM-2 Python Programming Lab
3. Read a date and check whether the date is valid or not, if it is valid print
incremented date.
Note: For this program, observe that comments are indicated in red color
Solution:
date=input("Enter date in dd/mm/yyyy format:\n")
count=date.count("/")
#check if the given date has exactly 2 slash symbols
if(count!=2):
print("Invalid date format,enter date as dd/mm/yyyy")
exit(0)
a,b,c=date.split('/')
dd=int(a)
mm=int(b)
yy=int(c)
MVSREC Page : 6
B.E-I/IV, SEM-2 Python Programming Lab
MVSREC Page : 7
B.E-I/IV, SEM-2 Python Programming Lab
4. Read x,y and print all prime numbers between x and y where x<=y
Solution:
Output:
MVSREC Page : 8
B.E-I/IV, SEM-2 Python Programming Lab
5. Accept Three Digits and Print all Possible Combinations from the Digits.
Solution:
Output:
MVSREC Page : 9
B.E-I/IV, SEM-2 Python Programming Lab
MVSREC Page : 10
B.E-I/IV, SEM-2 Python Programming Lab
Output:
MVSREC Page : 11
B.E-I/IV, SEM-2 Python Programming Lab
B) Armstrong Number:
Solution:
Output:
MVSREC Page : 12
B.E-I/IV, SEM-2 Python Programming Lab
C) Strong Number:
Solution:
Output:
MVSREC Page : 13
B.E-I/IV, SEM-2 Python Programming Lab
7. Compute a Polynomial Equation given that the Coefficients of the Polynomial are
stored in a List
Solution:
Output:
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “polynomial.py”
III) Open terminal and run the below command to see the output
“python3 polynomial.py”
8. Search the Number of Times a Particular Number Occurs in a List
Solution:
MVSREC Page: 14
B.E-I/IV, SEM-2 Python Programming Lab
Output:
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “numbercount.py”
III) Open terminal and run the below command to see the output
“python3 numbercount.py”
9. Read a List of Words and Return the Length of the Longest One
Solution:
MVSREC Page: 15
B.E-I/IV, SEM-2 Python Programming Lab
Output:
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “longestword.py”
III) Open terminal and run the below command to see the output
“python3 longestword.py”
MVSREC Page: 16
B.E-I/IV, SEM-2 Python Programming Lab
10. Remove the ith Occurrence of the Given Word in a List where Words can Repeat
Solution:
import sys
mylist=[]
num=int(input("Enter how many words to read?"))
for k in range(num):
item=input("Enter a word:")
mylist.append(item)
print(mylist)
word=input("Enter word:")
i=int(input("Enter occurence value:"))
if word not in mylist:
print("Entered word doesn't exist in list")
sys.exit(0)
count=0
index=0
flag=0
size=len(mylist)
for k in range(0,size):
if mylist[k]==word:
count+=1
if count==i:
flag=1
break
if flag:
mylist.pop(k)
else:
print("Given ocurence value is more than repeated word")
print("List after removal of ",word,"is:",mylist)
Output:
MVSREC Page: 17
B.E-I/IV, SEM-2 Python Programming Lab
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “wordoccurence.py”
III) Open terminal and run the below command to see the output
“python3 wordoccurence.py”
MVSREC Page: 18
B.E-I/IV, SEM-2 Python Programming Lab
11. Count the number of alphabets, consonants, vowels, digits, special characters in a
sentence
Solution:
data=input("Enter a sentence:")
print("Given sentence=",data)
vc=['a','e','i','o','u','A','E','I','O','U']
d=0
sp=0
a=0
v=0
c=0
for k in data:
if k.isdigit():
d+=1
elif k.isalpha():
a+=1
if k in vc:
v+=1
else:
c+=1
else:
sp+=1
print("Digits=",d)
print("Alphabets=",a)
print("Vowels=",v)
print("Consonants=",c)
print("Special characters=",sp)
Output:
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “sentencecount.py”
III) Open terminal and run the below command to see the output
“python3 sentencecount.py”
12. Store some elements in the dictionary and remove a given key from the dictionary.
MVSREC Page: 19
B.E-I/IV, SEM-2 Python Programming Lab
Solution:
Output:
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “dictdemo.py”
MVSREC Page: 20
B.E-I/IV, SEM-2 Python Programming Lab
III) Open terminal and run the below command to see the output
“python3 dictdemo.py”
13. To display which Letters are in the First String but not in the Second
Solution:
Output:
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “letters.py”
III) Open terminal and run the below command to see the output
“python3 letters.py”
MVSREC Page: 21
B.E-I/IV, SEM-2 Python Programming Lab
Output:
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “gcd.py”
III) Open terminal and run the below command to see the output
“python3 gcd.py”
B. Factorial
Solution:
MVSREC Page: 22
B.E-I/IV, SEM-2 Python Programming Lab
Output:
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “factorial.py”
III) Open terminal and run the below command to see the output
“python3 factorial.py”
C. Fibonacci series
Solution:
Output:
MVSREC Page: 23
B.E-I/IV, SEM-2 Python Programming Lab
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “fib.py”
III) Open terminal and run the below command to see the output
“python3 fib.py”
Output:
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “gcdrec.py”
III) Open terminal and run the below command to see the output
“python3 gcdrec.py”
B. Factorial recursion
MVSREC Page: 24
B.E-I/IV, SEM-2 Python Programming Lab
Solution:
Output:
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “factrec.py”
III) Open terminal and run the below command to see the output
“python3 factrec.py”
C. Fibonacci recursion
Solution:
Output:
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
MVSREC Page: 25
B.E-I/IV, SEM-2 Python Programming Lab
16. Create a class and compute the Area and the Perimeter of the Circle.
Solution:
Output:
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “area.py”
III) Open terminal and run the below command to see the output
“python3 area.py”
17. Creating a class “employee” with fields name, id, designation, salary. Initialize N
employees and print details of N employees. Use self and __init__() method.
Solution:
class employee:
def __init__(self,n,i,d,s):
self.name=n
MVSREC Page: 26
B.E-I/IV, SEM-2 Python Programming Lab
self.id=i
self.designation=d
self.salary=s
def display(self):
print("Name=",self.name)
print("Id=",self.id)
print("Designation=",self.designation)
print("Salary=",self.salary)
mylist=[]
num=int(input("Enter number of employees:"))
for k in range(num):
print("Enter employee",k+1," details:")
n=input("Enter name:")
i=int(input("Enter id:"))
d=input("Enter designation:")
s=int(input("Enter salary:"))
mylist.append(employee(n,i,d,s))
print("Details of",num," Employees are:")
for k in range(num):
print("Employee",k+1," details\n========")
mylist[k].display()
Output:
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “employeedemo.py”
III) Open terminal and run the below command to see the output
“python3 employeedemo.py”
MVSREC Page: 27
B.E-I/IV, SEM-2 Python Programming Lab
Output:
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “singleinh.py”
III) Open terminal and run the below command to see the output
“python3 singleinh.py”
B. Multi-level inheritance
Solution:
MVSREC Page: 28
B.E-I/IV, SEM-2 Python Programming Lab
Output:
Procedure To Run:
IV) Open “vi editor” or IDLE and type the program
V) Save the file as “multilevelinh.py”
VI) Open terminal and run the below command to see the output
“python3 multilevelinh.py”
MVSREC Page: 29
B.E-I/IV, SEM-2 Python Programming Lab
Output:
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “opoverload.py”
III) Open terminal and run the below command to see the output
“python3 opoverload.py”
B. Method overloading
Solution:
MVSREC Page: 30
B.E-I/IV, SEM-2 Python Programming Lab
Output:
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “overloading.py”
III) Open terminal and run the below command to see the output
“python3 overloading.py”
C. Method overriding
Solution:
MVSREC Page: 31
B.E-I/IV, SEM-2 Python Programming Lab
Output:
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “overriding.py”
III) Open terminal and run the below command to see the output
“python3 overriding.py”
MVSREC Page: 32
B.E-I/IV, SEM-2 Python Programming Lab
Output:
Output-2
Output-3
MVSREC Page: 33
B.E-I/IV, SEM-2 Python Programming Lab
Output-3
Output-4
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “exceptiondemo.py”
III) Open terminal and run the below command to see the output
“python3 exceptiondemo.py”
MVSREC Page: 34
B.E-I/IV, SEM-2 Python Programming Lab
Output-1:
Output -2:
Output- 3:
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “custom.py”
III) Open terminal and run the below command to see the output
“python3 custom.py”
MVSREC Page: 35
B.E-I/IV, SEM-2 Python Programming Lab
Output:
Procedure To Run:
I) Create a text file using any text editor like “vi editor” or “mousepad”
II) Add some data and Save file as “demo.txt”
III) Open “vi editor” or IDLE and type the program
IV) Save the file as “filedemo.py”
V) Open terminal and run the below command to see the output
“python3 filedemo.py”
Note: Save both files ie. demo.txt and filedemo.txt in same location
MVSREC Page: 36
B.E-I/IV, SEM-2 Python Programming Lab
Output:
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “gui.py”
III) Open terminal and run the below command to see the output
“python3 gui.py”
24. Read .csv file to print the statistical summary of each attribute and visualize the
data.
Solution:
MVSREC Page: 37
B.E-I/IV, SEM-2 Python Programming Lab
Output:
MVSREC Page: 38
B.E-I/IV, SEM-2 Python Programming Lab
25. numpy program to compute sum of all elements, sum of each column and sum of
each row of a given array.
Solution:
MVSREC Page: 39
B.E-I/IV, SEM-2 Python Programming Lab
Output:
Procedure To Run:
I) Open “vi editor” or IDLE and type the program
II) Save the file as “numpydemo.py”
III) Open terminal and run the below command to see the output
“python3 numpydemo.py”
MVSREC Page: 40