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

Python lab record_CSE_IT (1) (1)12

Uploaded by

245123742004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Python lab record_CSE_IT (1) (1)12

Uploaded by

245123742004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

B.

E-I/IV, SEM-2 Python Programming Lab

MATURI VENKATA SUBBA RAO (MVSR) ENGINEERING COLLEGE


NADERGUL, HYDERABAD-501510
(Sponsored by Matrusri Education Society, Estd.1980)
An Autonomous Institution
Approved by AICTE & Affiliated to Osmania University, Estd.1981
ISO 9001:2015 Certified Institution, Accredited by NAAC
website: www.mvsrec.edu.in

LAB PROGRAMS & SOLUTIONS

For

PROGRAMMING FOR PROBLEM SOLVING USING


PYTHON LAB
Subject Code: U21ESN83CS
Class: B.E- First Year, Sem-2
Branch: CSE, IT
Acad.Year: 2021-22 Even Semester

MVSREC Page : 1
B.E-I/IV, SEM-2 Python Programming Lab

LIST OF EXPERIMENTS

Sl.no Name of the Program Page no.

1 Reading and adding set of numbers from command line 4

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

6 Check for “amicable” numbers, armstrong number & strong 11


number

7 Compute a Polynomial Equation given that the Coefficients of the 14


Polynomial are stored in a List

8 Search the Number of Times a Particular Number Occurs in a List 14

9 Read a List of Words and Return the Length of the Longest One 15

10 Remove the ith Occurrence of the Given Word in a List where 17


Words can Repeat

11 Count the number of alphabets, consonants, vowels, digits, special 19


characters in a sentence

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

14 Write a function to compute gcd, factorial, fibonacci series 21

15 Write a recursive function to compute gcd, factorial, fibonacci 24


series

16 Create a class and compute the Area and the Perimeter of the Circle 26

17 Creating a class “employee” with fields name, id, designation, 26


salary. Initialize N employees and print details of N employees. Use

MVSREC Page : 2
B.E-I/IV, SEM-2 Python Programming Lab

self and __init__() method.

18 Demonstrate on single-level and multi-level inheritance. 28

19 Demonstrate on operator overloading, method overloading, method 29


overriding

20 Demonstrate on predefined multiple exceptions 32

21 Demonstrate on custom exceptions 34

22 Read the Contents of a File in Reverse Order 36

23 GUI program to convert celsius temperature to fahrenheit 36

24 Read .csv file to print the statistical summary of each attribute and 37
visualize the data

25 numpy program to compute sum of all elements, sum of each 39


column and sum of each row of a given array.

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:

Procedure to run program in Linux:


1. Write the program in vi editor
2. Save the file as cla.py
3. Run the below command to get 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:

Procedure to run program in Linux:


1. Open terminal and Write the program in vi editor
2. Save the file as randomdemo.py
3. Run the below command to get output:
python3 randomdemo.py

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)

b1=(mm==1 or mm==3 or mm==5 or mm==7 or mm==8 or mm==10 or


mm==12)
b2=(mm==4 or mm==6 or mm==9 or mm==11)
#check for date format
# setting the total no. of days for 12 months
if(b1==True) :
maxdays=31
elif b2==True:
maxdays=30
elif mm==2:
# check for leap year to set feb days
if (yy%4==0 and yy%100!=0 or yy%400==0):
maxdays=29
else :
maxdays=28

if(dd<1 or dd>31 or mm<1 or mm>12 or yy<0):


print("Invalid date")
# increment days and months if months<12 and days=maxdays
elif (dd==maxdays and mm<12):
dd=1
mm+=1
print("Incremented date=",dd,"/",mm,"/",yy)
# update days and months to 1 if months=12 and
days=maxdays
elif (dd==maxdays and mm==12):
dd=1
mm=1
yy+=1
print("Incremented date=",dd,"/",mm,"/",yy)
# check for leap year if days exceeds 29 in feb month

MVSREC Page : 6
B.E-I/IV, SEM-2 Python Programming Lab

elif dd>29 and mm==2:


print("Invalid date")
# check for non leap year if days exceeds 28 in feb month
elif dd>maxdays and mm==2 :
print("Invalid date")
#check if days exceeds 30 for months maxdays 30
elif dd>maxdays and (b2):
print("Invalid date")
else :
dd+=1
print("Incremented date=",dd,"/",mm,"/",yy)
Output:

Procedure to run program in Linux:


1. Open terminal and Write the program in vi editor
2. Save the file as dateincrement.py

MVSREC Page : 7
B.E-I/IV, SEM-2 Python Programming Lab

3. Run the below command to get output:


python3 dateincrement.py

4. Read x,y and print all prime numbers between x and y where x<=y

Solution:

Output:

Procedure to run program in Linux:


1. Open terminal and Write the program in vi editor
2. Save the file as prime.py
3. Run the below command to get output:
python3 prime.py

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

Procedure to run program in Linux:


1. Open terminal and Write the program in vi editor
2.Save the file as digitcombination.py
3.Run the below command to get output:
python3 digitcombination.py

MVSREC Page : 10
B.E-I/IV, SEM-2 Python Programming Lab

6. Check for “amicable” numbers, armstrong number & strong number


A) Amicable Number:
Solution:

Output:

Procedure to run program in Linux:


1. Open terminal and Write the program in vi editor
2. Save the file as amicable.py
3. Run the below command to get output:
python3 amicable.py

MVSREC Page : 11
B.E-I/IV, SEM-2 Python Programming Lab

B) Armstrong Number:
Solution:

Output:

Procedure to run program in Linux:


1. Open terminal and Write the program in vi editor
2. Save the file as armstrong.py
3. Run the below command to get output:
python3 armstrong.py

MVSREC Page : 12
B.E-I/IV, SEM-2 Python Programming Lab

C) Strong Number:
Solution:

Output:

Procedure to run program in Linux:


1. Open terminal and Write the program in vi editor
2. Save the file as strongnumber.py
3. Run the below command to get output:
python3 strongnumber.py

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”

14. Write a function to compute gcd, factorial, fibonacci series


A. GCD
Solution:

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”

15. Write a recursive function to compute gcd, factorial, fibonacci series


A. GCD Recursion
Solution:

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

II) Save the file as “fibrec.py”


III) Open terminal and run the below command to see the output
“python3 fibrec.py”

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

18. Demonstrate on single-level and multi-level inheritance.


A. Single-level inheritance
Solution:

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”

19. Demonstrate on operator overloading, method overloading, method overriding


A. Operator overloading
Solution:

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”

20. Demonstrate on predefined multiple exceptions


Solution:

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”

21. Demonstrate on custom exceptions


Solution:

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

22. Read the Contents of a File in Reverse Order


Solution:

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

23. GUI program to convert celsius temperature to Fahrenheit


Solution:

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:

Procedure To Run in Linux:


I) Open LibreOffice Calc application and enter some data as shown below
Example: Storing Weather report of 10 days

MVSREC Page: 38
B.E-I/IV, SEM-2 Python Programming Lab

II) Save the file as stats.csv


III) Open “vi editor” or IDLE and type the program
IV) Save the file as “pandademo.py”
V) Open terminal and run the below command to see the output
“python3 pandademo.py”
Procedure To Run in Windows:
I) Open Microsoft Excel application and enter some data as shown below
Example: Storing Weather report of 10 days

II) Save the file as stats.csv


III) Open “notepad” or IDLE and type the program
IV) Save the file as “pandademo.py”
V) Open terminal and run the below command to see the output
“python3 pandademo.py”

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

You might also like