T21L - Python Programming Laboratory Journal MCA - Semester-II
T21L - Python Programming Laboratory Journal MCA - Semester-II
T21L - Python Programming Laboratory Journal MCA - Semester-II
Laboratory Journal
MCA - Semester-II
4 Write a progrsam which create 2 user define set and perform 2-6-22
following opration on it
1.union
2.intersection_update
3.intersection
4.substraction
5.simentric diffrence
6.also perform compare
13 Write a program to read any text file and print report of total 5-7-22
characters as follows
File report of a.txt
Character Types
Total Count
Capital letters
22
Small letters
123
Digits
34
Special Characters
20
14 Write a menu driven (add subtract, multiply and division, exit) 14-7-22
program to handle two 1 D array
15 Write a Program for performing CRUD operation with 20-7-22
mongoDB and Python
Internal Examiner :
External Examiner :
Date :
Q1. Write a program to find sum of n given numbers
sum = sum+num
print("SUM of first ", n, "numbers is: ", sum )
# OUTPUT:
# INPUT:
# OUTPUT:
Enter n4
Enter x2 Enter
y2
x** 1 *y** 4 / 1+=x** 1 *y** 3 / 2+=x** 1 *y** 2 / 6+=x** 1 *y** 1 / 24+=42.5 Q3. Write a
program to print all Armstrong numbers within a given range
# INPUT:
Q4. write a program which create 2 users define set and perform following oration on it
1. union
2.intersection_update
3. intersection
4.substraction
5.simentric difference
6.also perform compare
1. union
# INPUT:
# OUTPUT:
{3, 'c', 'a', 'b', 1, 2}
# OUTPUT:
{'apple'}
# 3. Intersection:
# INPUT:
= x.intersection(y) print(z)
# OUTPUT:
{'apple'}
# 4. substraction
# INPUT:
OR
# INPUT:
# OUTPUT:
{10, 20}
{100, 60}
# 5.simentric diffrence:
# INPUT:
print(z)
# OUTPUT:
{'google', 'banana', 'microsoft', 'cherry'}
if a == b:
print("The list1 and list2 are equal")
else:
# OUTPUT:
The list1 and list2 are not equal
Q5.
Write a program to find out area circumference of circle using lambda function
# INPUT:
# OUTPUT:
a
Please enter the value of radius of a circle: 6
6
Area of circle: 113.03999999999999
Circumference of circle: 37.68
>
Q6.
Write a program to find out factorial of given numbers using recursive function.
# INPUT: def
fact(n): if
n == 1:
return n
else:
return n*fact(n-1)
m = int(input("Any number")) if
m<0:
# OUTPUT: Any
number 3
factorialof 3 is 6
Q7.
Write a program to create class student to store roll number name and address with
demo of constructor and destructor
self.n=input("Enter name")
self.r=int(input("Enter roll no"))
self.ad=input("Enter address") def
put(self):
print("Name:",self.n)
print("Roll no:",self.r)
print("Address:",self.ad) def
del (self):
print("Onject is deleted")
s=student()
s.get()
s.put()
s1=student("Shubham",77,"Pune")
s1.put()
'''
# OUTPUT:
paramerized constructor
Enter nameABC
Enter roll no 101
Enter addressMumbai
Name: Rohini
Roll no: 111 Address:
Mumbai paramerized
constructor Name:
Shubham
Roll no: 77
Address: Pune
'''
Q8. Write a program to create quadrilateral class (side1,side2,side3,side4) to find
perimeter derive rectangle class (side1 ,side2)from it to find area and perimeter Derive
square class (side1) from rectangle to find area and perimeter (multilevel inheritance)
self.side1=side1;
self.side2=side2; self.side3=side3;
self.side4=side4;
def display(self):
self.q=self.side1+self.side2+self.side3+self.side4
print("side1:",self.side1)
print("side2:",self.side2)
print("side3:",self.side3)
print("side4:",self.side4) print("Perimeter of
quadrilateral is:",self.q)
init (self,l,w,side1,side2,side3,side4):
quadrilateral. init (self,side1,side2,side3,side4)
self.l=l
self.w=w
def display1(self):
self.a=self.l*self.w
self.peri=2*(self.l*self.w)
print("Lenght:",self.l)
print("Width:",self.w) print("Area
of rectangle is: ",self.a)
print("Perimeter of rectangle is:
",self.peri)
self.display()
init (self,s1,s2,l,w,side1,side2,side3,side4):
rectangle. init (self,l,w,side1,side2,side3,side4)
def display2(self):
self.area=self.s1*self.s2
self.perimeter=4*(self.s1*self.s2)
print("side1:",self.s1) print("side2:",self.s2)
print("Area of square is: ",self.area)
print("Perimeter of =square is: ",self.perimeter)
self.display1()
s=square(20,2,7,8,6,4,29,7)
s.display2()
# OUTPUT:
side1: 20 side2:
2
Area of square is: 40
Perimeter of =square is: 160
Lenght: 7
Width: 8
Area of rectangle is: 56
Perimeter of rectangle is: 112
side1: 6 side2: 4 side3: 29
side4: 7
>>>'''
Q9. Create class rectangle(l,b) and circle(r) and derive class cylinder(r,h) to calculate area
(multiple inheritance)
# INPUT:
self.area=self.l*self.b
print("Lenght:",self.l)
print("breadth:",self.b) print("Area of
rectangle is :",self.area)
self.pi=3.14
self.r=r
def display1(self):
self.area1=self.pi*self.r*self.r
print("radius:",self.r) print("Area of
circle is :",self.area1)
class cylinder(rectangle,circle):
def init (self ,r,l,b,r1,h):
rectangle. init (self ,l,b)
circle. init (self ,r)
self.pi=3.14
self.r1=r1
self.h=h
self.a=2*(3.14)*r1*h+2*(3.14)*r1*r1
cy=cylinder(20,30,50,7,66) cy.display2()
# OUTPUT:
Area of cylinder is: 3209.08
Lenght: 30 breadth: 50
Area of rectangle is : 1500
radius: 20
# INPUT:
# OUTPUT:
All small letters= ['k', 'y', 'i', 's', 'o', 'n', 'l', 'y', 'i', 'm', 'i', 't', 'o', 'f', 't', 'h', 'e', 'r', 'o', 'g', 'r', 'a',
'm', 'm', 'e', 'r', 'i', 'n', 'e', 'a', 'r']
All lower case letters between m to s= ['s', 'o', 'n', 'm', 'o', 'r', 'o', 'r', 'm', 'm', 'r', 'n', 'r']
'''
11. Write a program to handle divide by zero exception using else and finally block
# INPUT:
try:
x=int(input("enter no 1"))
y=int(input("enter no 2"))
print("division=",x/y) except:
# OUTPUT:
enter no 15 enter no 20
divide by zero exception
Executed finally block
'''
12. Write a program to create table of 3 and 7 using thread and print it
# INPUT:
print(num,"*",i,"=",num*i)
# starting thread 1
t1.start()
# starting thread 2
t2.start()
# OUTPUT:
37 ** 11 == 37
37 ** 22 == 614
37 ** 33 == 921
37 ** 44 == 1228
37 ** 55 == 1535
37 ** 66 == 1842
37 ** 77 == 2149
37 ** 88 == 2456
37 ** 99 == 2763
37 ** 1010 == 3070
Done!
'''
Q 13. Write a program to read any text file and print report of total characters as follows
File report of a.txt
Capital letters 22
Small letters 123
Digits 34
Special Characters 20
# INPUT:
fn=input("Enter FileName:")
fp=open(fn,"r")
str=fp.read() A=0 a=0 sc=0
d=0
# OUTPUT:
Enter FileName:a.txt
Report of file
*******************************
Character Type Total Count
*******************************
Capital Letter 5
Small Letter 39
special Characters 12
Digits: 3
*******************************
'''
14. Write a menu driven(add subtract, multiply and division, exit) program to handle two 1
D array
# INPUT:
import numpy as np
def input1():
global a,b
list1=[]
list2=[]
print("3:Multiplication")
print("4:Divison") print("5:Input
array") print("6:Exit")
ch=int(input("Enter your choice"))
if ch==1:
c=a+b
print(a,b,c) elif
ch==2: c=a-b
print(a,b,c) elif
ch==3: c=a*b
print(a,b,c) elif
ch==4: c=a/b
print(a,b,c) elif
ch==5:
input1()
# OUTPUT:
1:Addition
2:Subtraction
3:Multiplication
4:Divison
5:Input array
6:Exit
Enter your choice5
Array length3
Array item of a1
Array item of b11
Array item of a2
Array item of b22
Array item of a3 Array item of b33
1:Addition
2:Subtraction
3:Multiplication
4:Divison
5:Input array
6:Exit
Enter your choice1
[1 2 3] [11 22 33] [12 24 36]
1:Addition
2:Subtraction
3:Multiplication
4:Divison
5:Input array
6:Exit
Enter your choice2
[1 2 3] [11 22 33] [-10 -20 -30]
1:Addition
2:Subtraction
3:Multiplication
4:Divison
5:Input array
6:Exit
Enter your choice6
'''
Q15. Write a Program for performing CRUD operation with MongoDB and Python
# INPUT:
#!/usr/bin/env python
# coding: utf-8
# In[1]: get_ipython().system('pip install
pymongo')
# In[7]:
#!/usr/bin/env python
# coding: utf-8
# In[1]: get_ipython().system('pip install
pymongo')
# In[47]:
import pymongo
db_client=pymongo.MongoClient("mongodb://localhost:27017")
db=db_client["SIBAR"] print("the list of database is
",db_client.list_database_names()) collection1=db["STUDENT"]
collection2=db["TEACHER"]
for d5 in collection1.find():
print("document is:",d5)
# OUTPUT:
Requirement already satisfied: pymongo in c:\users\admin\anaconda3\lib\site-packages (4.
1.1) the list of database is ['SIBAR', 'admin', 'config',
'local'] the list of collection is ['STUDENT']
deleted 0 document is: {'_id': 5, 'Rno': 101, 'name': 'shab', 'address':
'pune'} document is: {'_id': 11, 'Rno': 111, 'name': 'abhi', 'address':
'mumbai'} document is: {'_id': 6, 'Rno': 102, 'name': 'shubya',
'address': 'goa'}
‘’’