PSPP Lab Manual Updated
PSPP Lab Manual Updated
Aim:
Identification and solving of simple real life or scientific or technical problems
(Electricity Billing, Retail shop billing, Sin series) and developing flow charts for the same.
Flowchart:
1a. Electricity Billing – Flowchart
- Available in Book (Pg. No. 204)
1b. Sin Series – Flowchart
- Available in Book (Pg. No. 206)
1c. Retail shop Billing – Flowchart
Conclusion:
Thus the identification and solving of simple real life or scientific or technical problems
(electricity billing, retail shop billing, and sin series) and developing flow charts has been
developed and verified.
Experiment 2: Python programming using simple statements and expressions (exchange the
values of two variables, circulate the values of n variables, distance between two points).
Aim:
To write a Python program using simple statements and expressions (exchange the
values of two variables, circulate the values of n variables, distance between two points).
Output:
Enter value for P: 2
Enter value for Q: 3
The Value of P after swapping: 3
The Value of Q after swapping: 2
Program:
a,b,c,d=10,20,30,40
print("Before circulating")
print("a = ",a)
print("b = ",b)
print("c = ",c)
print("d = ",d)
t=a
a=d
d=c
c=b
Prepared by: Mr. Umapathy.M AP-CSE ,TKEC Page 2 of 25
GE3171 – Problem Solving and Python Programming Laboratory
b=t
print("After circulating")
print("a = ",a)
print("b = ",b)
print("c = ",c)
print("d = ",d)
Output:
a= 40
b = 10
c = 20
d = 30
Program:
print("Enter the first point")
x1=int(input("Enter the value for x1 "))
y1=int(input("Enter the value for y1 "))
print("Enter the second point")
x2=int(input("Enter the value for x2 "))
y2=int(input("Enter the value for y2 "))
a=(x1-x2)**2
b=(y1-y2)**2
distance=(a+b)**0.5
print("Distance = ",distance)
Output:
Enter the first point
Enter the value for x1 12
Enter the value for y1 14
Enter the second point
Enter the value for x2 15
Enter the value for y2 16
Distance = 3.605551275463989
Conclusion:
Thus the above Python programs using simple statements and expressions (exchange
the values of two variables, circulate the values of n variables, distance between two points)
has been executed and verified.
Experiment3: Scientific problems using Conditionals and Iterative loops. (Number series,
Number Pattern, Pyramid pattern)
Aim:
To write a Python program for scientific problems using Conditionals and Iterative
loops (Number series, Number Pattern, Pyramid pattern).
Program:
n=int(input("Enter the Number: "))
sum=0
i=1
while i<=n:
sum=sum+i
i=i+1
print("Sum of Numbers(1 to ",n,") is : ",sum)
Output:
Enter the Number: 10
Sum of Numbers (1 to 10 ) is : 55
Output:
Enter the Number: 5
*
**
***
****
*****
Output:
Enter the Number: 5
2
22
222
2222
22222
Conclusion:
Thus the above Python programs for scientific problems using Conditionals and
Iterative loops (Number series, Number Pattern, Pyramid pattern) has been executed and
verified.
Aim:
To write a Python programs to implement real-time/technical application (Items
present in a library) using Lists and Tuples.
Program:
books=['C', 'C++','Web Technology','PYTHON','Maths']
print("Currently No of books available: ",len(books))
pos1 = int(input("Enter the pos to insert: "))
element = input("Enter the element to insert: ")
books.insert(pos1,element)
print("After insertion updated book list is","\n",books)
print("Currently No of books available: ",len(books))
pos2 = int(input("Enter the pos to delete: "))
books.pop(pos2)
print("After deletion updated list is","\n",books)
print("Currently No of books available: ",len(books))
books.sort()
print("After sorting the book title", books)
Output:
Currently No of books available: 5
Enter the pos to insert: 2
Enter the element to insert: Networks
After insertion updated book list is
['C', 'C++', 'Networks', 'Web Technology', 'PYTHON', 'Maths']
Currently No of books available: 6
Enter the pos to delete: 1
After deletion updated list is
['C', 'Networks', 'Web Technology', 'PYTHON', 'Maths']
Currently No of books available: 5
After sorting the book title ['C', 'Maths', 'Networks', 'PYTHON', 'Web Technology']
Algorithm:
Step 1: Start
Step 2: Initialize the Value in the Tuple
Step 3: find the length of Tuple
Step 4: Print the length of the book by using len ()
Step 5: Print the Maximum price of the book by using max ()
Step 6: Print the Minimum price of the book by using min ()
Step 7: Stop
Program:
books=('C', 'C++', 'JAVA', 'C')
bookPrice=(1200,500, 1250)
print("Currently No of books available: ",len(books))
print("Maximum Book Price is ",max(bookPrice))
print("Minimum Book Price is ",min(bookPrice))
print("Enter book name to find how many copies of specified book available in the library")
countBook =input()
print(books.count(countBook))
Output:
Currently No of books available: 4
Maximum Book Price is 1250
Minimum Book Price is 500
Enter book name to find how many copies of specified book available in the library
C
2
Conclusion:
Thus the above Python programs to implement real-time/technical application (Items
present in a library) using Lists and Tuples has been executed and verified.
Aim:
To write a Python program to implement real-time/technical application using Sets and
Dictionaries for Components of an Automobile.
Program:
auto={'Engine':'V Engine','Clutch':'Diaphragm','Chassis':'Tubular','Gearbox':'Synchromesh'}
print("Length is: ",len(auto))
print("Type of Engine is: ",auto["Engine"])
x=auto.get("Clutch")
print("Type of Clutch is: ",x)
k=auto.keys()
print("The Keys are: ",k)
auto["Brakes"]="Drum Brakes"
print("The Keys are: ",k)
v=auto.values()
print("The Values are: ",v)
item=auto.items()
print("Items are: ")
print(item)
Output:
Length is: 4
Type of Engine is: V Engine
Type of Clutch is: Diaphragm
The Keys are: dict_keys(['Engine', 'Clutch', 'Chassis', 'Gearbox'])
The Keys are: dict_keys(['Engine', 'Clutch', 'Chassis', 'Gearbox', 'Brakes'])
Algorithm:
Step 1: Start
Step 2: Create a Set called auto1 and auto2 with components of automobile.
Step 3: Use add( ) method, to add an Item.
Step 4: Use remove( ) method, to remove an Item.
Step 5: Use union( ) method, to join two sets.
Step 6: Use pop( ) method, to remove the first element.
Step 7: Stop.
Program:
auto1={"Engine","Clutch","Gearbox","Engine"}
auto2={"V Engine","Diaphragm","Synchromesh"}
print("Items are: ")
print(auto1) #Duplicate values will be ignored
print(auto2)
auto1.add("Axle")
print("After Addition Items are: ")
print(auto1)
auto1.remove("Engine")
print("After Removal Items are: ")
print(auto1)
auto=auto1.union(auto2)
print("After Union: ")
print(auto)
auto.pop()
print("After Pop: ")
print(auto)
Output:
Items are:
{'Engine', 'Gearbox', 'Clutch'}
{'Diaphragm', 'Synchromesh', 'V Engine'}
After Addition Items are:
{'Axle', 'Engine', 'Gearbox', 'Clutch'}
After Removal Items are:
{'Axle', 'Gearbox', 'Clutch'}
After Union:
{'Diaphragm', 'V Engine', 'Gearbox', 'Axle', 'Synchromesh', 'Clutch'}
After Pop:
{'V Engine', 'Gearbox', 'Axle', 'Synchromesh', 'Clutch'}
Conclusion:
Thus the above Python programs to implement real-time/technical application using
Sets and Dictionaries for Components of an Automobile has been executed and verified.
Aim:
To write a Python program to implement the concept of functions for Factorial, largest
number in a list, area of shape.
Program:
def factorial(n):
fact=1
i=1
while i<=n:
fact=fact*i
i=i+1
print("Factorial Value of",n,"is :",fact)
n=int(input("Enter the Number to find the Factorial Value: "))
factorial(n)
Output:
Enter the Number to find the Factorial Value: 5
Factorial Value of 5 is: 120
Algorithm:
Step 1: Start
Step 2: Create a list of numbers.
Step 3: Use len( ), to obtain the length of the list.
Step 4: Use sort( ), to sort in ascending order.
Step 5: Print the largest number in a list.
Step 6: Stop.
Program:
list=[12,45,67,33,10,98,78,90,55]
x=len(list)
list.sort()
print("Numbers in List are: ")
print(list)
print("The Largest Number in the List is: ")
print(list[x-1])
Output:
Numbers in List are:
[10, 12, 33, 45, 55, 67, 78, 90, 98]
The Largest Number in the List is:
98
Program:
def calculate_area(name):
name = name.lower()
if name == "rectangle":
l = int(input("Enter rectangle's length: "))
b = int(input("Enter rectangle's breadth: "))
rect_area = l * b
print("The area of rectangle is: ",rect_area)
elif name == "square":
Output:
Calculate Shape Area
Enter the name of shape whose area you want to find: rectangle
Enter rectangle's length: 2
Enter rectangle's breadth: 5
The area of rectangle is: 10
Conclusion:
Thus the above Python programs to implement the concept of functions for Factorial,
largest number in a list, area of shape has been executed and verified.
Algorithm:
Step 1: Start
Step 2: Use slice operator [::-1] to reverse the string.
Step 3: Use replace( ),to replace a substring. Step
4: Compare strings and print the result. Step 5:
Stop.
Program:
txt="Computer Science"[::-1]
print(txt)
print("Number of Occurrences of Character e: ",txt.count("e"))
t="Computer Science"
x=t.replace("Computer","Social")
print(x)
t="radar"
b=(t==t[::-1])
if b:
print("Radar is a Palindrome String")
else:
print("Not Palindrome")
Output:
Conclusion:
Thus the above python program to implement the concept of Strings (Reverse,
Character Count, Replacing characters and Palindrome) has been executed and verified.
Experiment 8: Implementing programs using written modules and Python Standard Libraries
(pandas, numpy. Matplotlib, scipy).
Aim:
To write a Python program to implement the concept of Modules and Python Standard
Libraries (pandas, numpy, Matplotlib, scipy).
Program:
#Save the below code as calc.py
def add(x, y):
return (x+y)
def subtract(x, y):
return (x-y)
Output:
Addition: 12
Subtraction: 23
Program:
import pandas as pd
import matplotlib.pyplot as plt
data = {"Pass%": [90, 70, 50], "Students_Count": [20, 50, 30]}
df = pd.DataFrame(data, index = ["Test1", "Test2", "Test3"])
df.plot(kind = 'bar', x = 'Pass%', y = 'Students_Count')
plt.show()
Output:
Conclusion:
Thus the above Python program to implement the concept of Modules and Python
Standard Libraries (pandas, numpy, Matplotlib, scipy) has been executed and verified.
Aim:
To write a Python program to implement the real-time/technical applications using File
Handling (Copy from one file to another, word count, longest word).
Algorithm:
Step 1: Start
Step 2: Using open( ), open a file.
Step 3: Using read( ), read the content of a file.
Step 4: Using close( ), close the opened file.
Step 5: Stop
Output:
Enter source file name: first.txt
Enter destination file name: second.txt
File copied successfully
Given two text files, the task is to write a Python program to copy contents of the first file into the
second file.
The text files which are going to be used are second.txt and first.txt
Output:
Content of first.txt file:
Given two text files, the task is to write a Python program to copy contents of the first file into the
second file.
The text files which are going to be used are second.txt and first.txt
Number of words in the text file is: 36
Output:
Longest word in the file is:
['Communication']
Conclusion:
Thus the above Python program to implement the real-time/technical applications using File
Handling (Copy from one file to another, word count, longest word) has been executed and
verified.
Aim:
To write a Python program to implement real time / technical applications using Exception
handling (Divide by Zero error, voter’s age validity).
Output:
Enter a: 12
Enter b: 0
Can't divide by zero
Division by zero
Output:
Enter your Age: 25
Eligible to Vote
>>>
Enter your Age: 14
Not eligible to Vote
>>>
Enter your Age: 2f
Age must be a valid Number
>>>
Conclusion:
Thus the above Python program to implement real time / technical applications using
Exception handling (Divide by Zero error, voter’s age validity) has been executed and verified.
Aim:
To write a Python program to explore Pygame Tool.
Algorithm:
Step 1: Start.
Step 2: Import and Initialize Pygame. The command pygame.init() starts all modules that
need initialization inside pygame.
Step 3: Set screen size using pygame.display.set_mode to create an area for the game
window at the size of 500 X 500 pixels.
Step 4: Command fill is used to fill the screen color.
Step 5: Command pygame.display.flip is used to make the drawing visible to the user.
Step 6: Stop.
Program:
import pygame
pygame.init()
screen = pygame.display.set_mode([500, 500])
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0,0,0))
pygame.draw.circle(screen, (255, 255, 255), (250, 250), 75)
pygame.display.flip()
pygame.quit()
Output:
Conclusion:
Thus the above Python program to explore Pygame tool has been executed and verified.
Experiment 12: Developing a game activity using Pygame like bouncing ball.
Aim:
To write a Python program to develop a game activity using Pygame like bouncing ball.
Algorithm:
Step 1: Import and Initialize Pygame.
Step 2: Set screen size, background color and caption.
Step 3: Load the moving object and set the rectangle area covering the image.
Step 4: Set speed of the moving object.
Step 5: Make ball movement continuity.
Step 6: Fill the background color and blit the screen.
Step 7: Make image visible.
Step 8: Stop.
Program:
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import sys, pygame
from pygame.locals import *
pygame.init()
speed = [1, 1]
color = (255, 250, 250)
width = 550
height = 300
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Pygame bouncing ball")
ball = pygame.image.load(“Ball.png")
rect_boundry = ball.get_rect()
while 1:
for event in pygame.event.get():
rect_boundry = rect_boundry.move(speed)
if rect_boundry.left < 0 or rect_boundry.right > width:
speed[0] = -speed[0]
if rect_boundry.top < 0 or rect_boundry.bottom > height:
speed[1] = -speed[1]
screen.fill(color)
screen.blit(ball, rect_boundry)
pygame.display.flip()
if event.type == QUIT:
pygame.quit()
sys.exit()
Output
Conclusion:
Thus the above Python program to develop a game activity using Pygame like bouncing ball
has been developed and verified.