PYTHON RECORD Pvpsit
PYTHON RECORD Pvpsit
OUTPUT :
Enter the principle amount: 1000
Enter the time in years: 1
Enter the rate: 1
Simple interest is : 10
Compound interest is : 10
13.AIM : Write a menu driven program to add , subtract, multiply and divide two integers using
functions
PROGRAM :
def add(a,b) :
print("Addition : ", a+b)
def sub(a,b) :
print("Subtraction : ", a-b)
def mul(a,b) :
print("Multiply : ", a*b)
def div(a,b) :
print("Division : ", a//b)
a=int(input("Enter a : "))
b=int(input("Enter b : "))
n=int(input("Enter your choice : "))
while(n!=-1) :
if(n==1) :
add(a,b)
elif(n==2) :
sub(a,b)
elif(n==3) :
mul(a,b)
elif(n==4) :
div(a,b)
n=int(input("Enter your choice : "))
OUTPUT :
Enter a : 5
Enter b : 10
Enter your choice : 1
Addition : 15
Enter your choice : 2
Subtraction : -5
Enter your choice : 3
Multiply : 50
Enter your choice : 4
Division : 0
Enter your choice : -1
14.AIM : Write a program that passes lambda function as an argument to another function to
compute the cube of a number.
PROGRAM :
c = lambda x : x**3
a=int(input("Enter a : "))
print("Cube of number : ", c(a))
OUTPUT :
Enter a : 3
Cube of number : 27
OUTPUT :
Enter temperature in Celsius : 98
Temperature in Fahrenheit : 208.4
17.AIM : Write a function that accepts three integers and returns true if they are sorted else it returns
false.
PROGRAM :
def sortedOrNot(a,b,c) :
if(a<b and b<c) :
return "true"
else :
return "false"
a=int(input("Enter a : "))
b=int(input("Enter b : "))
c=int(input("Enter c : "))
if(sortedOrNot(a,b,c)=="true") :
print(a,b,c,"are sorted")
else:
print(a,b,c,"are not sorted")
OUTPUT :
Enter a : 1
Enter b : 6
Enter c : 3
1 6 3 are not sorted
18.AIM : Write a program that accepts a list from user and print the alternate element of the list
PROGRAM :
l1=[]
n=int(input("Enter no of elements : "))
print("Enter elements : ")
for i in range(0,n) :
x=int(input())
l1.append(x)
print("Alternate elements are : ")
for j in range(0,n) :
if(j%2!=0) :
print(l1[j])
OUTPUT :
Enter no of elements : 5
Enter elements :
1
2
3
4
5
Alternate elements are :
2
4
19.AIM : Find and display the largest number of a list without builtin function max()
PROGRAM :
l1 = []
n=int(input("Enter no of elements : "))
print("Enter elements : ")
for i in range(0,n) :
x=int(input())
l1.append(x)
max=l1[0]
for i in range(1,n) :
if(l1[i]>max) :
max=l1[i]
print("Greatest Element of list is : ", max)
OUTPUT :
Enter no of elements : 5
Enter elements :
1
2
3
4
5
Greatest Element of list is : 5
20.AIM : Write a program that rotates the element of a list so that the element at the first index
moves to the second index, the element in the second index moves to the third index, etc., and the
element in the last index moves to the first index.
PROGRAM :
l1 = []
n=int(input("Enter no of elements : "))
print("Enter elements : ")
for i in range(0,n) :
x=int(input())
l1.append(x)
i=n-1
x=l1[n-1]
while(i>0) :
l1[i]=l1[i-1]
i=i-1
l1[0]=x
print("Elements after rearranging : ")
print(l1)
OUTPUT :
Enter no of elements : 5
Enter elements :
3
7
6
11
9
Elements after rearranging :
[9, 3, 7, 6, 11]
27.AIM : Write a program with class name rectangle constructed by a length and width and a
method which will compute the area of a rectangle
PROGRAM :
class Rectangle :
def __init__(self,length,width) :
self.length = length
self.width = width
def area(self) :
return self.length*self.width
l = int(input("Enter length of rectangle : "))
b = int(input("Enter width of rectangle : "))
a=Rectangle(l,b)
print(a.area())
OUTPUT :
Enter length of rectangle : 10
Enter width of rectangle : 12
120
28.AIM : Write a program to find the three elements that sum to zero from a set of n real numbers
PROGRAM :
class SumTOZero :
def sum(a,b,c) :
if(a+b+c == 0) :
return 1
else :
return 0
l = [1,-3,2,4,-5,3,-6,8,0,9]
n=len(l)
for i in range(0,n-2) :
for k in range(i+1, n-1) :
for j in range(k+1, n) :
x = SumTOZero.sum(l[i],l[k],l[j])
if(x==1) :
print(l[i],l[k],l[j])
OUTPUT :
1 -3 2
1 4 -5
-3 -5 8
-3 3 0
-3 -6 9
2 4 -6
2 -5 3
29.AIM : Write a program tp convert an integer into a roman numeral and write a python class to
convert an integer to a roman numeral.
PROGRAM :
#integer to roman
class roman:
def int_to_Roman(self, num):
val = [
1000, 900, 500, 400,
100, 90, 50, 40,
10, 9, 5, 4,
1
]
sym = [
OUTPUT :
MMMM
#roman to numeral
class solution:
def roman_to_int(self, s):
rom_val = {'I':1, 'V': 5, 'X': 10, 'L':50, 'C':100, 'D':500, 'M':1000}
int_val = 0
for i in range(len(s)):
if i > 0 and rom_val[s[i]] > rom_val[s[i - 1]]:
int_val += rom_val[s[i]] - 2 * rom_val[s[i - 1]]
else:
int_val += rom_val[s[i]]
return int_val
print(solution().roman_to_int(‘MMMM’)
OUTPUT :
4000
30.AIM : Write a program with class named Circle constructed by a radius and two methods which
will compute the area and the perimeter of a circle.
PROGRAM :
class Circle :
def __init__(self,r):
self.r=r
def perimeter(self) :
return 2*3.14*r
def area(self) :
return 3.14*r*r
r = int(input("Enter radius of Cirlcle : "))
o = Circle(r)
print("Perimeter of Circle : ", o.perimeter())
print("area of Circle : ", o.area())
OUTPUT :
Enter radius of Cirlcle : 5
Perimeter of Circle : 31.400000000000002
area of Circle : 78.5
[2, 3, 5, 7, 8]
3
36.AIM : Write a program to implement Linear Search
PROGRAM :
def LinearSearch(l1,n,key) :
for i in range(0,n) :
if(l1[i]==key) :
return i
return -1
l1 = []
n=int(input("Enter no of elements : "))
print("Enter elements : ")
for i in range(0,n) :
x=int(input())
l1.append(x)
key = int(input("Enter element to search : "))
flag = LinearSearch(l1,n,key)
if(flag==-1) :
print("Element not found")
else :
print("Element fount at index ", flag)
OUTPUT :
Enter no of elements : 5
Enter elements :
10
25
36
48
69
Enter element to search : 15
Element not found
Enter elements :
10
25
36
48
15
Enter element to search : 36
Element fount at index 2
t=r=self.head
for i in range(1,pos) :
t=r
r = r.nextnode
t.nextnode = r.nextnode
print("Deleted element is : ", r.data)
def display(self) :
r=self.head
while(r!=None) :
print(r.data)
r=r.nextnode
s=SLL(None)
op = int(input("1.insertion_front\n2.insertion_end\n3.insertion_middle\n4.deletion\n5.display\n-
1.exit\nEnter your choice : "))
while(op!=-1) :
if(op==1) :
x=int(input("Enter the element to insert : "))
s.insertion_front(x)
if(op==2) :
x=int(input("Enter the element to insert : "))
s.insertion_end(x)
if(op==3) :
x=int(input("Enter the element to insert : "))
pos=int(input("Enter the position of element to insert : "))
s.insertion_middle(x,pos)
if(op==4) :
pos=int(input("Enter the position of element to delete : "))
s.deletion(pos)
if(op==5) :
s.display()
op = int(input("Enter your choice : "))
OUTPUT :
1.insertion_front
2.insertion_end
3.insertion_middle
4.deletion
5.display
-1.exit
Enter your choice : 1
Enter the element to insert : 10
Enter your choice : 1
Enter the element to insert : 20
Enter your choice : 2
Enter the element to insert : 50
Enter your choice : 3
Enter the element to insert : 80
Enter the position of element to insert : 4
Enter your choice : 5
20
10
50
80
Enter your choice : -1
self.prevnode = prevnode
self.nextnode = nextnode
class DLL() :
def __init__(self,head,tail) :
self.head=head
self.tail=tail
def insertion_front(self,x) :
p=node(x,None,self.head)
if(self.head==None) :
self.tail=self.head=p
else :
self.head.prevnode=p
self.head=p
def insertion_end(self,x) :
p=node(x,self.tail,None)
if(self.head==None) :
self.tail=self.head=p
else :
self.tail.nextnode=p
self.tail=p
def insertion_middle(self,x,pos) :
if(pos==1) :
self.insertion_front(x)
elif(pos==self.count()+1) :
self.insertion_end(x)
else :
t=r=self.head
for i in range(1,pos) :
t=r
r = r.nextnode
p=node(x,t,r)
t.nextnode = p
r.prevnode = p
def count(self) :
r=self.head
c=0
while(r!=None) :
c=c+1
r=r.nextnode
return c
def deletion_front(self) :
print("Deleted element is : ", self.head.data)
self.head=self.head.nextnode
self.head.prevnode=None
def deletion_end(self) :
print("Deleted element is : ", self.tail.data)
self.tail=self.tail.prevnode
self.tail.nextnode=None
def deletion(self,pos) :
if(self.head==None) :
print("Underflow\n")
elif(self.head==self.tail) :
print("Deleted element is : ", self.head.data)
self.head=self.tail=None
else :
if(pos==1) :
self.deletion_front()
elif(pos==self.count()) :
self.deletion_end()
else :
t=r=self.head
for i in range(1,pos) :
t=r
r = r.nextnode
t.nextnode = r.nextnode
r.nextnode.prevnode = t
print("Deleted element is : ", r.data)
def display(self) :
r=self.head
while(r!=None) :
print(r.data)
r=r.nextnode
s=DLL(None,None)
op = int(input("1.insertion_front\n2.insertion_end\n3.insertion_middle\n4.deletion\n5.display\n-1.exit\nEnter your choice
: "))
while(op!=-1) :
if(op==1) :
x=int(input("Enter the element to insert : "))
s.insertion_front(x)
if(op==2) :
x=int(input("Enter the element to insert : "))
s.insertion_end(x)
if(op==3) :
x=int(input("Enter the element to insert : "))
pos=int(input("Enter the position of element to insert : "))
s.insertion_middle(x,pos)
if(op==4) :
pos=int(input("Enter the position of element to delete : "))
s.deletion(pos)
if(op==5) :
s.display()
op = int(input("Enter your choice : "))
OUTPUT :
1.insertion_front
2.insertion_end
3.insertion_middle
4.deletion
5.display
-1.exit
Enter your choice : 1
Enter the element to insert : 10
Enter your choice : 1
Enter the element to insert : 20
Enter your choice : 2
Enter the element to insert : 30
Enter your choice : 3
Enter the element to insert : 40
Enter the position of element to insert : 2
Enter your choice : 5
20
40
10
30
Enter your choice : 4
Enter the position of element to delete : 3
Deleted element is : 10
Enter your choice : 5
20
40
30
r=r.next
r.next=self.head.next
self.head=temp.next
temp.next=None
def deletion_end(self):
if self.head is None:
print("Linked list is Empty")
elif self.head.next is self.head:
self.next=None
else:
temp=self.head
r=self.head.next
while r.next is not self.head:
r=r.next
temp=temp.next
temp.next=self.head
r.next=None
def deletion_middle(self,pos):
if self.head is None:
print("Linked list is Empty")
else:
temp=self.head.next
r=self.head
for i in range(1,pos-1):
temp=temp.next
r=r.next
r.next=temp.next
temp.next=None
def display(self):
temp=self.head
if self.head is None:
print("Linked List is Empty")
else:
while temp:
print(temp.data,'-->',end=" ")
if temp.next==self.head:
break
temp=temp.next
l=CLL()
while True:
print("\n----MENU----\n1.Insertion at beginning\n2.Insertion at end\n3.Insertion at middle\n4.Deletion at
front\n5.Deletion at end\n6.Deletion at middle\n7.Display\n8.Exit")
i=int(input("Enter your choice : "))
if i==1:
l.insertion_front(int(input("Enter element : ")))
elif i==2:
l.insertion_end(int(input("Enter element : ")))
elif i==3:
l.insertion_middle(int(input("Enter position : ")),int(input("Enter element : ")))
elif i==4:
l.deletion_front()
elif i==5:
l.deletion_end()
elif i==6:
l.deletion_middle(int(input("Enter position : ")))
elif i==7:
l.display()
elif i==8:
break
else:
print("INVALID ENTRY")
OUTPUT:
CLL OPERATIONS:
1.Insertion at beginning
2.Insertion at end
3.Insertion at middle
4.Deletion at front
5.Deletion at end
6.Deletion at middle
7.Display
8.Exit
Enter your choice : 1
Enter element : 10
CLL OPERATIONS:
1.Insertion at beginning
2.Insertion at end
3.Insertion at middle
4.Deletion at front
5.Deletion at end
6.Deletion at middle
7.Display
8.Exit
Enter your choice : 1
Enter element : 20
CLL OPERATIONS:
1.Insertion at beginning
2.Insertion at end
3.Insertion at middle
4.Deletion at front
5.Deletion at end
6.Deletion at middle
7.Display
8.Exit
Enter your choice : 1
Enter element : 30
CLL OPERATIONS:
1.Insertion at beginning
2.Insertion at end
3.Insertion at middle
4.Deletion at front
5.Deletion at end
6.Deletion at middle
7.Display
8.Exit
Enter your choice : 2
Enter element : 40
CLL OPERATIONS:
1.Insertion at beginning
2.Insertion at end
3.Insertion at middle
4.Deletion at front
5.Deletion at end
6.Deletion at middle
7.Display
8.Exit
Enter your choice : 3
Enter position : 2
Enter element : 50
CLL OPERATIONS:
1.Insertion at beginning
2.Insertion at end
3.Insertion at middle
4.Deletion at front
5.Deletion at end
6.Deletion at middle
7.Display
8.Exit
Enter your choice : 7
30 --> 50 --> 20 --> 10 --> 40 -->
CLL OPERATIONS:
1.Insertion at beginning
2.Insertion at end
3.Insertion at middle
4.Deletion at front
5.Deletion at end
6.Deletion at middle
7.Display
8.Exit
Enter your choice : 4
CLL OPERATIONS:
1.Insertion at beginning
2.Insertion at end
3.Insertion at middle
4.Deletion at front
5.Deletion at end
6.Deletion at middle
7.Display
8.Exit
Enter your choice : 6
Enter position : 3
CLL OPERATIONS:
1.Insertion at beginning
2.Insertion at end
3.Insertion at middle
4.Deletion at front
5.Deletion at end
6.Deletion at middle
7.Display
8.Exit
Enter your choice : 7
50 --> 20 -->
CLL OPERATIONS:
1.Insertion at beginning
2.Insertion at end
3.Insertion at middle
4.Deletion at front
5.Deletion at end
6.Deletion at middle
7.Display
8.Exit
Enter your choice : 8
self.preorder(t.nextnode)
def postorder(self,t) :
if(t):
self.postorder(t.prevnode)
self.postorder(t.nextnode)
print(t.data)
t=None
s=BST()
op = int(input("1.insertion\n2.deletion\n3.inorder\n4.preorder\n5.postorder\n-1.exit\nEnter your choice : "))
while(op!=-1) :
if(op==1) :
x=int(input("Enter the element to insert : "))
t=s.insertion(t,x)
root(t)
if(op==2) :
x=int(input("Enter the element to delete : "))
t=s.deletion(t,x)
root(t)
if(op==3) :
s.inorder(t)
if(op==4) :
s.preorder(t)
if(op==5) :
s.postorder(t)
op = int(input("Enter your choice : "))
OUTPUT :
1.insertion
2.deletion
3.inorder
4.preorder
5.postorder
-1.exit
Enter your choice : 1
Enter the element to insert : 30
Enter your choice : 1
Enter the element to insert : 25
Enter your choice : 1
Enter the element to insert : 40
Enter your choice : 1
Enter the element to insert : 20
Enter your choice : 1
Enter the element to insert : 50
Enter your choice : 3
20
25
30
40
50
Enter your choice : 4
30
25
20
40
50
Enter your choice : 5
20
25
50
40
30
2.SCATTER PLOTS:
import matplotlib.pyplot as plt
import pandas as pd
from google.colab import files
uploaded=files.upload()
import io
data=pd.read_csv(io.BytesIO(uploaded['salary.csv']))
x=data["exp"]
y=data["salary"]
plt.scatter(x,y)
plt.title("Scatterplot")
plt.xlabel("WorkingExperience(years)")
plt.ylabel("Annual wage(dollars)")
plt.show()
OUTPUT:
3.BAR CHARTS:
from sklearn import datasets
import matplotlib.pyplot as plt
iris=datasets.load_iris()
x_iris=iris.data
y_iris=iris.target
average=x_iris[y_iris==0].mean(axis=0)
plt.bar(iris.feature_names,average)
plt.title("Bar chart Setosa averages")
plt.ylabel("Avg in cm")
plt.show()
OUTPUT:
4.HISTOGRAMS:
from sklearn import datasets
import matplotlib.pyplot as plt
bins = 20
iris = datasets.load_iris()
X_iris = iris.data
X_sepal = X_iris[:, 0]
plt.hist(X_sepal, bins)
plt.title("Histogram Sepal Length")
plt.xlabel(iris.feature_names[0])
plt.ylabel("Frequency")
plt.show
OUTPUT:
5.BOXPLOTS:
from sklearn import datasets
import matplotlib.pyplot as plt
iris = datasets.load_iris()
X_iris = iris.data
X_sepal = X_iris[:, 0]
plt.boxplot(X_sepal, labels=[iris.feature_names[0]])
plt.title("Boxplot Sepal Length")
plt.ylabel("cm")
plt.show
OUTPUT:
<function matplotlib.pyplot.show>