Ge3171-Pspp Lab Manual Final
Ge3171-Pspp Lab Manual Final
Regulation- 2021
LAB MANUAL
I Year / I Semester
(Common to all Branches)
1
GE3171 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
1. Identification and solving of simple real life or scientific or technical problems, and developing
flow charts for the same. (Electricity Billing, Retail shop billing, Sin series, weight of a motorbike,
Weight of a steel bar, compute Electrical Current in Three Phase AC Circuit, etc.)
2. Python programming using simple statements and expressions (exchange the values of two
variables, circulate the values of n variables, distance between two points).
3. Scientific problems using Conditionals and Iterative loops. (Number series, Number Patterns,
pyramid pattern)
4. Implementing real-time/technical applications using Lists, Tuples. (Items present in a
library/Components of a car/ Materials required for construction of a building –operations of list
& tuples)
5. Implementing real-time/technical applications using Sets, Dictionaries. (Language, components
of an automobile, Elements of a civil structure, etc.- operations of Sets & Dictionaries)
6. Implementing programs using Functions. (Factorial, largest number in a list, area of shape)
7. Implementing programs using Strings. (Reverse, palindrome, character count, replacing
characters)
8. Implementing programs using written modules and Python Standard Libraries (pandas, numpy.
Matplotlib, scipy)
9. Implementing real-time/technical applications using File handling. (copy from one file to another,
word count, longest word)
10. Implementing real-time/technical applications using Exception handling. (divide by zero error,
voter’s age validity, student mark range validation)
11. Exploring Pygame tool.
12. Developing a game activity using Pygame like bouncing ball, car race etc.
Total Hours: 60
2
INDEX
EX. NO. NAME OF THE EXPERIMENT PAGE NO.
a. Electricity Billing 1
a. Number Series 23
3
b. Number Pattern 26
c. Pyramid Pattern 29
4 a. Operations of Lists 32
b. Operations of Tuples 35
5 a. Operations of Sets 38
b. Operations of Dictionaries 41
Functions
1
String Operations 53
a. Reversing a String 56
7
b. Checking Palindrome in a String 59
Exception Handling
11 Exploring Pygame 98
2
EX.NO: 1. a
ELECTRICITY BILLING
AIM:
To write a python program for implementing electricity billing.
ALGORITHM:
STEP 2 :Get the total units of current consumed by the customer using the variable unit.
STEP 3:If the unit consumed less or equal to 100 units, calculates the total amount of consumed
payAmount=0
STEP 4: If the unit consumed between 100 to 200 units, calculates the total amount of
consumedPayAmount=(units-100)*2.25
STEP 5:If unit consumed between 200 to 400 units ,calculates total amount of consumed
PayAmount=(100*2.25)+(200*4.5)+(units-400)*6.
STEP 6: If unit consumed between 400-600 units ,calculates total amount of consumed=
PayAmount payAmount=(100*2.25)+(200*4.5)+(100*6)+(units-500)*8
STEP 7: If the unit consumed above 600 units,calculates total amount of consumed=
=(100*2.25)+(200*4.5)+(100*6)+(200*8)+(units-600)*10
1
FLOW CHART:
START
IF
Units<=100
payAmount=0
elif
unit<=200
PayAmount=
(units-100)*2.25
elif
unit<=400
PayAmount=(100*2.25)+(200
*4.5)+(units-400)*6
elif
unit<=600
payAmount=(100*2.25)+(200*4.5)+(
100*6)+(units-500)*8
elif payAmount
unit>600 =(100*2.25)+(200*4.5)+(100*6)+(200*
8)+(units-600)*10
PayAmount=0
STOP
2
OUTPUT:
Please enter the number of units you consumed in a month: 120
Electricity bill=200.00
3
PROGRAM:
units=int(input("Please enter the number of units you consumed in a month : "))
if(units<=100):
payAmount=units*1.5
elif(units<=200):
payAmount=(100*1.5)+(units-100)*2.5
elif(units<=300):
payAmount=(100*1.5)+(200-100)*2.5+(units-200)*4
elif(units<=350):
payAmount=(100*1.5)+(200-100)*2.5+(300-200)*4+(units-300)*5
else:
payAmount=1500
Total=payAmount;
print("\nElectricity bill=%.2f" %Total)
RESULT:
Thus, the python program for implementing electricity billing is executed and the
output is obtained.
4
EX.NO: 1. b
RETAIL SHOP BILLING
AIM:
To write a python program for implementing retail shop billing.
ALGORITHM:
FLOWCHART:
START
Assign tax=0.18
Assign rate={“Pencil”:10,”Pen”:20,
”Scale”:7,”A4Sheet”:150}
Read quantity of
all items from
Calculate
cost=Pencil*Rate[“Pencil”]+Pen*Rate[“Pen”]+
Scale*Rate[“Scale”]+A4Sheet*Rate[A4Sheet]
Calculate Bill=cost+cost*tax
Print Bill
STOP
5
OUTPUT:
Retail Bill Calculator
Enter the quantity of the ordered items:
Pencil:5
Pen:2
Scale:2
A4Sheet:1
Please pay Rs.299.720000
6
PROGRAM:
tax=0.18
Rate={"Pencil":10,"Pen":20,"Scale":7,"A4Sheet":150}
print("Retail Bill Calculator\n")
print("Enter the quantity of the ordered items:\n")
Pencil=int(input("Pencil:"))
Pen=int(input("Pen:"))
Scale=int(input("Scale:"))
A4Sheet=int(input("A4Sheet:"))
cost=Pencil*Rate["Pencil"]+Pen*Rate["Pen"]+Scale*Rate["Scale"]+A4Sheet*Rate["A4S
heet"]
Bill=cost+cost*tax
print("Please pay Rs.%f"%Bill)
RESULT:
Thus, the python program for implementing retail shop billing is executed and the
output is obtained.
7
EX.NO: 1.c
WEIGHT OF A STEEL BAR
AIM:
To write a python program to calculate weight of a steel bar.
ALGORITHM:
FLOWCHART:
START
Read
Diameter
Calculate
Weight=(D*D)/162
STOP
8
OUTPUT:
Calculating weight of a steel bar:
Enter the Diameter of Steel bar:250
Weight of the steel bar is 385.000000 Kg/m
9
PROGRAM:
RESULT:
Thus, the python program for calculating the weight of the steel is executed and the
output is obtained.
10
EX.NO: 1. d
COMPUTE ELECTRICAL CURRENT IN 3 PHASE AC CIRCUIT
AIM:
To write a python program to calculate electrical current in 3 phase AC circuit.
ALGORITHM:
FLOWCHART:
START
Read Volts,
Amperes
Calculate
power=√3 * Volts*Amperes
Print power
STOP
11
OUTPUT:
Calculating electrical current of 3 phase AC circuit:
Enter the volts value:56
Enter the amperes value:85
Power = 8244.561844 KVA
12
PROGRAM:
import math
print("Calculating electrical current of 3 phase AC circuit:\n")
volts=int(input("Enter the volts value:"))
amperes=int(input("Enter the amperes value:"))
power=math.sqrt(3)*volts*amperes
print("Power = %f KVA" %power)
RESULT:
Thus, the python program to compute electrical current in 3 phase AC circuit is executed
and the output is obtained.
13
EX.NO: 2. a PROGRAMS USING SIMPLE STATEMENTS EXCHANGE THE VALUES OF
TWO VARIABLES
AIM:
To write a python program to exchange the values of two variables.
ALGORITHM:
14
OUTPUT:
Enter value of X 85
Enter value of Y 63
Before exchange of x,y
('x =', 85)
('Y= ', 63)
After exchange of x,y
('x =', 63)
('Y= ', 85)
15
PROGRAM:
def exchange(x,y):
x,y=y,x
print("After exchange of x,y")
print("x =",x)
print("Y= ",y)
x=input("Enter value of X ")
y=input("Enter value of Y ")
print("Before exchange of x,y")
print("x =",x)
print("Y= ",y)
exchange(x,y)
RESULT:
Thus, the python program to exchange the values of two variables is executed and the
output is obtained.
16
EX.NO: 2. b
CIRCULATE THE VALUES OF N VARIABLES
AIM:
To write a python program to circulate the values of n variables.
ALGORITHM:
17
OUTPUT:
Enter n: 5
('Circulation', 1, '=', [92, 93, 94, 95, 91])
('Circulation', 2, '=', [93, 94, 95, 91, 92])
('Circulation', 3, '=', [94, 95, 91, 92, 93])
('Circulation', 4, '=', [95, 91, 92, 93, 94])
('Circulation', 5, '=', [91, 92, 93, 94, 95])
18
PROGRAM:
def circulate(A,N):
for i in range(1,N+1):
B=A[i:]+A[:i]
print("Circulation",i,"=",B)
return
A=[91,92,93,94,95]
N=int(input("Enter n:"))
circulate(A,N)
RESULT:
Thus, the python program to circulate the values of n variables is executed and the output
is obtained.
19
EX.NO: 2. c DISTANCE BETWEEN TWO VARIABLES
AIM:
To write a python program to calculate distance between 2 variables.
ALGORITHM:
20
OUTPUT:
Enter a x1: 50
Enter a y1: 50
Enter a x2: 320
Enter a y2: 320
Distance = 381.84
21
PROGRAM:
import math
x1 = int(input("Enter a x1: "))
y1 = int(input("Enter a y1: "))
x2 = int(input("Enter a x2: "))
y2 = int(input("Enter a y2: "))
distance = math.sqrt(((x2-x1)**2)+((y2-y1)**2))
print("Distance = %.2f"%distance)
RESULT:
Thus, the python program to circulate the distance between two variables is executed and
the output is obtained.
22
EX.NO: 3. a PROGRAMS USING CONDITIONALS & ITERATIVE LOOPS
NUMBER SERIES
AIM:
To write a python program to calculate 12+22+32+…. +N2.
ALGORITHM:
23
OUTPUT:
Enter a number: 5
('Sum = ', 55)
24
PROGRAM:
RESULT:
Thus, the python program to calculate 12+22+32+…. +N2 is executed and the output is
obtained.
25
EX.NO: 3. b
NUMBER PATTERN
AIM:
To write a python program to print number pattern.
ALGORITHM:
26
OUTPUT:
Number Pattern Printing
Enter no. of rows to print: 5
1
12
123
1234
12345
27
PROGRAM:
RESULT:
Thus, the python program to program to print number pattern is executed and the output
is obtained.
28
EX.NO: 3. c
PYRAMID PATTERN
AIM:
To write a python program to print number pattern.
ALGORITHM:
STEP 1: Start the program
STEP 2: Read the number of rows to print from user
STEP 3: Print the pyramid pattern using for loops
STEP 4: Stop the program
29
OUTPUT:
30
PROGRAM:
RESULT:
Thus, the python program to program to print pyramid pattern is executed and the output
is obtained.
31
EX.NO: 4. a
OPERATIONS OF LISTS
AIM:
ALGORITHM:
STEP 1: Start.
STEP 2: Declare a list variable named library and assign values to the list.
STEP 9: Remove specific element from the list using remove () function.
STEP 10: Print the total number of elements in the list using count () function
32
OUTPUT:
library = ['books', 'magazines', 'documents', 'e-books']
library list after append: ['books', 'magazines', 'documents', 'e-books', 'musical books']
index of 'magazines': 1
33
PROGRAM:
library=['books','magazines','documents','e-books']
print("library = ", library)
print("first element = ", library[0])
print("Third element = ", library[2])
print("All Items in library= ", library[0:3])
print("-4th element = ", library[-4])
library.append('musical books')
print("library list after append: ", library)
print("index of \'magazines': ", library.index('magazines'))
library.sort()
print("after sorting: ",library)
print('popped elements is: ',library.pop())
print("after pop: ",library)
library.remove('e-books')
print("after removing '\e-books': ",library)
library.insert(2,'CDs')
print('number of elements in library list =',library.count('documents'))
RESULT:
Thus, the python program to verify the operations in the list is executed and the output
is obtained.
34
EX.NO: 4. b
OPERATIONS OF TUPLES
AIM:
ALGORITHM:
STEP 1: Start.
STEP 2: Declare a tuple variable named car and assign values to the Tuple.
STEP 6: Print the index value in the Tuple using index () function.
STEP 7: Print the total number of elements in the Tuple using count () function.
STEP 8: Print the length of elements in the Tuple using len () function.
STEP 9: Stop.
35
OUTPUT:
Components of a car: ('engine', 'battery', 'alternator', 'radiator', 'steering', 'break', 'seat belt')
index of 'radiator': 3
36
PROGRAM:
car=('engine','battery','alternator','radiator','steering','break','seat belt')
print("Components of a car: ", car)
print("first element = ", car[0])
print("Third element = ", car[2])
print("Components of a car from 0 to 4 index= ", car[0:4])
print("-7th element : ", car[-7])
print("index of \'radiator': ", car.index('radiator'))
print('Total number of elements in a car Tuple: ',car.count('steering'))
print("length of elements in a car Tuple: ", len('seat belt'))
RESULT:
Thus, the python program to verify the operations in the Tuples is executed and the
Output is obtained.
37
EX.NO: 5. a
OPERATIONS OF SETS
AIM:
ALGORITHM:
STEP 1: Start.
STEP 2: Declare a set variables named L1, L2 and assign values to the variables of the set.
STEP 7: Stop.
38
OUTPUT:
39
PROGRAM:
L1={'pitch','syllabus','scripts','grammar','sentences'}
L2={'grammar','syllabus','context','Words','Phonetics'}
print("union of L1 and L2 is: ", L1 | L2)
print("Intersection of L1 and L2 is: ",L1&L2 )
print("Difference of L1 and L2 is: ", L1-L2)
print("Symmetric Difference of L1 and L2 is: ", L1^L2)
RESULT:
Thus, the python program to verify the operations in the Sets is executed and the
output is obtained.
40
EX.NO: 5. b
OPERATIONS OF DICTIONARIES
AIM:
ALGORITHM:
STEP 1: Start.
STEP 2: Declare a Dictionary variable named thisdict and assign values to the variable.
STEP 7: Print the key value using array and using get() function.
STEP 9: Stop.
41
OUTPUT:
42
PROGRAM:
RESULT:
Thus, the python program to verify the operations in the Dictionaries is executed and
the output is obtained.
43
EX.NO: 6. a
FACTORIAL OF A NUMBER USING FUNCTION
AIM:
ALGORITHM:
STEP 1: Start.
STEP 6: Stop.
44
OUTPUT:
Enter a number 5
The factorial of 5 is 120
45
PROGRAM:
def fact(n):
if n==1:
return n
else:
return n*fact(n-1)
num=int(input("Enter a number "))
print("The factorial of ", num,"is", fact(num))
RESULT:
Thus, the python program to find the factorial of a number using function is executed
and the output is obtained.
46
EX.NO: 6. b
FINDING LARGEST NUMBER IN A LIST USING FUNCTION
AIM:
To write a python program to find the largest number in a list using functions.
ALGORITHM:
STEP 1: Start.
STEP 4: Read the set of values into variable ‘elements ‘using FOR loop.
STEP 6: Print the maximum value of the list using max() function.
STEP 7: Stop.
47
OUTPUT:
Enter number of Elements in list: 3
Enter the elements: 1
Enter the elements: 2
Enter the elements: 3
Largest element is: 3
48
PROGRAM:
def myMax(list1):
print("Largest element is : ",max(list1))
list1=[]
num=int(input("Enter number of Elements in list: "))
for i in range(0,num):
element=int(input("Enter the elements: "))
list1.append(element)
print("Largest element in the list is", myMax(list1))
RESULT:
Thus, the python program to find the largest number in a list using functions is
executed and the output is obtained.
49
EX.NO: 6. c
FINDING AREA OF A CIRCLE USING FUNCTION
AIM:
ALGORITHM:
STEP 1: Start.
STEP 2: Define a function ‘findarea’ with the parameter ‘r’ as findarea (r).
STEP 6: Print the area of circle by calling the function findarea (num).
STEP 7: Stop.
50
OUTPUT:
enter the r value7
Area is 153.958
51
PROGRAM:
def findarea(r):
PI=3.142
return PI*(r*r)
RESULT:
Thus, the python program for finding the area of a circle using functions is executed
and the output is obtained.
52
EX.NO: 7. a
REVERSING A STRING
AIM:
ALGORITHM:
STEP 1: Start.
STEP 2: Define a function ‘reverse’ with the parameter ‘string’ as reverse (string).
STEP 7: Stop.
53
OUTPUT:
Enter any string: Tendulkar
The original String is: Tendulkar
The reversed string (using reversed) is: rakludneT
54
PROGRAM:
def reverse(string):
string="".join(reversed(string))
return string
s=input("Enter any string: ")
print("The original String is: ",end=" ")
print(s)
print("The reversed string(using reversed)is: ",end= " ")
print(reverse(s))
RESULT:
Thus, the python program to reverse a string using reverse function is executed and
the output is obtained.
55
EX.NO: 7. b
CHECKING PALINDROME IN A STRING
AIM:
ALGORITHM:
STEP 1: Start.
STEP 2: Read the input string into string.
STEP 3: Call the functionreverse (s).
STEP 4: Reverse the string using the function reversed () and assign the value to
rev_string.
STEP 5: Compare string and rev_string.
STEP 6: If both strings are equal print palindrome or print not palindrome.
STEP 7: Stop.
56
OUTPUT:
Enter a string: madam
It is Palindrome
57
PROGRAM:
RESULT:
Thus, the python program to find the given string is palindrome or not is executed
and the output is obtained.
58
EX.NO: 7. c
COUNTING CHARACTERS IN A STRING
AIM:
ALGORITHM:
STEP 1: Start.
STEP 3: Read the input character included in the string into char.
STEP 4: Calculate the number of character in a string using count() function and store into
val.
STEP 6: Stop.
59
OUTPUT:
Enter any string: computer
Enter a character to count: r
1
60
PROGRAM:
RESULT:
Thus, the python program to count the number of characters from the given string is
executed and the output is obtained.
61
EX.NO: 7. d
REPLACE CHARACTERS IN A STRING
AIM:
ALGORITHM:
STEP 1: Start.
STEP 3: Read the old and new input strings into str1 and str2.
STEP 6: Stop.
62
OUTPUT:
Enter any string: heat and mass transfer
Enter old string: mass
Enter new string: volume
heat and volume transfer
63
PROGRAM:
RESULT:
Thus, the python program to replace the characters from the given string is executed
and the output is obtained.
64
INSTALLING AND EXECUTING PYTHON PROGRAM IN ANACONDA
NAVIGATOR
65
STEP 4: Click new python3.
66
STEP 5: Write or paste the python code.
67
EX.NO: 8. a COMPARE THE ELEMENTS OF TWO PANDAS SERIES USING PANDAS
LIBRARY
AIM:
To write a python program to compare the elements of two pandas series using pandas
library.
ALGORITHM:
STEP 1: Start.
STEP 5: Print the equal value, less than value and greater than value in the series by
comparison.
STEP 6: Stop.
68
OUTPUT:
series1:
0 2
1 4
2 6
3 8
4 10
dtype: int64
series2:
0 1
1 3
2 5
3 7
4 10
dtype: int64
Compare the elements of the series s1 and s2
Equals:
0 False
1 False
2 False
3 False
4 True
dtype: bool
Greater than:
0 True
1 True
2 True
3 True
4 False
dtype: bool
Less than:
0 False
1 False
2 False
3 False
4 False
dtype: bool
69
PROGRAM:
import pandas as pd
ds1=pd.Series([2,4,6,8,10])
ds2=pd.Series([1,3,5,7,10])
print("series1: ")
print(ds1)
print("series2: ")
print(ds2)
print("Compare the elements of the series s1 and s2")
print("Equals: ")
print(ds1==ds2)
print("Greater than: ")
print(ds1>ds2)
print("Less than: ")
print(ds1<ds2)
RESULT:
Thus, the python program to compare the elements of two pandas series using pandas
library is executed and the output is obtained.
70
EX.NO: 8. b PROGRAM TO TEST WHETHER NONE OF THE ELEMENTS OF A
GIVEN ARRAY IS ZERO USING NUMPY LIBRARY
AIM:
To write a python program to test whether none of the elements of a given array is zero
using numpy library.
ALGORITHM:
STEP 1: Start.
STEP 3: Assign non zero values in the array using numpy object into x.
STEP 5: Assign including zero value in the array using numpy object into x.
STEP 7: Stop.
71
OUTPUT:
Original array:
[1 2 3 4]
Test if none of the elements of the array is zero:
True
Original array:
[0 1 2 3]
Test if none of the elements of the array is zero:
False
72
PROGRAM:
import numpy as np
x=np.array([1,2,3,4])
print("Original array: ")
print(x)
print("Test if none of the elements of the array is zero: ")
print(np.all(x))
x=np.array([0,1,2,3])
print("Original array: ")
print(x)
print("Test if none of the elements of the array is zero: ")
print(np.all(x))
RESULT:
Thus, the python program to test whether none of the elements of a given array is
zero using numpy library is executed and the output is obtained.
73
EX.NO: 8. c
PROGRAM TO PLOT A GRAPH USING MATPLOTLIB LIBRARY
AIM:
ALGORITHM:
STEP 1: Start.
STEP 3: Assign non zero values in the array using numpy object into x.
STEP 5: Assign including zero value in the array using numpy object into x.
STEP 7: Stop.
74
OUTPUT:
75
PROGRAM:
RESULT:
Thus, the python program to plot a graph using matplotlib library is executed and the output
is obtained.
76
EX.NO: 8. d PYTHON PROGRAM TO RETURN THE SPECIFIED UNIT IN SECONDS
USING SCIPY LIBRARY
AIM:
To write a python program to return the specified unit in seconds (e.g. hour returns 3600.0)
using scipy library.
ALGORITHM:
STEP 1: Start.
STEP 3:print minute, hour, day, week, year, Julian year using constants.
STEP 4: Stop.
77
OUTPUT:
60.0
3600.0
86400.0
604800.0
31536000.0
31557600.0
78
PROGRAM:
RESULT:
Thus, the python program to return the specified unit in seconds using scipy library is
executed and the output is obtained.
79
EX.NO: 9. a
COPY FROM ONE FILE TO ANOTHER
AIM:
To write a python program to copy from one file to another using shutil.
ALGORITHM:
STEP 1: Start.
STEP 4: Copy the contents from source to destination using copyfile () function.
STEP 6: Stop.
80
OUTPUT:
Enter source file name: nandha.txt
81
PROGRAM:
RESULT:
Thus, the python program to copy from one file to another using shutil is executed and the
output is obtained.
82
EX.NO: 9. b
WORD COUNT FROM A FILE
AIM:
ALGORITHM:
STEP 1: Start.
STEP 6: Stop.
83
OUTPUT:
Number of words in text file is: 30
84
PROGRAM:
file=open("d:\data.txt")
data=file.read()
words=data.split()
print("Number of words in text file is: ",len(words))
RESULT:
Thus, the python program to count number of words in a file is executed and the output is
obtained.
85
EX.NO: 9. c
FINDING LONGEST WORD IN A FILE
AIM:
ALGORITHM:
STEP 1: Start.
STEP 3: Read input from the file to words using split() function.
STEP 4: Calculate the maximum length of words using len() function and max() function.
STEP 6: Stop.
86
OUTPUT:
Longest_word: [‘hippopotamus’]
87
PROGRAM:
def longest_word(filename):
with open(filename,'r')asinfile:
words=infile.read().split()
max_len=len(max(words,key=len))
return[word for word in words if len(word)==max_len]
print(longest_word("d:\deva.txt"))
RESULT:
Thus, the python program to find longest word in a file is executed and the output is
obtained.
88
EX.NO: 10. a
A DIVIDE BY ZERO ERROR USING EXCEPTION HANDLING
AIM:
To write a python program to handle divide by zero error using exception handling.
ALGORITHM:
STEP 1: Start.
STEP 6: Stop.
89
OUTPUT:
Enter the value of n: 2
Enter the value of d: 4
Enter the value of c: 5
quotient: -2.0
90
PROGRAM:
RESULT:
Thus, the python program to check voter’s age validity is executed and the output is
obtained.
91
EX.NO: 10. b
VOTERS AGE VALIDITY
AIM:
ALGORITHM:
STEP 1: Start.
STEP 5: Check with the condition that age is less than or equal to 18.
STEP 7: Stop.
92
OUTPUT:
In which year u took birth: 1996
Your current age is: 25
You are eligible to vote.
93
PROGRAM:
import datetime
year_of_birth=int(input("In which year u took birth..."))
current_year=datetime.datetime.now().year
current_age=current_year-year_of_birth
print("Your current age is: ",current_age)
if(current_age<=18):
print("you are not eligible to vote. ")
else:
print("you are eligible to vote. ")
RESULT:
Thus, the python program to check voter’s age validity is executed and the output is
obtained.
94
EX.NO: 10. c
STUDENT MARK RANGE VALIDATION
AIM:
ALGORITHM:
STEP 1: Start.
STEP 3: Check the input value is less than 0 or greater than hundred.
STEP 5: Stop.
95
OUTPUT:
Enter the mark95
The mark is in the range...
96
PROGRAM:
RESULT:
Thus, the python program to perform Student mark range validation is executed and the
output is obtained.
97
EX.NO: 11
EXPLORING PYGAME
AIM:
STEPS:
1. Install python 3.6.2 into C:\
https://www.pygame.org/download.shtml
98
Collecting Pygame
C:\>cd Python36-32\Scriptspygame-1.9.3
RESULT:
Thus, Pygame module is installed and verified.
99
EX.NO: 12
SIMULATE BOUNCING BALL USING PYGAME
AIM:
ALGORITHM:
STEP 1: Start
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 13: Draws the starting position of the arena and make cursor invisible.
101
PROGRAM:
import sys, pygame
pygame.init()
size = width, height = 700, 250
speed = [1, 1]
background = 255, 255, 255
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Bouncing ball")
ball = pygame.image.load("ball.jpg")
ballrect = ball.get_rect()
while 1:
pygame.time.delay(2)
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()
RESULT:
Thus, the python program for bouncing ball in Pygame is executed and the output is
obtained.
102