Python Lab manual program for BE First semester (all department
1. GE 8161 β Problem Solving and Python Programming Laboratory
Program:
print("Enter first Number:")
a=int(input())
print("Enter second number: ")
b=int(input())
rem=a%b
while rem!=0:
a=b
b=rem
rem=a%b
print("GCD of given number is:", b)
2. GE 8161 β Problem Solving and Python Programming Laboratory
Output:
Enter first Number:
5
Enter second number:
10
('GCDof given number is:', 5)
3. GE 8161 β Problem Solving and Python Programming Laboratory
Program:
def newtonSqrt(n):
approx = 0.5 * n
better = 0.5 * (approx + n/approx)
while better != approx:
approx = better
better = 0.5 * (approx + n/approx)
return approx
print('The square root is' ,newtonSqrt(81))
4. GE 8161 β Problem Solving and Python Programming Laboratory
Output:
('The square root is', 9.0)
5. GE 8161 β Problem Solving and Python Programming Laboratory
Program:
n = int(input("Enter the number: "))
e = int(input("Enter the expo: "))
r=n
for i in range(1,e):
r=n*r
print("Exponent is :", r)
6. GE 8161 β Problem Solving and Python Programming Laboratory
Output:
Enter the number: 2
Enter the expo: 3
('Exponent is :', 8)
7. GE 8161 β Problem Solving and Python Programming Laboratory
Program:
alist = [-45,0,3,10,90,5,- 2,4,18,45,100,1,-266,706]
largest = alist[0]
for large in alist:
if large > alist:
largest = large
print(βThe Largest Number is:β,largest)
8. GE 8161 β Problem Solving and Python Programming Laboratory
Output:
(βThe Largest of the List is:β, 706)
9. GE 8161 β Problem Solving and Python Programming Laboratory
Program:
def Linearsearch(alist,item):
pos=0
found=False
stop=False
while pos<len(alist) and not found and not stop:
if alist[pos]==item:
found=True
print("Element found in position",pos)
else:
if alist[pos]>item:
stop=True
else:
pos=pos+1
return found
a=[]
n=int(input("Enter upper limit"))
for i in range(0,n):
e=int(input("Enter the elements"))
a.append(e)
x=int(input("Enter element to search"))
Linearsearch(a,x)
10. GE 8161 β Problem Solving and Python Programming Laboratory
Output:
Enter upper limit:5
Enter the elements:1
Enter the elements:8
Enter the elements:9
Enter the elements:6
Enter the elements:7
Enter element to search:9
Element found in position: 2
11. GE 8161 β Problem Solving and Python Programming Laboratory
Program:
def binarysearch(a,n,k):
low = 0
high = n
while(low<=high):
mid=int((low+high)/2)
if(k==a[mid]):
return mid
elif(k<a[mid]):
high=mid-1
else:
low=mid+1
return -1
n=int(input("Enter the size of list"))
a=[i for i in range(n)]
print(n)
print("Enter the element")
for i in range(0,n):
a[i]=int(input())
print("Enter the element to be searched")
k=int(input())
position=binarysearch(a,n,k)
if(position!=-1):
print("Entered no:%d is present:%d"%(k,position))
else:
print("Entered no:%d is not present in list"%k)
12. GE 8161 β Problem Solving and Python Programming Laboratory
Output:
Enter the size of list
5
Enter the element
1
8
6
3
4
Enter the element to be searched
1
Entered no:1 is present:0
13. GE 8161 β Problem Solving and Python Programming Laboratory
Program:
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):
pos=0
for location in range(1,i+1):
if alist[location]>alist[pos]:
pos= location
temp = alist[i]
alist[i] = alist[pos]
alist[pos] = temp
alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)
14. GE 8161 β Problem Solving and Python Programming Laboratory
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]
15. GE 8161 β Problem Solving and Python Programming Laboratory
Program:
def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]
position = index
while position>0 and alist[position-1]>currentvalue:
alist[position]=alist[position-1]
position = position-1
alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)
16. GE 8161 β Problem Solving and Python Programming Laboratory
Output:
[17, 20, 26, 31, 44, 54, 55, 77, 93]
17. GE 8161 β Problem Solving and Python Programming Laboratory
Program:
def mergeSort(alist):
print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=0
j=0
k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1
else:
alist[k]=righthalf[j]
j=j+1
k=k+1
while i < len(lefthalf):
alist[k]=lefthalf[i]
i=i+1
k=k+1
while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1
print("Merging ",alist)
alist = [23, 6, 4 ,12, 9]
mergeSort(alist)
print(alist)
19. GE 8161 β Problem Solving and Python Programming Laboratory
Program:
i=1
x = int(input("Enter the Upper Limit:"))
for k in range (1, (x+1), 1):
c=0;
for j in range (1, (i+1), 1):
a = i%j
if (a==0):
c = c+1
if (c==2):
print (i)
else:
k = k-1
i=i+1
20. GE 8161 β Problem Solving and Python Programming Laboratory
Output:
Enter the Upper Limit:100
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
21. GE 8161 β Problem Solving and Python Programming Laboratory
Program:
X= [[1,2,7],
[4,5,6],
[1,2,3]]
Y= [[5,8,1],
[6,7,3],
[4,5,1]]
result= [[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(Y[0])):
for k in range(len(Y)):
result[i][j]+=X[i][k]*Y[k][j]
for r in result:
print(r)
22. GE 8161 β Problem Solving and Python Programming Laboratory
Output:
[45, 57, 14]
[74, 97, 25]
[29, 37, 10]
23. GE 8161 β Problem Solving and Python Programming Laboratory
Program:
import sys
if len(sys.argv)!=2:
print('Error: filename missing')
sys.exit(1)
filename = sys.argv[1]
file = open(filename, "r+")
wordcount={}
for word in file.read().split():
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word]+=1
for k,v in wordcount.items():
print (k, v)
24. GE 8161 β Problem Solving and Python Programming Laboratory
Output:
25. GE 8161 β Problem Solving and Python Programming Laboratory
Program:
filename = input("Enter file name:")
inf = open(filename,'r')
wordcount={}
for word in inf.read().split():
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
for key in wordcount.keys():
print("%sn%sn"%(key, wordcount[key]))
linecount = 0
for i in inf:
paragraphcount = 0
if'n'in i:
linecount += 1
if len(i)<2: paragraphcount *= 0
elif len(i)>2: paragraphcount = paragraphcount + 1
print('%-4dn%4dn%sn'%(paragraphcount,linecount,i))
inf.close()
26. GE 8161 β Problem Solving and Python Programming Laboratory
Output:
Enter file name:'text.txt'
a
1
used
1
for
1
language
1
Python
1
is
1
programming
2
general-purpose
1
widely
1
high-level
1
28. GE 8161 β Problem Solving and Python Programming Laboratory
Output:
29. GE 8161 β Problem Solving and Python Programming Laboratory
Program:
import pygame,sys,time,random
from pygame.locals import*
from time import*
pygame.init()
windowSurface=pygame.display.set_mode((500,400),0,32)
pygame.display.set_caption("BOUNCE")
BLACK=(0,0,0)
WHITE=(255,255,255)
RED=(255,0,0)
GREEN=(0,255,0)
BLUE=(0,0,255)
info=pygame.display.Info()
sw=info.current_w
sh=info.current_h
y=0
direction=1
while True:
windowSurface.fill(BLACK)
pygame.draw.circle(windowSurface,GREEN,(250,y),13,0)
sleep(.006)
y+=direction
if y>=sh:
direction=-1
elif y<=0:
direction=1
pygame.display.update()
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
30. GE 8161 β Problem Solving and Python Programming Laboratory
Output: