Python Manual Record (1)
Python Manual Record (1)
Python Manual Record (1)
Program No: 1A
Ex. No: DEVELOPING FLOWCHARTS FOR ELECTRICITY BILLING
Date:
AIM:
ALGORITHM:
Tariff rates in TN
L = [[0],[0,1.5],[0,2,3],[0,3.5,4.6,6.6]]
Bill = 0
Bill = 0
elif(Unit<=200):
Bill = (Unit-100)*L[1][1]
elif(Unit<=500):
Bill = 100*L[2][1]+(Unit-200)*L[2][2]
else:
Bill = 100*L[3][1]+300*L[3][2]+(Unit-500)*L[3][3]
OUTPUT:
Unit : 500
RESULT:
lOMoAR cPSD| 45433932
Program No: 1B
Ex. No: DEVELOPING FLOWCHARTS FOR RETAIL SHOP BILLING
Date:
AIM:
ALGORITHM:
FLOW CHART:
lOMoAR cPSD| 45433932
tax=0.5
Rate={"SHIRT":250,"SAREE":650,"TEASHIRT":150,"TRACKPANT":150}
SHIRT=int(input("SHIRT:"))
SAREE=int(input("SAREE:"))
TEASHIRT=int(input("TEASHIRT:"))
TRACKPANT=int(input("TRACKPANT:"))
cost=SHIRT*Rate["SHIRT"]+SAREE*Rate["SAREE"]+TEASHIRT*Rate["TEASHIRT"]+TRACK
PANT*Rate["TRACKPANT"]
Bill=cost+cost*tax
OUTPUT:
SHIRT:1
SAREE:1
TEASHIRT:1
TRACKPANT:0
Result:
lOMoAR cPSD| 45433932
Date:
AIM:
ALGORITHM:
FLOWCHART:
START
Read
Diameter
Calculate
Weight=(D*D)/162
STOP
lOMoAR cPSD| 45433932
Program for calculating Weight of a Steel Bar and motor bike in Python
OUTPUT:
RESULT:
lOMoAR cPSD| 45433932
Date:
AIM:
ALGORITHM:
FLOWCHART:
START
Read
Watts,
Calculate
power=√3 * Volts*Amperes
STOP
lOMoAR cPSD| 45433932
PROGRAM:
import math
P = eval(input("Enter power (in Watts) : "))
pf = eval(input("Enter power factor (pf<1): "))
OUTPUT:
python current.py
RESULT:
lOMoAR cPSD| 45433932
AIM:
ALGORITHM:
PROGRAM:
print("x =",x)
print("Y= ",y)
print("x =",x)
print("Y= ",y)
OUTPUT:
Enter value of X: 67
Enter value of Y: 56
lOMoAR cPSD| 45433932
RESULT:
lOMoAR cPSD| 45433932
Program No: 2B
Ex. No: PROGRAM TO CIRCULATE THE VALUES OF N VARIABLES
Date:
AIM:
ALGORITHM:
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)
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])
RESULT:
lOMoAR cPSD| 45433932
Program No: 2C
Ex. No: PROGRAM TO CALCULATE DISTANCE BETWEEN TWO VARIABLES
Date:
AIM:
ALGORITHM:
PROGRAM:
import math
x1 = int(input("x1 = "))
y1 = int(input("y1 = "))
x2 = int(input("x2 = "))
y2 = int(input("y2 = "))
OUTPUT:
x1 = 50
y1 = 758
lOMoAR cPSD| 45433932
x2 = 25
y2 = 50
RESULT:
lOMoAR cPSD| 45433932
Program No: 3A
Ex. No: PROGRAMS USING CONDITIONALS AND ITERATIVE LOOPS
Date: NUMBER SERIES
AIM:
2
+22+32+….+N2.
ALGORITHM:
PROGRAM:
sum=0
i=1
while i<=n:
sum=sum+i*i
i+=1
print('Sum = ',sum)
OUTPUT:
Enter a number: 10
Sum = 385
RESULT:
lOMoAR cPSD| 45433932
Program No: 3B
Ex. No: PROGRAMS USING CONDITIONALS AND ITERATIVE LOOPS
Date: NUMBER PATTERN
AIM:
ALGORITHM:
PROGRAM:
for n in range(1,N+1,1):
print()
OUTPUT:
Number Triangle :
1
22
333
4444
55555
RESULT:
lOMoAR cPSD| 45433932
AIM:
ALGORITHM:
PROGRAM:
for n in range(1,N+1,1):
print(" "*(N-n),end="")
print(" * "*((2*n)-1))
OUTPUT:
** *
** * * *
** * * * * *
** * * * * * * *
RESULT:
lOMoAR cPSD| 45433932
Program No: 4A
Ex. No: PYTHON PROGRAM DEMOSTRATING OPERATIONS OF LIST
Date:
AIM:
ALGORITHM:
PROGRAM:
library =['Books','Periodicals','Newspaper','Manuscripts','Maps','Prints','Documents','Ebooks']
print('Library: ',library)
library.append('Audiobooks')
library.sort()
library.remove('Maps')
lOMoAR cPSD| 45433932
library.insert(2, 'CDs')
OUTPUT:
('Items in Library from 0 to 4 index: ', ['Books', 'Periodicals', 'Newspaper', 'Manuscripts', 'Maps'])
('Library list after append Audiobooks: ', ['Books', 'Periodicals', 'Newspaper', 'Manuscripts',
'Maps', 'Prints', 'Documents', 'Ebooks', 'Audiobooks'])
('after sorting: ', ['Audiobooks', 'Books', 'Documents', 'Ebooks', 'Manuscripts', 'Maps', 'Newspaper',
'Periodicals', 'Prints'])
('after pop(): ', ['Audiobooks', 'Books', 'Documents', 'Ebooks', 'Manuscripts', 'Maps', 'Newspaper',
'Periodicals'])
('after insert CDs: ', ['Audiobooks', 'Books', 'CDs', 'Documents', 'Ebooks', 'Manuscripts',
'Newspaper', 'Periodicals'])
RESULT:
lOMoAR cPSD| 45433932
Program No: 4B
Ex. No: PYTHON PROGRAM DEMOSTRATING OPERATIONS OF TUPLE
Date:
AIM:
ALGORITHM:
PROGRAM:
Car_Auxiliaries = ['car lightening system','wiper and washer system','power door locks system','car
instrument panel','electric windows system','car park system']
def fnPrint(L):
for ele in L:
if(isinstance(ele,dict)):
for k, v in (ele.items()):
continue
print("\t",ele)
fnPrint(Car_Main_components)
print("Car Auxiliaries:")
fnPrint(Car_Auxiliaries)
lOMoAR cPSD| 45433932
OUTPUT:
Chassis
Engine
Car Auxiliaries:
RESULT:
Program No: 4C
Ex. No: PYTHON PROGRAM DEMOSTRATING OPERATIONS OF LISTS
Date: AND TUPLE
AIM:
To write a python program to implement operations of list and tuple for a civil structure example.
ALGORITHM:
Bricks
Blocks
Wooden Products
Natural Stones
Doors
Windows
Modular Kitchen
Sand
Aggregates
Electrical materials
Tiles
lOMoAR cPSD| 45433932
ElectricalMaterials = ('Conduit Pipes and Fittings', 'Wires and Cables', 'Modular switches and
sockets', 'Electric Panels', 'Switch Gear')
# Slicing
# length of tuple
# Concat, repetition
print("Concatenation operation")
AllMaterials = BuildingMaterials+ElectricalMaterials
print("All materials")
print(AllMaterials)
print("Repetition operation")
print(AllMaterials*2)
# Membership operator
if SearchMaterial in AllMaterials:
else:
# Iteration operation
print("Iteration operation")
print(mat)
OUTPUT:
Created tuple :
Building Materials : ('Cement', 'Bricks', 'Blocks', 'Wooden Products', 'Hardware and Fixtures',
'Natural Stones', 'Doors', 'Windows', 'Modular Kitchen', 'Sand', 'Aggregates', 'Tiles')
Electrical Materials : ('Conduit Pipes and Fittings', 'Wires and Cables', 'Modular switches and
sockets', 'Electric Panels', 'Switch Gear')
First 5 elements in tuple : ('Cement', 'Bricks', 'Blocks', 'Wooden Products', 'Hardware and
Fixtures')
Length of tuple : 12
Concatenation operation
All materials
lOMoAR cPSD| 45433932
('Cement', 'Bricks', 'Blocks', 'Wooden Products', 'Hardware and Fixtures', 'Natural Stones', 'Doors',
'Windows', 'Modular Kitchen', 'Sand', 'Aggregates', 'Tiles', 'Conduit Pipes and Fittings', 'Wires and
Cables', 'Modular switches and sockets', 'Electric Panels', 'Switch Gear')
Repetition operation
('Cement', 'Bricks', 'Blocks', 'Wooden Products', 'Hardware and Fixtures', 'Natural Stones', 'Doors',
'Windows', 'Modular Kitchen', 'Sand', 'Aggregates', 'Tiles', 'Conduit Pipes and Fittings', 'Wires and
Cables', 'Modular switches and sockets', 'Electric Panels', 'Switch Gear', 'Cement', 'Bricks', 'Blocks',
'Wooden Products', 'Hardware and Fixtures', 'Natural Stones', 'Doors', 'Windows', 'Modular Kitchen',
'Sand', 'Aggregates', 'Tiles', 'Conduit Pipes and Fittings', 'Wires and Cables', 'Modular switches and
sockets', 'Electric Panels', 'Switch Gear')
RESULT:
Thus the program to Materials required for construction of a building has been executed
successfully.
lOMoAR cPSD| 45433932
Program No: 5A
Ex. No: PYTHON PROGRAM DEMOSTRATING OPERATIONS OF SET
Date:
AIM:
ALGORITHM:
PROGRAM:
motorbike={'Engine','Fuel tank','Wheels','Gear','Break'}
#union operation
#intersection operation
#difference operation
#Symmetric difference
OUTPUT:
('Components of Car: ', set(['Engine', 'Alternator', 'Radiator', 'Seat Belt', 'Battery', 'Break',
'Steering']))
('Union of car and motorbike: ', set(['Engine', 'Alternator', 'Gear', 'Radiator', 'Seat Belt', 'Battery',
'Fuel tank', 'Break', 'Wheels', 'Steering']))
('Symmetric difference operation: ', set(['Alternator', 'Gear', 'Radiator', 'Seat Belt', 'Battery', 'Fuel
tank', 'Wheels', 'Steering']))
RESULT:
Thus the program to Components of an automobile of a building has been executed successfully.
lOMoAR cPSD| 45433932
Program No: 5B
Ex. No: PYTHON PROGRAM DEMOSTRATING OPERATIONS OF
Date: DICTIONARY
AIM:
ALGORITHM:
PROGRAM:
building={1:'foundation',2:'floor',3:'beams',4:'columns',5:'roof',6:'stair'}
#Elements in dictionary
#length of a dictionary
building.update({6:'lift'})
building[7]='window'
building.pop(3)
OUTPUT:
('Elements in dictionary building: ', {1: 'foundation', 2: 'floor', 3: 'beams', 4: 'columns', 5: 'roof', 6:
'stair'})
('After updation of stair as lift: ', {1: 'foundation', 2: 'floor', 3: 'beams', 4: 'columns', 5: 'roof', 6: 'lift'})
('After adding window: ', {1: 'foundation', 2: 'floor', 3: 'beams', 4: 'columns', 5: 'roof', 6: 'lift', 7:
'window'})
('After removing element beams from building: ', {1: 'foundation', 2: 'floor', 4: 'columns', 5: 'roof', 6:
'lift', 7: 'window'})
RESULT:
Thus the program to elements of a civil structure has been executed successfully.
lOMoAR cPSD| 45433932
Program No: 6A
Ex. No: PYTHON PROGRAM USING FUNCTIONS FOR FINDING
Date: FACTORIAL OF A NUMBER
AIM:
ALGORITHM:
PROGRAM:
def fnfact(N):
if(N==0):
return 1
else:
return(N*fnfact(N-1))
OUTPUT
Enter Number : 5
Factorial of 5 is 120
Result:
Program No: 6B
Ex. No: PYTHON PROGRAM FOR FINDING LARGEST NUMBER IN A LIST
Date:
AIM:
ALGORITHM:
PROGRAM:
def maximum(list):
return max(list)
list=[]
print("Enter",n,"elements")
i=0
for i in range(0,n):
element=int(input())
list.append(element)
OUTPUT:
('Enter', 5, 'elements')
65
78
52
99
56
RESULT:
Thus the program to finding largest number in a list has been executed successfully.
lOMoAR cPSD| 45433932
Program No: 6C
Ex. No: PYTHON PROGRAM FOR FINDING AREA OF SHAPES
Date:
AIM:
ALGORITHM:
PROGRAM:
# Functions
# Area of a shape
def fnSquare(s):
return (s*s)
def fnRectangle(l,b):
return (l*b)
def fnCircle(r):
return 3.142*r*r
def fnTriangle(base,ht):
return 0.5*base*ht
# square
OUTPUT:
Area of square = 25
Area of rectangle = 25
RESULT:
Thus the program Area of shapes in a list has been executed successfully.
lOMoAR cPSD| 45433932
Program No: 7 A
Ex. No: PYTHON PROGRAM TO FIND THE REVERSE OF A STRING
Date:
AIM:
ALGORITHM:
PROGRAM:
s=input("Enter a string:")
OUTPUT:
RESULT:
Program No: 7 B
Ex. No: PYTHON PROGRAM FOR CHECKING PALINDROME IN A STRING
Date:
AIM:
ALGORITHM:
PROGRAM:
s1=input("Enter a string:")
s2=s1[::-1]
if list(s1)==list(s2):
print("It is a palindrome")
else:
OUTPUT:
It is not a palindrome
It is a palindrome
RESULT:
Thus the program checking palindrome in a string has been executed successfully.
lOMoAR cPSD| 45433932
Program No: 7 C
Ex. No: PYTHON PROGRAM FOR COUNTING CHARACTER IN A STRING
Date:
AIM:
ALGORITHM:
PROGRAM:
x = txt.count(char)
OUTPUT:
Enter a string: "I love python programming, python is very interesting subject"
RESULT:
Thus the program counting character in a string has been executed successfully.
lOMoAR cPSD| 45433932
Program No: 7 D
Ex. No: PYTHON PROGRAM FOR REPLACING CHARACTERS IN A
Date: STRING
AIM:
ALGORITHM:
PROGRAM:
print(string.replace(str1, str2))
OUTPUT:
RESULT:
Thus the program Replacing characters in a string has been executed successfully.
lOMoAR cPSD| 45433932
Program No: 8 A
Ex. No: PYTHON PROGRAM FOR DEMOSTRATING OPERATIONS USING
Date: PANDAS
AIM:
To write a python program to compare the elements of the two Pandas Series using Pandas library.
ALGORITHM:
PROGRAM:
import pandas as pd
print("Series1:")
print(ds1)
print("Series2:")
print(ds2)
print("Equals:")
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
Equals:
0 False
1 False
2 False
3 False
4 True
dtype: bool
Greater than:
lOMoAR cPSD| 45433932
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
RESULT:
Thus the program Replacing characters in a string has been executed successfully.
lOMoAR cPSD| 45433932
AIM:
To write a program to test whether none of the elements of a given array is zero using
NumPy library.
ALGORITHM:
PROGRAM:
import numpy as np
x = np.array([1, 2, 3, 4])
print("Original array:")
print(x)
print(np.all(x))
x = np.array([0, 1, 2, 3])
print("Original array:")
print(x)
print(np.all(x))
OUTPUT:
Original array:
[1 2 3 4]
Original array:
[0 1 2 3]
RESULT:
Thus the program Replacing characters in a string has been executed successfully.
lOMoAR cPSD| 45433932
AIM:
To write a python program to plot a graph using matplotlib library.
ALGORITHM:
PROGRAM:
import numpy as np
plt.plot(xpoints, ypoints)
plt.show()
lOMoAR cPSD| 45433932
OUTPUT:
RESULT:
.
lOMoAR cPSD| 45433932
AIM:
To write a python program to return the specified unit in seconds (eg. Hour returns 3600.0)using
scipy library.
ALGORITHM:
PROGRAM:
print('1 minute=',constants.minute,'seconds')
print('1 hour=',constants.hour,'seconds')
print('1 day=',constants.day,'seconds')
print('1 week=',constants.week,'seconds')
print('1 year=',constants.year,'seconds')
OUTPUT:
RESULT:
.
lOMoAR cPSD| 45433932
AIM:
To write a python program to copy from one file to another.
ALGORITHM:
PROGRAM:
copyfile(sourcefile, destinationfile)
c=open(destinationfile, "r")
print(c.read())
c.close()
print()
print()
lOMoAR cPSD| 45433932
OUTPUT:
ROSE
JASMINE
SUN FLOWER
RESULT:
Thus the program copy from one file to another has been executed successfully
.
lOMoAR cPSD| 45433932
AIM:
Data.txt
A file is a collection of data stored on a secondary storage device like hard disk. They can be
easily retrieved when required. Python supports two types of files. They are Text files & Binary files.
ALGORITHM:
PROGRAM:
data = file.read()
words = data.split()
OUTPUT:
RESULT:
Thus the program word count from a file has been executed successfully
lOMoAR cPSD| 45433932
Program No: 9 C
Ex. No: PYTHON PROGRAM FOR FINDING LONGEST WORD IN A FILE
Date:
AIM:
Data.txt
A file is a collection of data stored on a secondary storage device like hard disk. They can be easily
retrieved when required. Python supports two types of files. They are Text files & Binary files.
ALGORITHM:
PROGRAM:
def longest_word(filename):
with open(filename, 'r') as infile:
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('F:\Data.txt'))
OUTPUT:
['collection']
RESULT:
Thus the program finding longest word in a file has been executed successfully
lOMoAR cPSD| 45433932
AIM:
To write a python program to handle divide by zero error using exception handling.
ALGORITHM:
PROGRAM:
try:
q=n/(d-c)
print("Quotient:",q)
except ZeroDivisionError:
print("Division by Zero!")
lOMoAR cPSD| 45433932
OUTPUT:
Division by Zero!
('Quotient:', 4)
RESULT:
Thus the program handle divide by zero error using exception handling has been executed
successfully
lOMoAR cPSD| 45433932
AIM:
ALGORITHM:
import datetime
current_year = datetime.datetime.now().year
Current_age = current_year-Year_of_birth
if(Current_age<=18):
else:
OUTPUT:
RESULT:
Thus the program voters age validity has been executed successfully
lOMoAR cPSD| 45433932
AIM:
ALGORITHM:
PROGRAM:
OUTPUT:
RESULT:
Thus the program student mark range validation has been executed successfully
lOMoAR cPSD| 45433932
PYGAME INSTALLATION
In order to install Pygame, Python must be installed already in your system. Tocheck whether Python
is installed or not in your system, open the command prompt and give the command as shown below.
pip --version
lOMoAR cPSD| 45433932
To install Pygame, open the command prompt and give the command as shownbelow:
Open command prompt set the path to the directory where pygame is installed
Run a game say aliens.py
RESULT:
AIM:
To write a python program to create bouncing ball game activity using pygame
PROGRAM:
import pygame
pygame.init()
window_w = 800
window_h = 600
black = (0, 0, 0)
FPS = 120
clock = pygame.time.Clock()
def game_loop():
block_size = 20
velocity = [1, 1]
pos_x = window_w/2
pos_y = window_h/2
running = True
while running:
if event.type == pygame.QUIT:
lOMoAR cPSD| 45433932
pygame.quit()
quit()
pos_x += velocity[0]
pos_y += velocity[1]
velocity[0] = -velocity[0]
velocity[1] = -velocity[1]
# DRAW
window.fill(white)
pygame.draw.rect(window, black, [pos_x, pos_y, block_size, block_size])
pygame.display.update()
clock.tick(FPS)
game_loop()
OUTPUT:
RESULT:
Thus the program bouncing ball game has been executed successfully