Python Lab Manual Final
Python Lab Manual Final
BONAFIDE CERTIFICATE
Name :
Degree :
Branch :
Reg. No. :
Certified that this is the bonafide record of the work done by the above student in
Laboratory during
the academic year
Students must be present in proper dress code and wear the ID card.
Students should enter the log-in and log-out time in the log register without fail.
Students are not allowed to download pictures, music, videos or files without
the permission of respective lab in-charge.
Students should wear their own lab coats and bring observation note books to
the laboratory classes regularly.
Record of experiments done in a particular class should be submitted in the
next lab class.
Students who do not submit the record note book in time will not be allowed
to do the next experiment and will not be given attendance for that laboratory
class.
Students will not be allowed to leave the laboratory until they complete the
experiment.
Students are advised to switch-off the Monitors and CPU when they leave the
lab.
Students are advised to arrange the chairs properly when they leave the lab.
College
Vision
To improve the quality of human life through multi-disciplinary programs in
Engineering, architecture and management that are internationally recognized and would
facilitate research work to incorporate social economical and environmental development.
Mission
To create a vibrant atmosphere that creates competent engineers, innovators, scientists,
entrepreneurs, academicians and thinkers of tomorrow.
To establish centers of excellence that provides sustainable solutions to industry and
society.
To enhance capability through various values added programs so as to meet the
challenges of dynamically changing global needs.
Department
Vision
To cultivate creative, globally competent, employable and disciplined computing
professionals with the spirit of benchmarking educational system that promotes academic
excellence, scientific pursuits, entrepreneurship and professionalism.
Mission
To develop the creators of tomorrows technology to meet the social needs of our nation.
To promote and encourage the strength of research in Engineering, Science and Technology.
The Graduates inculcate soft skills such as communication, team work, leadership
PEO3 qualities, professional and ethical values and an ability to apply the acquired skills to
address the societal issues.
The Graduates will be able to work with an environment conducive for life-long learning
PEO4 needed for a successful professional career.
Program Outcomes(POs)
Design, develop, test and maintain software in area of algorithms, system software,
web design, multimedia, big data analytics and networking for efficient design for
PSO2
computer based system.
Course Outcomes(COs)
C03 Develop Python programs step-wise by defining functions and calling them.
C04 Use Python lists, tuples, dictionaries for representing compound data.
Mapping
Course
Outcome PO PO PO PO PO PO PO PO PO PO1 PO1
PO12 PSO1 PSO2
s 1 2 3 4 5 6 7 8 9 0 1
(COs)
C01 3 3 2 2 1 1 2 3 3
C02 3 3 1 1 1 2 2
C03 3 3 1 2 2 1 3 2
C04 3 3 1 1 1 1 2
C05 2 2 1 1 1 2 1
5a Linear search 24
5b Binary search 29
6a Selection sort 35
6b Insertion sort 37
7 Merge sort 42
9 Multiply matrices 54
Programs that take command
10 59
line arguments (word count)
Find the most frequent words in
11
a text read from a file
64
Simulate elliptical orbits in
12
Pygame
70
Simulate bouncing ball using
13
Pygame
77
LAB-IN-CHARGE:
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
OUTCOME
PRE-REQUISITE
AIM:
To compute the Greatest Common Divisor of two numbers using python programming.
ALGORITHM:
PROGRAM:
def gcd(a,b) :
if(b==0):
return a
else:
return gcd(b,a%b)
a=int(input(Enter first number:))
b=int(input(Enter second number:))
GCD=gcd(a,b)
Print(GCD is: )
Print(GCD)
1
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
OUTPUT:
CONCLUSION:
Thus the python program to compute the Greatest Common Divisor of two numbers was
executed and the output is obtained.
2
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PRACTICE EXERCISES:
OBJECTIVE QUESTIONS:
1. Python is an example of
a) High level language b) Middle level language c) Low level language d) None
2. Python and Rexx is a type of
a) Logic based programming language b) Functional programming language
c) Interpreted programming language d) Object Oriented programming language
3. Which among the following is compiled programming language?
a) Q b) Python c) Clean d) Java
4. Python was developed by
a) Guido Van Rossum b) JohanDahl c) John George Kemeny d)Niklaus Wirth
5. Python is
a) Interactive b) Interpreted c) Only A d) Both A and B
VIVA QUESTIONS:
3
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
4
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
5
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
OBJECTIVE
OUTCOME
PRE-REQUISITE
AIM:
To find the square root of a number (Newtons method) using python programming
ALGORITHM:
PROGRAM:
6
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
OUTPUT:
CONCLUSION:
Thus the pyhton program to find square root of a number using newtons method is
executed and the output is obtained.
7
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PRACTICE EXERCISES
OBJECTIVE QUESTIONS:
8
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
VIVA QUESTIONS:
9
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
10
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
11
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
OBJECTIVE
OUTCOME
PRE-REQUISITE
AIM:
ALGORITHM:
PROGRAM:
12
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
print(exponent(n, m))
OUTPUT:
n value: 2
m value: 5
32
CONCLUSION:
Thus the python program to find Exponent of a given number using is executed and the
output is obtained.
13
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PRACTICE EXERCISES
OBJECTIVE QUESTIONS:
a) / b) // c) % d) None
a) 7 b) 1 c) 0 d) 5
14
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
VIVA QUESTIONS:
15
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
16
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
17
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
OBJECTIVE
OUTCOME
PRE-REQUISITE
AIM:
ALGORITHM:
18
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PROGRAM:
Lst=[]
num=int(input(How many numbers:))
For n in range(num):
Numbers=int(input(Enter number ))
Lst.append(Numbers)
Print(Maximum element in the list is:, max(Lst))
OUTPUT:
CONCLUSION:
Thus the python program to compute the Maximum numbers in the List was executed
and the output is obtained.
19
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PRACTICE EXERCISES:
OBJECTIVE QUESTIONS:
a=[1,2,3,None,(),[],]
print(len(a))
a) Syntax error b) 4 c) 5 d) 6
4. Which of the following data structures can be used with the "in" operator to check if an item is
in the data structure?
5. Which method is used in the List to find number of times a given item is present in it.
20
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
VIVA QUESTIONS:
1. Define List
2. Define Sort
3. Mention the use of Slip method in the List
4. What is the need for count method?
5. Compare List, tuple and Dictionary and mention its uses?
6. Why do we use Loop in the List?
7. Difference between Append and Extend
8. Mention some built in functions available in the List
9. Define the method for finding Maximum and Minimum element in the List
10. Define Indexing,Slicing and Matrices in the List
21
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
22
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
23
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
LINEAR SEARCH
OBJECTIVE
OUTCOME
Students become expertise to write a Python coding to implement the linear search.
PRE-REQUISITE
AIM:
ALGORITHM:
PROGRAM:
24
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
if items[i] == x:
flag = 1
break
i=i+1
if flag == 1:
print("item found at position:", i + 1)
else:
print("item not found")
OUTPUT:
CONCLUSION:
Thus the python program for the implementation of linear search was executed and
the output was obtained.
25
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PRACTICE EXERCISES:
OBJECTIVE QUESTIONS:
VIVA QUESTIONS:
26
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
27
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
28
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
BINARY SEARCH
OBJECTIVE
OUTCOME
Students become expertise to write a Python coding to implement the binary search.
PRE-REQUISITE
AIM:
ALGORITHM:
PROGRAM:
def binary_sort(sortedlist,n,x):
start = 0
end = n - 1
while(start <= end):
mid = (start + end)/2
29
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
if (x == sortedlist[mid]):
return mid
elif(x < sortedlist[mid]):
end = mid - 1
else:
start = mid + 1
return -1
sortedlist = []
for i in range(n):
sortedlist.append(input("Enter %dth element: "%i))
if(position != -1):
print("Entered number %d is present at position: %d"%(x,position))
else:
print("Entered number %d is not present in the list"%x)
OUTPUT:
CONCLUSION:
Thus the pyhton program for the implementation of binary search was executed and
output was obtained.
30
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PRACTICE EXERCISES
1. Write a python program to Read an Array and Search for an Element using binary search.
2. Write a python program using Recursion to Search an Element in Array using binary
search.
3. Develop a python program for implementing binary search on a sorted array of N
numbers.
4. Write a pyhton program to search the element 32 using binary search
76 45 98 32 12 65 27
OBJECTIVE QUESTIONS:
31
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
VIVA QUESTIONS:
1. What is binary?
2. What is binary search?
3. What are the techniques to implement binary search?
4. What are the operations in binary search?
5. What is binary searching?
6. Write the routine for binary search.
7. Search the given element using binary search
14 34 56 78 90 123
The key element is 90
8. Difference between linear search and binary search.
9. Write the advantage and disadvantage of binary search.
10. In what order binary search is performed.
32
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
33
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
34
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SELECTION SORT
OBJECTIVE
OUTCOME
PRE-REQUISITE
AIM:
ALGORITHM:
PROGRAM:
def ssort(aList):
for I in range( len( len(aList)):
least = i
for k in range(i+1,len(aList)):
if aList[k]<aList[least]:
least=k
swap(alilst,least ,i)
def swap(A,x,y):
35
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
tmp=A[x]
A[x]=A[y]
A[y]=tmp
a=[]
n=int(input(Enter Upper Limit:))
for I in range(n):
a.append(int(input()))
ssort(a)
print(After Sorting:.a)
OUTPUT:
CONCLUSION:
Thus the python program for the implementation of selection sort was executed and
the output was obtained.
36
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
INSERTION SORT
OBJECTIVE
OUTCOME
PRE-REQUISITE
AIM:
ALGORITHM:
37
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
def isort(a):
for index in range(1,len(a)):
currentvalue=a[index]
position=index
while position>0 and a[position-1]>currentvalue:
a[position]=a[position-1]
position=position-1
a[position]=currentvalue
a=[]
n=int(input(Enter Upper Limit:))
for i in range(n):
a.append(int(input()))
isort(a)
print(List afterinsert:,a)
OUTPUT:
EnterUpperLimit: 5
3
List after insert: [3]
2
List after insert: [2, 3]
5
List after insert: [2, 3, 5]
1
List after insert: [1, 2, 3, 5]
4
List after insert: [1, 2, 3, 4, 5]
CONCLUSION:
Thus the pyhton program for the implementation of insertion sort was executed and
output was obtained.
38
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PRACTICE EXERCISES
OBJECTIVE QUESTIONS:
VIVA QUESTIONS:
39
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
40
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
41
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
MERGE SORT
OBJECTIVE
OUTCOME
PRE-REQUISITE
AIM:
ALGORITHM:
42
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
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
alist = [13,2,17,76,82,12,7]
mergeSort(alist)
print(alist)
43
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
OUTPUT:
CONCLUSION:
Thus the python program for the implementation of merge sort was executed and
the output was obtained.
44
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PRACTICE EXERCISES
OBJECTIVE QUESTIONS:
VIVA QUESTIONS:
45
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
46
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
47
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
OBJECTIVE
OUTCOME
PRE-REQUISITE
AIM:
ALGORITHM:
48
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PROGRAM:
OUTPUT:
CONCLUSION:
Thus the python program for the implementation of prime number was executed and
the output was obtained.
49
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PRACTICE EXERCISES
OBJECTIVE QUESTIONS:
VIVA QUESTIONS:
50
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
51
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
52
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
53
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
MULTIPLY MATRICES
OBJECTIVE
OUTCOME
PRE-REQUISITE
AIM:
ALGORITHM:
54
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PROGRAM
a=[[1,2],
[3,4]]
b=[[1,2],
[3,4]]
c=[[0,0],
[0,0]]
for i in range(len(a)):
for j in range(len(b[0])):
for k in range(len(b)):
c[i][j] += a[i][k] *b[k][j]
print("Resultant Matrix")
for r in c:
print(r)
OUTPUT:
Resultant Matrix
[7, 10]
[15, 22]
CONCLUSION:
Thus the python program for the implementation of matrix multiplication was executed
and the output was obtained.
55
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PRACTICE EXERCISES:
OBJECTIVE QUESTIONS:
1. Which of the following loops will execute the body of loop even when condition controlling
the loop is initially false?
a) do-while b) while c) for d) None of the mentioned
2. Which of these statement is incorrect?
a) switch statement is more efficient than a set of nested ifs.
b) two case constants in the same switch can have identical values.
c) switch statement can only test for equality, whereas if statement can evaluate
any type of boolean expression.
d) it is possible to create a nested switch statements.
3. Applications of multidimensional array are?
a) Matrix-Multiplication
b) Minimum Spanning Tree
c) Finding connectivity between nodes
d) All of the mentioned
VIVA QUESTIONS:
56
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
57
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
58
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
OBJECTIVE
OUTCOME
PRE-REQUISITE
AIM:
ALGORITHM:
PROGRAM:
parser= argparse.ArgumentParser()
parser.add_argument("filename")
59
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
args= parser.parse_args()
if args.words:
elif args.lines:
else:
OUTPUT:
Word in a file 23
Lines in a file 1
CONCLUSION:
Thus the pyhton program to perform command line arguments (word/line counts) was
executed and output was obtained.
60
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PRACTICE EXERCISES
1. Write a program to find the most frequent words in a text read from a file.
2. Write a program to get a line with max word count from the given file.
3. Write a C Program to copy contents of a file to another file.
4. Write a program append text to a file and display the text
5. Write a python program to find the longest words.
6. Write a program to read a file line by line and store it into an array.
7. Write a Progam to illustrate the use of kwargs in python.
OBJECTIVE QUESTIONS:
VIVA QUESTIONS:
61
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
62
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
63
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
OBJECTIVE
OUTCOME
PRE-REQUISITE
AIM:
ALGORITHM:
64
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PROGRAM:
file=open("C:\\Python27\\dgct.txt","r")
wordcount={}
wordcount[word] = 1
else:
wordcount[word] += 1
print (word,wordcount)
file.close()
OUTPUT:
CONCLUSION:
Thus the python program for the implementation of words in a text read from a file was
executed and the output was obtained.
65
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PRACTICE EXERCISES
OBJECTIVE QUESTIONS:
1. Which of the following command is used to open a file in write mode only??
a) infile=open(c:\dgct.txt,r) b) infile=open(c:\\dgct.txt,r)
c) infile=open(file=c:\dgct.txt,r+) d) infile=open(file=c:\\dgct.txt,r)
2. . Which of the following command is used to read n number of charecters from a file using
file oblect<file>
a) file.read(n) b) n=file.read() c) file.readline(n) d) file.readlines()
3. . Which of the following command is used to read the entire contents of the file as a string
using file object<file>
a) tmpfile.read(n) b)tmpfile.read() c)tmpfile.readline()
d)tmpfile.readlines()
4. . Which of the following function do you use to write data in binary file.
a)write b)output c)dump d)send
5. The readlines() method returns
a) str b) a list of lines c) a list of single charecter
d) a list of integers
6. Which function is used to read all charecters?
a)Read() b)Readcharecteers() c)Readall() d)Readchar()
VIVA QUESTIONS:
66
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
67
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
68
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
69
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
OBJECTIVE
OUTCOME
Students become expertise to write a Python coding to implement the linear search.
PRE-REQUISITE
AIM:
ALGORITHM:
70
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Step 14 : Create a RED player block and used to manage how fast the screen updates.
Step 15 : See if the player block has collided with anything.
Step 16 : Stop.
PROGRAM:
import pygame
import random
import math
#colors
black = (0,0,0)
white = (255,255,255)
green = (0,255,0)
red = (255,0,0)
blue = (0,0,255)
class block(pygame.sprite.sprite):
def_init_(self,color,width,height):
super()._init_()
self.image = pygame.surface((width,height))
self.image.fill(color)
self.rect=self.image.get_rect()
self.center_x=0
self.center_y=0
self.angle=0
self.radius=0
self.speed=0.05
def update(self):
self.rect.x+self.radius * math.sin(self.angle)+ self.center_x
self.rect.y+self.radius * math.cos(self.angle)+ self.center_y
self.angle+=self.speed
class player(pygame.sprite.sprite):
class to represent the player.
def_init_(self,color,width,height):
create the player image.
super()._init_()
self.image=pygame.surface((width,height))
self.image.fill(color)
self.rect=self.image.get_rect()
def update(self);
71
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
pos=pygame.mouse.get_pos()
self.rect.x=pos(0)
self.rect.y=pos(1)
pygame.init()
screen_width=700
screen_height=400
screen=pygame.display.set_mode((screen_width,screen_height))
block_list=pygame.sprite.group()
all_sprites_list=pygame.sprite.group()
for i in range(50):
block=block(black,20,15)
block.center_x=random.radrange(screen_width)
block.center_y=random.radrange(screen_height)
block.radius=random.radrange(10,200)
block.angle=random.random()*2*math.pi
block.speed=0.008
block_list.add(block)
all_sprites_list.add(block)
player=player(red,20,15)
all_sprites_list.add(player)
done=false
clock=pygame.time.clock()
score=0
while not done:
for event in pygame.event.get():
if event.type==pygame.quit:
done=true
all_sprites_list.update()
screen.fill(white)
blocks_hit_list=pygame.sprite.spritecollide(player,block_list,true)
for block in blocks_hit_list:
score+=1
print(score)
all_sprites_list.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
72
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
OUTPUT:
CONCLUSION:
Thus the python program for the implementation elliptical orbits using Pygame is
obtained.
73
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PRACTICE EXERCISES:
OBJECTIVE QUESTIONS:
2. Mode which is used to open a file for both appending and reading
a) w+ b) a+ c) ab d) ab+
VIVA QUESTIONS:
74
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
75
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
76
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
OBJECTIVE
OUTCOME
PRE-REQUISITE
AIM
ALGORITHM
Step 1: Start
Step 2: Number of frames per second
Step 3: Change this value to speed up or slow down your game
Step 4: Global values to be used through the program
Step 5 : Set up the colors
Step 6 : Draw the arena the game will be played in
Step 7 : Draw outline of arena and center line
Step 8 : Draw the paddle and stop the paddle moving too low
Step 9 : draw and move the ball and returns new position
Step 10 : Checks for collision with the wall, and bounces ball off it and returns new
direction
Step 11 : Initiate variable and set starting positions and any future changes made within
rectangles
Step 12: Create rectangles for ball and paddles
Step 13 : Draws the starting position of the arena and make cursor invisible.
Step 14 : Mouse movement commands are produced
Step 15 : Stop
77
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PROGRAM:
Shapes.py
main.py
screen = Screen()
board = Turtle()
board.speed(0)
board.ht()
ball = Turtle()
ball.speed(0)
screen.register_shape("ball", ((0, 0), (0, 5), (5, 5), (5, 0)))
ball.shape("ball")
ball.penup()
ball.setx(100)
ball.sety(100)
78
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
return (r1x < r2x + r2w) and (r1x + r2w > r2x) and (r1y < r2y + r2h) and (r1h + r1y > r2y)
dx = 1
dy = 1.5
while True:
ball.setx(ball.xcor() + dx)
ball.sety(ball.ycor() + dy)
79
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
OUTPUT:
CONCLUSION:
Thus the python program for the implementation of Simulate bouncing ball using Pygame
was executed successfully.
VIVA QUESTIONS:
1. What is pygame?
2. Mention the applications of pygame.
3. List the library files that python uses for designing.
4. Can pygame applications run on android phones?
5. Mention some alternative for pygame library.
80