Professional Documents
Culture Documents
WHAT IS PYTHON?
WHY PYTHON?
1. Simple syntax
2. Free, Open source
3. Interpreted Language
4. Good support
5. Short programs
6. Multi-platform support
Interactive Mode
Script Mode
1.PROGRAM TO FIND THE GCD OF TWO NUMBERS:
rem =n1%n2
while rem!=0:
n1 = n2
n2 = rem
rem = n1% n2
Enter a number:54
(NEWTON’S METHOD):
def newtonSqrt(n):
approx = 0.5 * n
approx = better
return approx
e=int(input(“enter exponent:”))
r=n
r=n*r
print(‘exponentiation is :’,r)
OUTPUT:
Enter number:25
Enter exponent:2
Exponent is :625
4.TO FIND THE MAXIMUM AND MINIMUM OF A LIST OF
NUMBERS:
list=[]
for i in range(0,n):
list.append(a)
maxno =list[0]
minno =list[0]
for i in range(len(list)):
if list[i]>maxno:
maxno=list[i]
if list[i]<minno:
minno=list[i]
found = False
for i in range(len(list)):
if(list[i] == x):
found = True
break
else:
def Binary_search(arr,start,last,element):
mid =(int)(start+last)/2
if(start>last):
elif (element>arr[mid]):
start = mid+1
Binary_search(arr,start,last,element)
elif (element<arr[mid]):
last = mid-1
Binary_search(arr,start,last,element)
else:
arr = [2,14,19,21,99,100,102,103,104,181]
element = 100
start = 0
last = len(arr)-1
Binary_search(arr,start,last,element)
OUTPUT:
least=i
least=k
def swap(a,x,y):
tmp =a[x]
a[x]=a[y]
a[y]=tmp
return (a,x,y)
a=[]
a.append(int(input ()))
ssort(a)
def isort(a):
currentvalue =a[index]
position=index
a[position]=a[position-1]
position=position-1
a[position]=currentvalue
a=[]
a.append(int(input()))
isort(a)
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
alist[k] = lefthalf[i]
i=i+1
k=k+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=[13,2,17,76,82,12,7]
mergesort (alist)
print (alist)
OUTPUT:
splitting [13, 2, 17, 76, 82, 12, 7]
splitting [13, 2, 17]
splitting [13]
merging [13]
splitting [2, 17]
splitting [2]
merging [2]
splitting [17]
merging [17]
merging [2, 17]
merging [2, 13, 17]
splitting [76, 82, 12, 7]
splitting [76, 82]
splitting [76]
merging [76]
splitting [82]
merging [82]
merging [76, 82]
splitting [12, 7]
splitting [12]
merging [12]
splitting [7]
merging [7]
merging [7, 12]
merging [7, 76, 82, 12]
merging [2, 13, 17, 7, 76, 82, 12]
[2, 13, 17, 7, 76, 82, 12]
8. FIRST n PRIME NUMBERS
for i in range(2,num):
if num%i == 0:
j=num/i
break
else:
1 is a prime number
2 is a prime number
3 is a prime number
4 equals 2*2
5 is a prime number
6 equals 2*3
9. MULTIPLY MATRICES:
A=[[1,2],
[3,4]]
B=[[1,2],
[3,4]]
C=[[0,0],
[0,0]]
For j in range(len(b[0])):
For k in range(len(b)):
For r in c:
Print(r)
OUTPUT:
Resultant matrix
[7, 10]
[15, 22]
10.COMMAND LINE ARGUMENTS (WORD COUNT)
import sys
parser = argparse.ArgumentParser()
parser.add_argument('--words','-w', action='store_true')
parser.add_argument('--lines','-l', action='store_true')
parser.add_argument('filename')
args = parser.parse_args()
if args.words:
elif args.lines:
else:
Word in a file 23
Lines in a file 1
11.TO FIND THE MOST FREQUENT WORDS IN A TEXT
READ FROM A FILE:
def main():
infile=open(filename, "r")
wordcounts={}
processLine(line.lower(),wordcounts)
pairs=list(wordcounts.items())
items.sort()
line=replacepunctuations (line)
words =line.split()
if word in wordcounts:
wordcounts[word] += 1
else:
wordcounts[word]=1
def replacepunctuations(line):
for ch in line:
if ch in "-@#$%^&*()_+=_<>?/,.;:!{}[]'\"":
line=line.replace(ch,"")
return line
main()
OUTPUT:
enter a filename: cse.txt
hi 1
how 1
are 1
you 1
12. SIMULATE ELLIPTICAL ORBITS IN PYGAME
import math
import random
import pygame
class Particle ():
def __init__ (self, x, y, colour=0x000000):
self.x = x
self.y = y s
elf.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.x += self.vx
self.y += self.vy
pygame.init()
0x800000]
p.vx = i / 100
while (True):
# main_surface.fill(0x000000)
for p in particles:
p.apply_gravity (earth)
p.update ()
display.flip()
OUTPUT:
13. SIMULATE BOUNCING BALL
import sys
import pygame
pygame.init()
size = width, height = 320, 240
speed = [2, 2]
black = 0, 0, 0
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: