T21L - Python Programming Laboratory Journal MCA - Semester-II

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 29

T21L– Python Programming

Laboratory Journal

MCA - Semester-II

Name: Mohammad Faizan Ayazuddin

Roll No.: 071


Academic Year
2021-2022
INDEX

Sr.no. Lab Question Date


1 Write a program to find sum of n given numbers 19-5-22

2 Write a program to solve and print following series 20-5-22


xyn/n!+x2yn-1/(n-1)!+x3yn-2/(n-2)!....xny

3 Write a program to print all Armstrong numbers within a 23-5-22


given range

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

5 Write a program to find out area circumference of circle using 6-6-22


lambda function

6 Write a program to find out factorial of given numbers using 10-6-22


recursive function.

7 Write a program to create class student to store roll number 13-6-22


name and address with demo of constructor and
destructor
8 Write a program to create quadrilateral class 14-6-22
(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)

9 Create class rectangle(l,b) and circle(r) and derive class 14-6-22


cylinder(r,h) to calculate area (multiple inheritance)

10 Write a program to find following pattern from the string 16-6-22


“Sky is only Limit of the Programmer in Year 2022”
1. All capital alphabets
2. All digits
3. All small alphabets
4. All lower case between “m-“ to “s”

11 Write a program to handle divide by zero exception using 23-6-22


else and finally block
12 Write a program to create table of 3 and 7 using thread and 28-6-22
print it

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

Signature of Faculty Incharge

Internal Examiner :

External Examiner :

Date :
Q1. Write a program to find sum of n given numbers

# INPUT: n = input("Enter Number to


calculate sum")
n = int (n)
sum = 0 for num in
range(0, n+1, 1):

sum = sum+num
print("SUM of first ", n, "numbers is: ", sum )

# OUTPUT:

Enter Number to calculate sum


7
SUM of first 7 numbers is: 28
Q 2. Write a program to solve
and print following series
xyn/n!+x2yn-1/(n-1)!+x3yn-2/(n-
2)!....xny

# INPUT:

n=int(input("Enter n")) x=int(input("Enter


x"))
y=int(input("Enter y"))
sum=1 t=n for i in
range(1,n+1):
fact=1 for j in
range(1,t+1):
fact=fact*j sum=sum+
(((x**i)*(y**t))/fact)
print("x**",i,"*y**",t,"/",fact,end='+=')
t=t-1
print(sum)

# 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:

r1=int(input("Enter first range:"))


r2=int(input("Enter second range:"))
print("Armstrong numbers=",end=':') for
n in range(r1,r2):
sum=0
p=len(str(n)) n1=n
while(n1>0):
d=n1%10 sum=sum+
(d**p)
n1=int(n1/10)
if(n==sum):
print(n,end=':')
# OUTPUT:

Enter first range:1


Enter second range:200
Armstrong numbers=: 1:2:3: 4:5:6: 7:8:9:153:
Enter first range:100
Enter second range:2000
Armstrong numbers=:153:370:371:407:1634:

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:

set1 = {"a", "b", "c"}


set2 = {1, 2, 3} set3 =
set1.union(set2)
print(set3)

# OUTPUT:
{3, 'c', 'a', 'b', 1, 2}

2.. intersection update:


# INPUT:

x = {"apple", "banana", "cherry"} y

= {"google", "Microsoft", "apple"}


x.intersection_update(y)
print(x)

# OUTPUT:
{'apple'}
# 3. Intersection:
# INPUT:

x = {"apple", "banana", "cherry"} y


= {"google", "microsoft", "apple"} z

= x.intersection(y) print(z)

# OUTPUT:

{'apple'}
# 4. substraction
# INPUT:

A = {10, 20, 30, 40, 80} B


= {100, 30, 80, 40, 60}
print (A.difference(B))
print (B.difference(A))

OR
# INPUT:

A = {10, 20, 30, 40, 80}


B = {100, 30, 80, 40, 60}
print (A - B) print (B - A)

# OUTPUT:
{10, 20}
{100, 60}

# 5.simentric diffrence:
# INPUT:

x = {"apple", "banana", "cherry"} y

= {"google", "microsoft", "apple"} z


= x.symmetric_difference(y)

print(z)

# OUTPUT:
{'google', 'banana', 'microsoft', 'cherry'}

# 6.also perform compare


# INPUT:

list1 = [11, 12, 13, 14, 15, 89]


list2 = [12, 13, 11, 15, 14,16]
a = set(list1) b = set(list2)

if a == b:
print("The list1 and list2 are equal")
else:

print("The list1 and list2 are not equal")

# OUTPUT:
The list1 and list2 are not equal
Q5.
Write a program to find out area circumference of circle using lambda function

# INPUT:

PI = 3.14 r = int(input("Please enter the value of radius of a


circle: ")) area = PI*r*r Circumference = 2*PI*r

print("Area of circle: ", area) print("Circumference


of circle: ", Circumference)

# 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:

print("factorial does not exist negative number") elif


m == 0:

print("the factorial of 0 is 1") else:


print("factorial of",m,"is",fact(m))

# 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

# INPUT: class student: def


init (self,d="",a=0,c=""):
print("paramerized constructor")
self.n=d
self.r=a
self.ad=c def
get(self):

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)

# INPUT: class quadrilateral: def


init (self,side1,side2,side3,side4):

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)

class rectangle(quadrilateral): def

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()

class square(rectangle): def

init (self,s1,s2,l,w,side1,side2,side3,side4):
rectangle. init (self,l,w,side1,side2,side3,side4)

#quadrilateral. init (self,side1,side2,side3,side4)


self.s1=s1
self.s2=s2

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

Perimeter of quadrilateral is: 46

>>>'''
Q9. Create class rectangle(l,b) and circle(r) and derive class cylinder(r,h) to calculate area
(multiple inheritance)

# INPUT:

class rectangle: def


init (self ,l,b):
self.l=l
self.b=b def
display(self):

self.area=self.l*self.b
print("Lenght:",self.l)
print("breadth:",self.b) print("Area of
rectangle is :",self.area)

class circle: def


init (self ,r):

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

def display2(self): self.display()


self.display1() print("Area of
cylinder is:",self.a)

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

Area of circle is : 1256.0


'''
10. Write a program to find following pattern from the string “Sky is only Limit of the
Programmer in Year 2022”

1. All capital alphabets


2. All digits
3. All small alphabets
4. All lower case between “m-“ to “s”

# INPUT:

import re txt="Sky is only Limit of the Programmer in


Year 2022" x=re.findall("[A-Z]",txt) print("All Capital
letters=",x) x=re.findall("[0-9]",txt) print("All
digits=",x) x=re.findall("[a-z]",txt) print("All small
letters=",x) x=re.findall("[m-s]",txt) print("All lower
case letters between m to s=",x)

# OUTPUT:

All Capital letters= ['S', 'L', 'P', 'Y']


All digits= ['2', '0', '2', '2']

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:

print("divide by zero exception") else:


print("No error") finally:
print("Executed finally block")

# 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:

import threading def


table(num): for i in
range(1,11):

print(num,"*",i,"=",num*i)

if name == " main ": # creating


thread t1 = threading.Thread(target=table,
args=(3,)) t2 = threading.Thread(target=table,
args=(7,))

# starting thread 1
t1.start()

# starting thread 2
t2.start()

# wait until thread 1 is completely executed


t1.join()

# wait until thread 2 is completely executed


t2.join()

# both threads completely executed


print("Done!")

# 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

Character Types Total Count

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

for i in range(len(str)): if ((ord(str[i])>=ord('A')) and


(ord(str[i])<=ord('Z'))): A=A+1
elif(ord(str[i])>=ord('a')) and (ord(str[i])<=ord('z')):

a=a+1 elif(ord(str[i])>=ord('0')) and


(ord(str[i])<=ord('9')):
d=d+1
else:
sc=sc+1

print(" Report of file ")


print("*******************************")
print("Character Type Total Count")
print("*******************************")
print("Capital Letter ",A)

print("Small Letter ",a)


print("special Characters ",sc)
print("Digits: ",d)
print("*******************************")
fp.close()

# 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=[]

n=int(input("Array length")) for


i in range(n):

n1=int(input("Array item of a"))


list1.append(n1)
n2=int(input("Array item of b"))
list2.append(n2)
a=np.array(list1)
b=np.array(list2)

ch=1 while ch<6:


print("1:Addition")
print("2:Subtraction")

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"]

print("the list of collection is ",db.list_collection_names())


#insert documnet into collection
#doc1=collection1.insert_one({"Rno":101,"name":"Rohini","address":"pune"})
#doc2=collection1.insert_one({"_id":5,"Rno":101,"name":"shab","address":"pune"})
#doc2=collection1.insert_one({"_id":11,"Rno":111,"name":"abhi","address":"satara"})
#doc3=collection1.insert_many([{"_id":6,"Rno":102,"name":"shubham","address":"goa"},{"
_id":10,"Rno":103,"name":"nipul","address":"maldives"}])
#d1=collection1.find_one()
#print("first document is:",d1) #for
d2 in collection1.find():
# print("document is:",d2)
#for d21 in collection1.find({},{"Rno":0}):
# print("document is:",d21)
#delete all document in collection
d3=collection1.delete_one({"Rno":103,"name":"nipul"})
print("deleted", d3.deleted_count) #update documents
in collection crit={"address":"satara"}
update_with={"$set":{"address":"mumbai"}}
d5=collection1.update_one(crit,update_with)

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'}
‘’’

You might also like