Prcatical File - Aditi Mahale XII C
Prcatical File - Aditi Mahale XII C
2|Page
INDEX
3|Page
7 Program to create a line chart using NumPy 19-20
4|Page
18 Program, to create a module length conversion.py 51-52
that stores function for various length conversion, i.e.,
mile to km, inch to ft, etc. It should store constant
values and help() should display proper information
5|Page
Program 1
Source code:
No1=1
No2=2
print(‘’,+No1)
print(‘\n’,+No2)
x=1
while (x<=10):
No3=No1+No2
No1=No2
No2=No3
print(‘\n’,+No3)
x=x+1
6|Page
Output:
7|Page
Program 2
Source code:
var=True
while var:
choice=int(input(“Press – 1 to find the ordinal value of a character \n
Press – 2 to find a character of a value \n”))
if choice==1:
character=input(“Enter a character: ”)
print(ord(character))
elif choice==2:
value=int(input(“Enter an integer: ”))
print(chr(value))
else:
print(“You have entered wrong choice”)
8|Page
Output:
Program 3
9|Page
Source code:
# create a dictionary whose keys are months and values are number
of days in the corresponding month
Program 4
11 | P a g e
Source code:
# initialize sum
sum = 0
12 | P a g e
Output:
Program 5
13 | P a g e
Source code:
import matplotlib.pyplot as pl
con = [23.4,17.8,25,34,40]
zones = [‘East’, ‘West’, ‘North’, ‘South’, ‘Central’]
ex = [0,0,0.2,0,0]
col = [‘r’, ‘b’, ‘g’, ‘y’, ‘p’]
pl.axis(“equal”)
pl.title(“Zones”)
14 | P a g e
Output:
Program 6
15 | P a g e
Source code:
import numpy as np
import matplotlib.pyplot as plt
16 | P a g e
plt.bar(r1, bar1, colour = 'r', width = barwidth, edgecolor = 'white',
label = 'var1')
plt.bar(r2, bar2, colour = 'y', width = barwidth, edgecolor = 'white',
label = 'var2')
plt.bar(r3, bar3, colour = 'b', width = barwidth, edgecolor = 'white',
label = 'var2')
17 | P a g e
Output:
Program 7
18 | P a g e
Source code:
import matplotlib.pyplot as pl
import numpy as np
pl.show()
19 | P a g e
Output:
Program 8
20 | P a g e
Source code:
import csv
def readcsv():
with open(‘data1.csv’, ‘r’) as f:
data = csv.reader(f) #reader function to generate a
reader object
for row in data:
print(row)
def appendcsv():
with open(‘data1.csv’, ‘a’) as file:
append = csv.writer(file, delimiter = ‘,’)
#write new record in file
append.writerow([321, “Aman”, “Arts”])
file.close()
21 | P a g e
appendcsv()
else:
print(“Invalid value”)
Output:
Program 9
22 | P a g e
Source code:
import pickle
class Student:
def __init__(self, rno, name =""):
self.rollNo = rno
self.name = name
self.marks1, self.marks2, self.marks3 = 0.0 ,0.0 ,0.0
def readMarks(self):
print("Enter marks of three subjects")
self.marks1 = float(input("Subject1: "))
self.marks2 = float(input("Subject2: "))
self.marks3 = float(input("Subject3: "))
def display(self):
print("Student details are: ")
print("Roll number: ", self.rollNo)
print("Name: ", self.name)
23 | P a g e
print("Marks in 3 subjects: ", self.marks1, self.marks2,self.marks3)
file1=open("Student.log","wb")
pickle.dump(stud1 ,file1)
pickle.dump(stud2, file1)
file1.close()
Output:
24 | P a g e
Program 10
Source code:
25 | P a g e
#program to read instances of class Student from a file student.log
import pickle
class Student:
def __init__(self,rno = 0, name = ""):
self.rollNo = rno
self.name = name
self.marks1, self.marks2, self.marks3 = 0, 0, 0
def readMarks(self):
print("Enter marks of three subjects: ")
self.marks1 = float(input("Subject1: "))
self.marks2 = float(input("Subject2: "))
self.marks3 = float(input("Subject3: "))
def display(self):
print("Student details are: ")
print("Roll number: ", self.rollNo)
print("Name: ", self.name)
print("Marks in 3 subjects: ", self.marks1, self.marks2, self.marks3)
26 | P a g e
stud1 = Student()
file1 = open("Student.log","rb")
try:
while True:
stud1 = pickle.load(file1)
stud1.display()
except EOFError:
file1.close()
Output:
27 | P a g e
Program 11
Source code:
28 | P a g e
# program to reads a file containing integers sorted in ascending
order and then asks the user to enter integers to search for
mid = (high+low)//2
#if element is smaller than mid, then it can only be present in left sub
array
elif arr[mid] > x:
return binary_search (arr,low,mid-1,x)
29 | P a g e
return binary_search (arr,mid+1,high,x)
else:
#element is not present in the array
return -1
#input array
arr = [2,3,1,10,40,54,67,86]
x = int(input("Enter the searched element: "))
Output:
30 | P a g e
Program 12
Source code:
31 | P a g e
#program to execute bubble sort function
#bubble sort function
def bubble_sort(lst):
Output:
32 | P a g e
Program 13
Source code:
33 | P a g e
#program to implement all stack operations
def isEmpty(stack):
if stack==[]:
return True
else:
return False
def peek(stack):
if isEmpty(stack): #calling function isEmpty
return "Underflow"
else:
top=len(stack)-1
return stack[top]
35 | P a g e
print("4.Peek")
print("5.EXIT")
choice=int(input("Enter your choice: "))
if (choice==1):
value=int(input("Enter value: "))
push(stack,value) #calling push function
if (choice==2):
item=pop(stack) #calling pop function
if item=="Underflow":
print("Underflow! Stack is empty")
else:
print("Popped item is", item)
if (choice==3):
display(stack) #calling display function
if (choice==4):
item=peek(stack) #calling peek function
if item=="Underflow":
print("Underflow! Stack is empty")
else:
print("Topmost item is: ", item)
36 | P a g e
if (choice==5):
print("You have selected to close this program")
Output:
37 | P a g e
Program 14
38 | P a g e
Source code:
def isEmpty(Qu):
if Qu==[]:
return True
else:
return False
39 | P a g e
if len(Qu)==0:
front=rear=None
return item
40 | P a g e
print(Qu[rear],"<-rear")
#main program
queue=[] #intially queue is empty
front=None
while True:
print("*****Queue Menu*****")
print("1.Add element")
print("2.Delete element")
print("3.Peek")
print("4.Display queue")
print("EXIT")
ch=int(input("Enter your choice"))
if ch==1:
item=int(input("Enter value: "))
Enqueue(queue,item)
input("Press Enter to continue...")
elif ch==2:
item=Dequeue(queue)
if item=="Underflow":
print("Underflow! Queue is empty")
41 | P a g e
else:
print("Dequeued item is: ",item)
input("Press Enter to continue...")
elif ch==3:
item=Peek(queue)
if item=="Underflow":
print("Queue is empty")
else:
print("Foremost item is: ",item)
input("Press Enter to continue...")
elif ch==4:
Display(queue)
input("Press Enter to continue...")
elif ch==5:
break
else:
print("Invalid choice")
input("Press Enter to continue...")
Output:
42 | P a g e
43 | P a g e
44 | P a g e
Program 15
Source code:
#function to take two numbers and returns the number that has
minimum one's digit
def min(x , y ) :
a = x % 10
b = y % 10
if a < b :
return x
else :
return y
#main program
first = int(input("Enter first number = "))
second = int(input("Enter second number = "))
45 | P a g e
Output:
46 | P a g e
Program 16
Source code:
#main program
num=int(input("Enter an octal number: "))
oct2others(num)
47 | P a g e
Output:
48 | P a g e
Program 17
Source code:
#main program
ini=int(input("Enter initial value of the AP series: "))
st=int(input("Enter step value of the AP series: "))
print("Series with initial value", ini, "& step value", st, "goes as: ")
t1,t2,t3,t4 = retSeries(ini,st)
print(t1,t2,t3,t4)
49 | P a g e
Output:
50 | P a g e
Program 18
Source code:
#lenghtconversion.py
"""This module stores various conversion functions to convert
distances into different lengths"""
def miletokm(d):
"""Returns miles converted to kilometres"""
return d*one_mile
def kmtomile(d):
"""Returns kilometres converted to miles"""
return d/one_mile
def feettoinches(ln):
"""Returns feet converted to inches"""
return ln*one_feet
def inchestofeet(ln):
"""Returns inches converted to feet"""
return ln/one_feet
#constant
one_mile=1.6093
one_feet=12
51 | P a g e
#main program
import lenghtconversion as pl
help(pl)
Output:
52 | P a g e
Program 19
Source code:
def encrypt(sttr,enkey):
return enkey.join(sttr)
def decrypt(sttr,enkey):
return sttr.split(enkey)
#main program
mainString=input("Enter main string: ")
encryptStr=input("Enter encryption key: ")
enStr=encrypt(mainString,encryptStr)
deLst=decrypt(enStr,encryptStr)
deStr="".join(deLst)
53 | P a g e
print("The encrypted string is: ", enStr)
print("String after decryption is: ", deStr)
Output:
54 | P a g e
Program 20
Source code:
#main program
import shape
x=int(input("Input radius: "))
print("Area of circle is: ")
print(shape.areacircle(x))
55 | P a g e
r=int(input("Enter radius: "))
h=int(input("Enter height: "))
print("Area of cylinder is: ")
print(shape.areacylinder(r,h))
Output:
56 | P a g e
Program 21
Source code:
Output:
57 | P a g e
Program 22
Source code:
mycursor = mydb.cursor()
mycursor.executemany(endata,val)
mydb.commit()
58 | P a g e
Output:
59 | P a g e
Program 23
Source code:
mycursor = mydb.cursor()
c = mycursor.rowcount
print("Total number of rows: ",c)
60 | P a g e
Output:
61 | P a g e
Program 24
Source code:
mydb.commit()
mydb.close()
62 | P a g e
Output:
63 | P a g e
Program 25
Source code:
mydb.commit()
mydb.close()
64 | P a g e
Output:
65 | P a g e