Python Lab Manual - STUDENTCOPY2
Python Lab Manual - STUDENTCOPY2
PAGE
S.NO DATE NAME OF THE EXPERIMENT MARK SIGN
NO.
3 EXPONENTIATION (POWER OF A
NUMBER)
FINDING THE MAXIMUM OF A
4 LIST OF NUMBERS
7 MERGE SORT
MULTIPLY MATRICES
9
Program:
if x > y:
smaller = y
else:
smaller = x
gcd = i
return gcd
num1 = 20
num2 = 10
Output:
Program:
approx = 0.5 * n
for i in range(a):
approx = betterapprox
return betterapprox
print(newtonSqrt(10, 3))
print(newtonSqrt(10, 5))
print(newtonSqrt(10, 10))
Output:
3.162319422150883
3.162277660168379
3.162277660168379
Ex.No:3EXPONENTIATION
Date:
Program:
Output:
Result: 125
Ex.No:4FIND THE MAXIMUM OF A LIST OF NUMBERS
Date:
Program:
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("Maximum of a List of Number is:",a[n-1])
Output:
Program:
Output:
return found
if(found = = FALSE):
print (“The element {} is not found in the list”.format(-1))
el s e :
print (“The element {} is found in the list”.format(13))
blist= [0, 1 , 2, 8 , 13, 17, 19, 32, 42]
print(binarySearch(blist, -1))
print(binarySearch(blist, 13))
Output:
Program:
def selsort(sample):
print("intial list:",sample)
for i in range(len(sample)):
print(sample)
minIndex=i
j=i+1
Output:
def insertsort(sample):
print("sorted list:",sample)
sample1 = [12,300,-90,-100-1000,1,4]
Insertsort(sample1)
Output:
[-1000,-100,-90,1,4,12,300]
Ex.No:7 MERGE SORT
Date:
Program:
def merge(left,right):
result = []
i,j = 0, 0
while i<len(left) and j<len(right):
if left[i] <= right[j]:
result.append(left[i])
i+=1
else:
result.append(right[j])
j+=1
result += left[i:]
result += right[j:]
returnresult
defmergesort(lst):
if(len(lst) <= 1):
returnlst
mid = int(len(lst)/2)
left = mergesort(lst[:mid])
right = mergesort(lst[mid:])
return merge(left,right)
arr = [1,2,-1,0,9,65,7,3,4,1,2]
print(mergesort(arr))
Output:
[-1,0,1,2,2,3,4,,7,9,65]
Ex.No:8FIRST N PRIME NUMBERS
Date:
Program:
Output:
Program:
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
# 3x4
matrix Y
=[[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
# result is 3x4
result =
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
for i in range(len(X)):
for r in result:
print(r)
Output:
Program:
num_words = 0
for line in f:
words = line.split()
num_words += len(words)
print("Number of words:")
print(num_words)
Output:
Program:
fr = open("prempaul.txt","r")
wordcount = {}
for word in fr.read().split():
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
for k,v in wordcount.items():
print(k, v)
fr.close()
Output:
To - 1
Find - 1
The - 1
Most-1
Frequent-1
Words-1
In-1
A-2
Text-1
Read-1
From-1
File -1
Ex.No:12SIMULATE ELLIPTICAL ORBITS IN PYGAME
Date:
Program:
import math
import random
import pygame
class Particle ():
def __init__ (self, x, y, colour=0x000000):
self.x = x
self.y = y
self.vx = 0
self.vy = 0
self.colour = colour
def apply_gravity (self, target):
dsqd = (self.x - target.x) ** 2 + (self.y - target.y) ** 2
#g = G*m/dsqd * normalized (self - target)
if dsqd == 0:
return
self.vx += -1 / dsqd * (self.x - target.x) / dsqd ** 0.5
self.vy += -1 / dsqd * (self.y - target.y) / dsqd ** 0.5
def update (self):
self.x += self.vx
self.y += self.vy
pygame.init()
window = pygame.display.set_mode ((600, 400))
main_surface = pygame.Surface ((600, 400))
colours = [0x000000, 0x111111, 0x222222, 0x333333, 0x444444, 0x555555,
0x666666, 0x777777, 0x888888, 0x999999, 0xaaaaaa, 0xbbbbbb] +
[0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF,
0x888888, 0xFFFFFF, 0x808000, 0x008080, 0x800080, 0x800000]
#colours = [0xFF0000, 0x00FF00, 0x0000FF,
0xFFFF00, 0xFF00FF, 0x00FFFF, 0x888888,
0xFFFFFF, 0x808000, 0x008080, 0x800080,
0x800000] particles = [Particle (200, 100,
colours [i]) for i in range (20)] earth =
Particle (200, 200)
for i, p in enumerate (particles):
p.vx = i / 100
while (True):
# main_surface.fill(0x000000)
pygame.draw.circle (main_surface, 0x00FF00, (earth.x, earth.y), 5, 2)
for p in particles:
p.apply_gravity (earth)
p.update ()
pygame.draw.circle (main_surface, p.colour, (int (p.x), int (p.y)), 5, 2)
window.blit(main_surface, (0, 0))
pygame.display.flip()
Output:
\
Ex.No:13 SIMULATE BOUNCING BALL USING PYGAME
Date:
Program:
import sys
import pygame
pygame.init()
screen = pygame.display.set_mode(size)
ball = pygame.image.load('C:\\Users\\admin\\Desktop//ball.jpg')
ballrect = ball.get_rect()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
screen.fill(black)
screen.blit(ball, ballrect)
pygame.display.flip()
Output: