PYTHON_LAB_MANUAL[1]
PYTHON_LAB_MANUAL[1]
def linear_search(a,n,key):
for i in range(0,n):
if(a[i]==key):
return i
return -1
a=[1,3,5,4,7,9]
print(a)
key=int(input("Enter the number to search:"))
n=len(a)
res=linear_search(a,n,key)
if(res==-1):
print(f"Elements not found")
else:
print("Elements found at index:",res)
7)Create a calculator program
def calc(n1,n2,op):
if op=='+':
r=n1+n2
elif op=='-':
r=n1-n2
elif op=='*':
r=n1*n2
elif op=='/':
r=n1/n2
elif op=='^':
r=n1**n2
else:
print("Invalid operator")
return r
n1=float(input("Enter 1st number:"))
n2=float(input("Enter 2nd number:"))
op=input("Enter the operator(+,-,*,/,^)")
r=calc(n1,n2,op)
print(f"{n1} {op} {n2}={r}")
text='hello'
print("converted string to uppercase:")
print(text.upper())
def selec(a,size):
for i in range(size):
min=i
for j in range(i+1,size):
if a[j]<a[min]:
min=j
a[i],a[min]=a[min],a[i]
a=[-2,45,0,11,-9]
print(f"before sorting {a}")
size=len(a)
selec(a,size)
print(f"after sorting {a}")
10)Implement stack
stack=[]
size=3
def display():
print("stack elements are")
for item in stack:
print(item)
def push(item):
print(f"push on item to stack:{item}")
if len (stack)<size:
stack.append(item)
else:
print("stack is full")
def popitem():
if len(stack)>0:
print(f"pop an item from stack:{stack.pop()}")
else:
print("stack is empty")
push(10)
push(20)
push(30)
display()
push(40)
popitem()
display()
popitem()
popitem()
popitem()
file.write("hello there\n")
file.writelines(l)
file.close()
file.seek(0)
print("out of readline function is")
print(file.readline())
file.seek(0)
print("output of readlines function is")
print(file.readlines())
PART B
pswd=input("enter password:")
res=validate_password(pswd)
if res:
print("valid password")
else:
print("Invalid password")
3)Demonstrate use of List
list=[10,40,20,50,30]
print("first element:",list[0])
print("fourth element:",list[3])
print("list element from 0 to 2 index:", list[0:3])
list.append(20)
print('list after append:',list)
print("index of 20",list.index(20))
list.sort()
print("list after sorting",list)
print("popped element is:",list.pop())
print("list after pop",list)
list.insert(2,50)
print("list after insert:",list)
print("number of occurence of 50:",list.count(5))
list.remove(10)
print("list after removing 10:",list)
list.reverse()
print("list after reversing:",list)
boxoffice={}
for i in range(0,4):
key=input("enter key:")
value=input("enter value:")
boxoffice[key]=value
print(boxoffice)
print("length of dictionary:",len(boxoffice))
boxoffice["avatar"]=2009
print("dictionary after modification:",boxoffice)
print("titanic is present:","titanic" in boxoffice)
print("titanic was released in the year:",boxoffice.get("titanic",1997))
print("dictionary keys are:",boxoffice.keys())
print("dictionary values are:",boxoffice.values())
print("pop item from dictionary",boxoffice.popitem())
boxoffice.setdefault("harrypotter",2011)
print("after setting harrypotter:",boxoffice)
boxoffice.update({"frozen":2013})
print("after inserting new item",boxoffice)
self.lbl1.place(x=100, y=50)
self.t1.place(x=200, y=50)
self.lbl2.place(x=100, y=100)
self.t2.place(x=200, y=100)
self.b1.place(x=100, y=150)
self.b2.place(x=200, y=150)
self.lbl3.place(x=100, y=200)
self.t3.place(x=200, y=200)
def add(self):
self.t3.delete(0, 'end')
num1=int(self.t1.get())
num2=int(self.t2.get())
result=num1+num2
self.t3.insert(END, str(result))
def sub(self, event):
self.t3.delete(0, 'end')
num1=int(self.t1.get())
num2=int(self.t2.get())
result=num1-num2
self.t3.insert(END, str(result))
window=Tk()
mywin=MyWindow(window)
window.title('Hello Python')
window.geometry("400x300+10+10")
window.mainloop()
Histogram
import matplotlib.pyplot as plt
marks=[65,50,34,90,26,97,56,67,45,95]
grade=[0,35,70,100]
plt.xticks([0,35,70,100])
plt.hist(marks,grade,facecolor="g")
plt.xlabel("Percentage")
plt.ylabel("No.of Students")
Pie chart
from matplotlib import pyplot as plt
import numpy as np
cars = ['AUDI', 'BMW', 'FORD','TESLA', 'JAGUAR', 'MERCEDES']
data = [23, 17, 35, 29, 12, 41]
fig = plt.figure(figsize=(10, 7))
plt.pie(data, labels=cars)
plt.show()
import numpy as np
a = np.array(42)
b = np.array([1, 2, 3, 4, 5,6])
c = np.array([[1, 2, 3,4,5], [6,7,8,9,10]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[10, 20, 30], [40, 50, 60]]])
print("Dimension of array a",a.ndim)
print("Dimension of array d",d.ndim)
print("No of elements in each dimension of array c",c.shape)
print("Different types of arrays")
print(a)
print(b)
print(c)
print(d)
e=np.arange(2, 10)
print(e)
x = b.copy()
y = b.view()
print(x.base)
print(y.base)
print("b array after reshaping")
newarr = b.reshape(2, 3)
print(newarr)