SlideShare a Scribd company logo
UNIVERSITY COLLEGE OF ENGINEERING, ARIYALUR
(A Constituent College of Anna University, Chennai)
Ariyalur-621704
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
LABORATORY RECORD
NAME
SUBJECT CODE & NAME :.........................................................................
: 
REGISTER NUMBER : ....
BRANCH : ....
UNIVERSITY COLLEGE OF ENGINEERING, ARIYALUR
(A Constituent College of Anna University, Chennai)
Ariyalur-621704
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
BONAFIDE CERTIFICATE
Certified that this is bonafide record work done by
Mr/Ms …………………………..……With Register No ……………………..
for GE8161 - Problem Solving & Python Programming Laboratory in the
First year - Ist
semester of B.E Degree Course in Computer Science and
Engineering branch during the Academic Year 2019-2020.
Staff-In charge Head of the Department
Submitted for University Practical Examination held on ……………………
INTERNAL EXAMINER EXTERNAL EXAMINER
INDEX
S.No Name of the Experiments Page No Staff Sign
1. Compute the GCD of two numbers
2. Find the square root of a number (Newton‘s
method)
3. Exponentiation (power of a number)
4. Find the maximum of a list of numbers
5. Linear search and Binary search
6. Selection sort, Insertion sort
7. Merge sort
8. First n prime numbers
9. Multiply matrices
10. Programs that take command line arguments (word
count)
11. Find the most frequent words in a text read from a
file
12. Simulate elliptical orbits in Pygame
13. Simulate bouncing ball using Pygame
Compute the GCD of two numbers
Program
d1 = int(input("Enter a number:"))
d2 = int(input("Enter another number"))
rem = d1 % d2
while rem != 0 :
d1 = d2
d2 = rem
rem=d1 % d2
print ("gcd of given numbers is :", d2)
Output :
Find the square root of a number (Newton‘s method)
Program
def newtonSqrt(n, howmany):
approx = 0.5 * n
for i in range(howmany):
betterapprox = 0.5 * (approx + n/ approx)
approx = betterapprox
return betterapprox
print(newtonSqrt(10, 3))
print(newtonSqrt(10, 5))
print(newtonSqrt(10, 10))
Find the square root of a number
n = int(input("Enter a number"))
howmany = int(input("Enter another number"))
approx = 0.5 * n
for i in range(howmany):
betterapprox = 0.5 *(approx + n/approx)
approx = betterapprox
print("The square root of a number is:", betterapprox)
Output:
Basic python laboratoty_ PSPP Manual .docx
Exponentiation (power of a number)
Program
n = input ("Enter a number : ")
n = int(n)
e = input ("Enter an exponent : ")
e = int(e)
r = n
for i in range (1,e):
r = n * r
print(r)
Output:
Basic python laboratoty_ PSPP Manual .docx
Find the maximum of a list of numbers
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("Largest element is:",a[n-1])
Output:
Linear search
Program
list = [4,1,2,5,3] #Set up array
search = int(input("Enter search number")) # Ask for a number
for i in range(0,len(list)): # Repeat for each item in list
if search==list[i]: #if item at position i is search time
print(str(search)+"found at position " + str(i)) #Report found
Output:
Binary search
Program
def binary_search(item_list,item):
first = 0
last = len(item_list)-1
found = False
while( first<=last and not found):
mid = (first + last)//2
if item_list[mid] == item :
found = True
else:
if item < item_list[mid]:
last = mid - 1
else:
first = mid + 1
return found
print(binary_search([1,2,3,5,8], 6))
print(binary_search([1,2,3,5,8], 5))
Output:
Selection sort
Program
def selectionSort(alist):
for fillslot in range(len(alist) - 1, 0, -1):
positionOfMax = 0
for location in range(1, fillslot + 1):
if alist[location] > alist[positionOfMax]:
positionOfMax = location
temp = alist[fillslot]
alist[fillslot] = alist[positionOfMax]
alist[positionOfMax] = temp
alist = [54, 26, 93, 17, 77, 31, 44, 55, 20]
selectionSort(alist)
print(alist)
Output:
Insertion sort
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)
Output:
Merge sort
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 = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)
Output:
First n prime numbers
Program
a = int(input("Enter number: "))
k = 0
for i in range(2,a//2):
if(a%i==0):
k = k + 1
if(k <= 0):
print("Number is prime")
else:
print("Number isn't prime")
Output:
Multiply matrices
Program
matrix1 = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
matrix2 = [[10, 11, 12, 13],
[14, 15, 16, 17],
[18, 19, 20, 21]]
rmatrix = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
rmatrix[i][j] += matrix1[i][k] * matrix2[k][j]
for r in rmatrix:
print(r)
Output:
Programs that take command line arguments (word count)
Program
fname = raw_input("Enter file name: ") #test.txt
num_words = 0
with open(fname, 'r') as f:
for line in f:
words = line.split()
num_words += len(words)
print("Number of words:")
print(num_words)
Output:
Find the most frequent words in a text read from a file
Program
fname = input("Enter file name: ") #test.txt
l=input("Enter letter to be searched:")
k = 0
with open(fname, 'r') as f:
for line in f:
words = line.split()
for i in words:
for letter in i:
if(letter==l):
k=k+1
print("Occurrences of the letter:")
print(k)
Output:
Simulate elliptical orbits in Pygame
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) + 300
y1 = int(math.sin(degree * 2 * math.pi / 360) * yRadius) + 150
screen.fill((0, 0, 0))
pygame.draw.circle(screen, (255, 0, 0), [300, 150], 35)
pygame.draw.ellipse(screen, (255, 255, 255), [50, 50, 500, 200], 1)
pygame.draw.circle(screen, (0, 0, 255), [x1, y1], 15)
pygame.display.flip()
clock.tick(5)
Output:
Simulate bouncing ball using Pygame
Program
import sys, pygame
pygame.init()
size = width, height = 700, 300
speed = [1, 1]
background = 255, 255, 255
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Bouncing ball")
ball = pygame.image.load("ball2.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(background)
screen.blit(ball, ballrect)
pygame.display.flip()
Output:

More Related Content

PDF
III MCS python lab (1).pdf
PPTX
Python programming workshop session 3
PDF
python practicals-solution-2019-20-class-xii.pdf
PDF
Xi CBSE Computer Science lab programs
PDF
analysis of data structure design programs
PPTX
4. functions
PDF
solution-of-practicals-class-xii-comp.-sci.-083-2021-22 (1).pdf
PPTX
Python programming workshop
III MCS python lab (1).pdf
Python programming workshop session 3
python practicals-solution-2019-20-class-xii.pdf
Xi CBSE Computer Science lab programs
analysis of data structure design programs
4. functions
solution-of-practicals-class-xii-comp.-sci.-083-2021-22 (1).pdf
Python programming workshop

Similar to Basic python laboratoty_ PSPP Manual .docx (20)

PPTX
Working with functions.pptx. Hb.
PPT
2025pylab engineering 2025pylab engineering
PDF
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
PDF
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
PDF
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PDF
design and analysis of algorithm Lab files
PDF
Python Manuel-R2021.pdf
PDF
Python Lab manual program for BE First semester (all department
PDF
Advanced Web Technology ass.pdf
DOCX
Python Laboratory Programming Manual.docx
PDF
The Ring programming language version 1.8 book - Part 94 of 202
PPTX
GE8151 Problem Solving and Python Programming
PDF
programs on arrays.pdf
PDF
Functions
PDF
Kotlin for Android Developers - 2
DOCX
cs class 12 project computer science .docx
PPTX
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
DOCX
ECE-PYTHON.docx
PDF
SWP - A Generic Language Parser
PPTX
USER DEFINE FUNCTIONS IN PYTHON
Working with functions.pptx. Hb.
2025pylab engineering 2025pylab engineering
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
design and analysis of algorithm Lab files
Python Manuel-R2021.pdf
Python Lab manual program for BE First semester (all department
Advanced Web Technology ass.pdf
Python Laboratory Programming Manual.docx
The Ring programming language version 1.8 book - Part 94 of 202
GE8151 Problem Solving and Python Programming
programs on arrays.pdf
Functions
Kotlin for Android Developers - 2
cs class 12 project computer science .docx
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
ECE-PYTHON.docx
SWP - A Generic Language Parser
USER DEFINE FUNCTIONS IN PYTHON
Ad

More from Kirubaburi R (9)

PDF
CS3481_Database Management Laboratory .pdf
DOCX
Internet Programming laboratory -manual.docx
PDF
Cryptography and network security record for cse .pdf
DOCX
object oriented programming lab manual .docx
DOCX
CCS354-NETWORK SECURITY-network-security notes
PDF
Big Data
PDF
PDF
Honeypots for Network Security
PDF
L 1000 423
CS3481_Database Management Laboratory .pdf
Internet Programming laboratory -manual.docx
Cryptography and network security record for cse .pdf
object oriented programming lab manual .docx
CCS354-NETWORK SECURITY-network-security notes
Big Data
Honeypots for Network Security
L 1000 423
Ad

Recently uploaded (20)

PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Construction Project Organization Group 2.pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
UNIT 4 Total Quality Management .pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
PPT on Performance Review to get promotions
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
Sustainable Sites - Green Building Construction
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
web development for engineering and engineering
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Foundation to blockchain - A guide to Blockchain Tech
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Construction Project Organization Group 2.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
UNIT 4 Total Quality Management .pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
CYBER-CRIMES AND SECURITY A guide to understanding
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPT on Performance Review to get promotions
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Sustainable Sites - Green Building Construction
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
web development for engineering and engineering
Operating System & Kernel Study Guide-1 - converted.pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems

Basic python laboratoty_ PSPP Manual .docx

  • 1. UNIVERSITY COLLEGE OF ENGINEERING, ARIYALUR (A Constituent College of Anna University, Chennai) Ariyalur-621704 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING LABORATORY RECORD NAME SUBJECT CODE & NAME :......................................................................... :  REGISTER NUMBER : .... BRANCH : ....
  • 2. UNIVERSITY COLLEGE OF ENGINEERING, ARIYALUR (A Constituent College of Anna University, Chennai) Ariyalur-621704 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING BONAFIDE CERTIFICATE Certified that this is bonafide record work done by Mr/Ms …………………………..……With Register No …………………….. for GE8161 - Problem Solving & Python Programming Laboratory in the First year - Ist semester of B.E Degree Course in Computer Science and Engineering branch during the Academic Year 2019-2020. Staff-In charge Head of the Department Submitted for University Practical Examination held on …………………… INTERNAL EXAMINER EXTERNAL EXAMINER
  • 3. INDEX S.No Name of the Experiments Page No Staff Sign 1. Compute the GCD of two numbers 2. Find the square root of a number (Newton‘s method) 3. Exponentiation (power of a number) 4. Find the maximum of a list of numbers 5. Linear search and Binary search 6. Selection sort, Insertion sort 7. Merge sort 8. First n prime numbers 9. Multiply matrices 10. Programs that take command line arguments (word count) 11. Find the most frequent words in a text read from a file 12. Simulate elliptical orbits in Pygame 13. Simulate bouncing ball using Pygame Compute the GCD of two numbers
  • 4. Program d1 = int(input("Enter a number:")) d2 = int(input("Enter another number")) rem = d1 % d2 while rem != 0 : d1 = d2 d2 = rem rem=d1 % d2 print ("gcd of given numbers is :", d2) Output :
  • 5. Find the square root of a number (Newton‘s method)
  • 6. Program def newtonSqrt(n, howmany): approx = 0.5 * n for i in range(howmany): betterapprox = 0.5 * (approx + n/ approx) approx = betterapprox return betterapprox print(newtonSqrt(10, 3)) print(newtonSqrt(10, 5)) print(newtonSqrt(10, 10)) Find the square root of a number n = int(input("Enter a number")) howmany = int(input("Enter another number")) approx = 0.5 * n for i in range(howmany): betterapprox = 0.5 *(approx + n/approx) approx = betterapprox print("The square root of a number is:", betterapprox) Output:
  • 8. Exponentiation (power of a number) Program n = input ("Enter a number : ") n = int(n) e = input ("Enter an exponent : ") e = int(e) r = n for i in range (1,e): r = n * r print(r) Output:
  • 10. Find the maximum of a list of numbers 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("Largest element is:",a[n-1])
  • 12. Program list = [4,1,2,5,3] #Set up array search = int(input("Enter search number")) # Ask for a number for i in range(0,len(list)): # Repeat for each item in list if search==list[i]: #if item at position i is search time print(str(search)+"found at position " + str(i)) #Report found
  • 14. Program def binary_search(item_list,item): first = 0 last = len(item_list)-1 found = False while( first<=last and not found): mid = (first + last)//2 if item_list[mid] == item : found = True else: if item < item_list[mid]: last = mid - 1 else: first = mid + 1 return found print(binary_search([1,2,3,5,8], 6)) print(binary_search([1,2,3,5,8], 5))
  • 16. Program def selectionSort(alist): for fillslot in range(len(alist) - 1, 0, -1): positionOfMax = 0 for location in range(1, fillslot + 1): if alist[location] > alist[positionOfMax]: positionOfMax = location temp = alist[fillslot] alist[fillslot] = alist[positionOfMax] alist[positionOfMax] = temp alist = [54, 26, 93, 17, 77, 31, 44, 55, 20] selectionSort(alist) print(alist) Output:
  • 18. 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) Output:
  • 20. 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
  • 21. k=k+1 print("Merging ",alist) alist = [54,26,93,17,77,31,44,55,20] mergeSort(alist) print(alist) Output:
  • 22. First n prime numbers
  • 23. Program a = int(input("Enter number: ")) k = 0 for i in range(2,a//2): if(a%i==0): k = k + 1 if(k <= 0): print("Number is prime") else: print("Number isn't prime") Output:
  • 25. Program matrix1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] matrix2 = [[10, 11, 12, 13], [14, 15, 16, 17], [18, 19, 20, 21]] rmatrix = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] for i in range(len(matrix1)): for j in range(len(matrix2[0])): for k in range(len(matrix2)): rmatrix[i][j] += matrix1[i][k] * matrix2[k][j] for r in rmatrix: print(r) Output:
  • 26. Programs that take command line arguments (word count)
  • 27. Program fname = raw_input("Enter file name: ") #test.txt num_words = 0 with open(fname, 'r') as f: for line in f: words = line.split() num_words += len(words) print("Number of words:") print(num_words)
  • 29. Find the most frequent words in a text read from a file Program fname = input("Enter file name: ") #test.txt l=input("Enter letter to be searched:") k = 0 with open(fname, 'r') as f: for line in f: words = line.split() for i in words: for letter in i: if(letter==l): k=k+1 print("Occurrences of the letter:") print(k)
  • 31. Simulate elliptical orbits in Pygame 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) + 300 y1 = int(math.sin(degree * 2 * math.pi / 360) * yRadius) + 150 screen.fill((0, 0, 0)) pygame.draw.circle(screen, (255, 0, 0), [300, 150], 35) pygame.draw.ellipse(screen, (255, 255, 255), [50, 50, 500, 200], 1) pygame.draw.circle(screen, (0, 0, 255), [x1, y1], 15) pygame.display.flip() clock.tick(5)
  • 33. Simulate bouncing ball using Pygame Program import sys, pygame pygame.init() size = width, height = 700, 300 speed = [1, 1] background = 255, 255, 255 screen = pygame.display.set_mode(size) pygame.display.set_caption("Bouncing ball") ball = pygame.image.load("ball2.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(background) screen.blit(ball, ballrect) pygame.display.flip()