Python Lab Programs
Python Lab Programs
n1 = int(input("Enter a number:"))
n2 = int(input("Enter another number:"))
rem = n1 % n2
while rem != 0 :
n1 = n2
n2 = rem
rem=n1 % n2
print ("GCD of given numbers is :", n2)
Output:
Enter a number:4
Enter another number:8
gcd of given numbers is : 4
2. FIND SQUARE ROOT OF A NUMBER(NEWTON’S METHOD)
n = int(input("Enter a number"))
c = int(input("Enter count"))
approx = 0.5 * n
for i in range(c):
final = 0.5 *(approx + n/approx)
approx = final
print("The square root of a number is:", final)
print("If answer is not exact Increase the count value")
Output:
Enter a number 64
Enter count 7
The square root of a number is: 8.0
If answer is not exact Increase the count value
3.POWER OF NUMBER
Program 1:
Program 2:
Output:
Enter a number : 5
Enter an power : 3
125
4. FIND THE MAXIMUM OF A LIST OF NUMBERS
Program 1:
a = []
n = int(input("Enter number of elements:"))
for i in range(1, n+1):
b = int(input("Enter element:"))
a.append(b)
a.sort()
print("Largest element is:",a[n-1])
Output:
Enter number of elements:5
Enter element:34
Enter element:12
Enter element:55
Enter element:34
Enter element:10
Largest element is: 55
Program 2:
L=[2,26,7,9,12]
max=L[0]
for i in L:
if (i>max):
max=i
print(Largest element is:”,max)
Output:
Output:
Enter search number5
found at position 3
num=int(input("enter no"))
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
break
else:
print(num,"is a prime number")
Output:
enter no 5
5 is a prime number
enter no 6
6 is not a prime number
7.FIRST N PRIME NUMBERS
Output
enter the upper limit10
2
3
5
7
8.EXPONENTIATION OF NUMBER
import math
n = int(input("Enter number :"))
e=math.exp(n)
print(“Exponentiation is “,e)
output
Enter number :3
Exponentiation is 8.154
9.Binary Search
def binarysearch(list,no):
first = 0
last = len(list)-1
found = False
while( first<=last and not found):
mid = (first + last)//2
if list[mid] == no :
found = True
pos=mid+1
else:
if no < list[mid]:
last = mid - 1
else:
first = mid + 1
if(return==True):
print(“Found in Position”,pos)
else:
print(“Number not found”)
list=[10,20,40,50,60]
no=int(input(”Enter search No”))
binarysearch(list,no)
output:
Enter search No
20
Found in Position 2
10. Bubble Sort
def bubble_sort(lst):
n=len(lst)
for i in range(n):
for j in range(i+1,n):
if lst[j] < lst[i]:
lst[j], lst[i] = lst[i], lst[j]
return lst
lst = [50,40,30,20,10,67,34,90]
list=bubble_sort(lst)
print(list)
output
for i in range(len(m1)):
for j in range(len(m2[0])):
for k in range(len(m2)):
m3[i][j]+=m1[i][k]*m2[k][j]
for k in m3:
print(k)
output:
def insertionsort(List):
n=len(List)
for i in range(1,n):
tmp = List[i]
j=i-1
List[j+1]=List[j]
j-= 1
List[j+1]=tmp
List = [100,40,30,20,10,67,34,90]
insertionsort(List)
print(List)
out put:
def mergesort(seq):
if len(seq)<=1:
return seq
m=len(seq)//2
return merge(mergesort(seq[:m]),mergesort(seq[m:]))
result = []
i=j=0
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result += left[i:]
result += right[j:]
return result
r= mergesort([50,40,30,20,10])
print(r)
output:
10,20,30,40,50
14.Selection Sort
n=len(list)
for i in range(n):
midx = i
for j in range(i+1,n):
midx = j
print(list)
output:
11,12,22,25,64
15.To Print Python Version, Count Number of Arguments(usingCommand Line Argument)
import sys
output:
version is 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)]
Number of arguments: 1
num_words=0
words=line.split()
num_words+=len(words)
print("Number of Words:")
print(num_words)
output:
Number of Words:
b.txt
cat bat
rat apple
run pan
man
17.Program to find frequency of words in a file
f=open("D:/b.txt","r+")
wc={}
wc[word]=1
else:
wc[word]+=1
print(k,v)
f.close()
output:
cat 1
bat 3
rat 2
apple 2
run 2
pan 2
man 3
b.txt
cat bat
rat apple
run pan
bat bat
rat apple
run pan
man man man
Program:
import pygame
import math
import sys
pygame.init()
screen=pygame.display.set_mode([600,300])
pygame.display.set_caption("Elliptical Orbit")
clock = pygame.time.Clock()
while(True):
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
xRadius=250
yRadius=100
for degree in range(0,360,10):
x1=int(math.cos(degree*2*math.pi/360)*xRadius)+360
y1=int(math.sin(degree*2*math.pi/360)*yRadius)+360
screen.fill([0,0,255])
pygame.draw.circle(screen,[255,0,0],[300,150],50,0)
pygame.draw.ellipse(screen,[255,255,255],[50,50,500,200],1)
pygame.draw.circle(screen,[0,255,255],[x1,y1],15)
pygame.display.flip()
clock.tick(5)