Python Lab Manual Final(R-21)
Python Lab Manual Final(R-21)
1 DEVELOPING FLOWCHART
Date:
AIM:
To draw a flow chart for to calculate the electricity bill
FLOWCHART: Start
Take Unit
Value
Yes
Amount=Unit*0
Unit<=100
S_charge=Amount*0.20
No
S_
Yes
Amount=25-(Unit-50)*0.75
Unit<=150
S_charge=Amount*0.20
No
Yes Amount=100-(Unit-150)*1.20
Unit<=250
S_charge=Amount*0.20
No
Amount=220+(Unit+250)*1.50
Total Amount=Amount+s_charge
Total
Amount
Stop 1
1.(b) RETAIL SHOP BILLING:
Start
Input
itemcost,quantity
Input rate
Yes
Rate>1000 Tax=Rate*10
No
Tax=Rate*5
Output item
cost,tax,itemcost+
tax
Stop
2
𝒙𝟑 𝒙𝟓 𝒙𝟕
1.(C). SINE SERIES (Sin x = x- 𝟑! + - 𝟕! + ………):
𝟓!
Start
Read X,n
X=X*3.142/180
t=X
Sum=X
Loop
No
i<=n
Yes
t=(-t*x*x)/(2*i*(2*i+1))
Sum=sum+t
i=i+1
Print sum
Stop
3
1.(d) WEIGHT OF THE STEEL BAR:
Start
Read
Density D,
Volume V
Compute Weight=D*V
Display
Weight
Stop
RESULT:
Thus the flowchart for the given problem was created successfully.
4
Ex.No.2(A) SIMPLE STATEMENTS AND EXPRESSIONS -
EXCHANGE THE VALUES OF TWO VARIABLES
Date:
AIM:
ALGORITHM:
PROGRAM:
5
OUTPUT:
RESULT:
Thus the Python program to exchange the given values of two variables was
created and executed successfully.
6
Ex.No.2(B) CIRCULATE THE VALUES OF N VARIABLES
Date :
AIM:
ALGORITHM:
PROGRAM:
list1=[]
list1.append(ele)
ele=list1.pop(0)
list1.append(ele)
print(list1)
7
OUTPUT:
Enter integer : 3
Enter integer : 5
Enter integer : 7
[5, 7, 3]
[7, 3, 5]
[3, 5, 7]
RESULT:
Thus the Python program to circulate the given values of n variables was created
and executed successfully.
8
Ex.No.2(C) DISTANCE BETWEEN TWO POINTS
Date :
AIM:
ALGORITHM:
PROGRAM:
9
OUTPUT:
enter x1 : 4
enter x2 : 5
enter y1 : 6
enter y2 : 2
RESULT:
Thus the program was written and executed to calculate distance between two
points.
10
Ex.No.2(D) DEMONSTRATE DIFFERENT NUMBER DATA TYPES
Date :
AIM:
ALGORITHM:
PROGRAM:
a=5
a = 2.0
a = 1+2j
11
OUTPUT:
RESULT:
Thus the program was written and executed to demonstrate different number
data types.
12
Ex.No.2(E) DIFFERENT ARITHMETIC OPERATIONS ON NUMBERS
Date :
AIM:
ALGORITHM:
PROGRAM:
num1 = int(input('Enter First number: '))
13
print('Product of' ,num1 ,'and' ,num2 ,'is :',mul)
OUTPUT:
Sum of 9 and 5 is : 14
Difference of 9 and 5 is : 4
Product of 9 and 5 is : 45
Modulus of 9 and 5 is : 4
RESULT:
Thus the program was written and executed to perform different arithmetic
14
Ex.No.2(F) TO PRINT THE CURRENT DATE
Date :
AIM:
To write a python script to print the current date in the fallowing format”Sun
May 29 02:26:23 IST 2017”
ALGORITHM:
time.asctime(time.localtime(time.time()))
Step 4: Print the localtime.
Step 7: Stop the program.
PROGRAM:
import time;
localtime = time.asctime(time.localtime(time.time()))
15
OUTPUT:
RESULT:
Thus the python program was written and executed to print the
current date in the fallowing format”Sun May 29 02:26:23 IST 2017”.
16
Ex.No.2(G) TO CONVERT TEMPERATURE IN CELSIUS TO FAHRENHEIT
Date :
AIM:
ALGORITHM:
PROGRAM:
(celsius,fahrenheit))
17
OUTPUT:
RESULT:
Thus the python program was written and executed to convert temperature in
Celsius to Fahrenheit.
18
Ex.No.3(A) CONDITIONALS AND ITERATIVE LOOPS -
FIND THE LARGEST NUMBER AMONG THE THREE NUMBERS
Date :
AIM:
To write a Python program to find the largest number among the three input
numbers.
ALGORITHM:
Step 4: The above conditions are false, again check (num2 >= num1) and
(num2 >= num3) then set largest = num2
Step 5: Else set largest = num3.
Step 6: Print the largest value.
Setp 7: Stop the program.
19
PROGRAM:
largest = num1
largest = num2
else:
largest = num3
OUTPUT:
RESULT:
Thus a Python program to find the largest number among the three input
numbers was created and executed successfully.
20
Ex.No.3(B) CONSTRUCT THE PATTERN
Date :
AIM:
To write a python program to construct the given patter, using a nested for loop.
ALGORITHM:
PROGRAM:
def run():
j=7
k=7
p=1
for i in range(8):
print( " " * k," #" * i)
k -=1
while j > 1:
j -= 1
print (" " * p," #" * j)
p+=1
run()
21
OUTPUT:
##
###
####
#####
######
#######
######
#####
####
###
##
RESULT:
Thus a Python program to construct the pattern, using a nested for loop was
created and executed successfully.
22
Ex.No.3(C) PRINT PRIME NUMBERS LESS THAN 20
Date :
AIM:
ALGORITHM:
PROGRAM:
Start = 1
n = int(input("Enter the number: "))
print("Prime numbers between", Start, "and", n, "are:")
for num in range(Start, n + 1):
if num > 1:
for i in range(2, int(num/2)+1):
if (num % i) == 0:
break
else:
print(num)
23
OUTPUT:
11
13
17
19
RESULT:
Thus a Python program to print prime numbers less than 20 was created and
executed successfully.
24
Ex.No.4(A) LIST AND LIST OPERATIONS
Date :
AIM:
ALGORITHM:
PROGRAM
25
# Deleting element from list in python example
print("\nDELETING ELEMENT FROM LIST:")
print("===========================")
print("Elements of the list, list1 are:");
print(list1)
index = input("\nEnter index no:")
index = int(index)
print("Deleting the element present at index number",index)
del list1[index]
print("\nNow elements of the list, list1 are:")
print(list1)
OUTPUT :
CREATE A LIST:
==============
Elements of the lists, list1,list2 and list3 are:
['computer', 'programming', 1957, 2070, 3242]
[1, 2, 3, 4, 5]
['a', 'b', 'c', 'd', 'e']
LIST CONCATENATION:
===================
List's items after concatenating:
['computer', 'programming', 1957, 2070, 3242, 1, 2, 3, 4, 5]
RESULT:
Thus a program to create, append and remove lists in python was created and
executed successfully.
26
Ex.No.4(B) DEMONSTRATE WORKING WITH TUPLE
Date :
AIM:
ALGORITHM:
PROGRAM
27
print("\nInserting some items to the tuple...")
print("============================")
tp = ("my parents", "my friend", "my brother", "my sister")
print("\nPrinting the tuple tp...")
print(tp)
OUTPUT :
RESULT:
Thus a program to demonstrate working with tuples in python was created and
executed successfully.
28
Ex.No.5 DEMONSTRATE WORKING WITH DICTIONARIES
Date :
AIM:
PROGRAM:
print("\nACCESS ELEMENTS...")
print("==================")
print(dict2['name'])
print(dict2.get(1))
29
print(dict3)
print("\nDelete or remove elements...")
print("==========================")
print(dict3.pop(3))
print(dict3)
print(dict3.popitem())
print(dict3)
del dict3
print(dict3)
OUTPUT :
ACCESS ELEMENTS...
==================
John
[2, 4, 3]
RESULT:
30
Ex.No.6 FACTORIAL USING FUNCTION
Date :
AIM:
ALGORITHM:
PROGRAM
def factorial(n):
if n == 1:
return n
else:
return n*factorial(n-1)
31
OUTPUT 1:
OUTPUT 2:
RESULT:
Thus, a python program to find the factorial of a number using recursion was
created and executed successfully.
32
Ex.No.7(a) CREATE, CONCATENATE AND PRINT A STRING AND
ACCESSING SUB-STRING
Date :
AIM:
To write a program to create, concatenate and print a string and accessing sub-
string from given string.
ALGORITHM:
PROGRAM
#create string
print("STRING CREATION")
print("===============")
mystring = 'Hello'
print(mystring)
mystring = "Hello"
print(mystring)
my_string = '''Hello'''
print(my_string)
33
# triple quotes string can extend multiple lines
mystring = """Hello, welcome to
the world of Python"""
print(my_string)
# using +
print('str1 + str2 = ', str1 + str2)
# using *
print('str1 * 3 =', str1 * 3)
#PRINT STRING
print("PRINTING STRING")
print("===============")
str = 'programiz'
print('str = ', str)
#first character
print('str[0] = ', str[0])
#last character
print('str[-1] = ', str[-1])
34
OUTPUT:
STRING CREATION
===============
Hello
Hello
Hello
Hello
PRINTING STRING
===============
str = programiz
str[0] = p
str[-1] = z
str[1:5] = rogr
str[5:-2] = am
RESULT:
Thus a python program to create, concatenate and print a string and accessing
sub-string from given string was created and executed successfully.
35
Ex.No.8 DEFINE A MODULE AND IMPORT A SPECIFIC FUNCTION
Date:
AIM:
ALGORITHM:
PROGRAM i):
def add(a,b):
result=a+b
return result
36
OUTPUT: >>> import sample >>> sample.add(12,5) 17
PROGRAM ii):
import math
OUTPUT:
RESULT:
Thus a python program to define a module and import a specific function in that
module to another program was created and executed successfully.
37
Ex.No.:9 COPY FROM ONE FILE TO ANOTHER
Date:
AIM:
To write a Python program to copy one file to another using File handling.
ALGORITHM:
PROGRAM:
import shutil
shutil.copy2('D:/PY PGM/file1.txt', 'D:/PY PGM/file2.txt')
38
OUTPUT:
>>>
RESTART: C:/Users/AppData/Local/Programs/Python/Python36-32/filecopy.py
File Copy Done
file1.txt
Hai how are you?
this is problem solving and python
programming lab.
file2.txt
Hai how are you?
this is problem solving and python
programming lab.
RESULT:
Thus a Python programs to copy one file to another using File handling was
created and executed successfully.
39
Ex.No.: 10 DIVIDE BY ZERO ERROR
Date:
AIM:
ALGORITHM:
PROGRAM:
except ZeroDivisionError:
print ("Infinite: Divided by 0")
else:
print (c)
40
OUTPUT 1:
OUTPUT 2:
RESULT:
Thus a Python program to raising divide by zero error using Exception handling
was created and executed successfully.
41
Ex.No.11 EXPLORING PYGAME TOOL
Date:
AIM:
ALGORITHM:
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((255,255,255))
pygame.draw.circle(screen,(0,0,255),(250,250),75)
pygame.display.flip()
pygame.quit()
42
OUTPUT :
RESULT:
Thus a Python program for exploring pygame tool was created and executed
successfully.
43
Ex.No.12 SIMULATE BOUNCING BALL USING PYGAME
Date :
AIM:
To write a Python program to simulate bouncing ball using pygame.
ALGORITHM:
Step 1: Start
Step 2: Import necessary GUI for simulation
Step 3: Set the window coordinates and the ball coordiates
Step 4: Assign various colors for the ball and set the base color
Step 5: Fix the color, shape and bouncing speed randomly
Step 6: Write a function to move the base according to the ball position
Step 7: Stop
PROGRAM:
import sys, pygame
pygame.init()
size = width, height = 800, 400
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:
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]
44
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
screen.fill(background)
screen.blit(ball, ballrect)
pygame.display.flip()
OUTPUT :
RESULT:
Thus a Python program to simulate bouncing ball using pygame was simulated
successfully.
45