R2021 Python Lab
R2021 Python Lab
R2021 Python Lab
List of Programs
1. Developing flow charts
a. Electricity Billing,
b. Retail Shop Billing,
c. Sin Series,
d. Weight of a Motorbike,
e. Weight of a Steel Bar,
f. Compute Electrical Current in Three Phase AC Circuit,
2. Programs Using Simple Statements
a. Exchange the values of two variables,
b. Circulate the values of n variables,
c. Distance between two points.
3. Programs Using Conditionals and Iterative Statements
a. Number Series
b. Number Patterns
c. Pyramid Pattern
4. Operations of Lists and Tuples (Items present in a library/Components of a
car/ Materials required for construction of a building)
5. Operations of Sets & Dictionaries (Language, components of an automobile,
Elements of a civil structure, etc)
6. Programs Using Functions
a. Factorial of a Number
b. Largest Number in a list
c. Area of Shape
7. Programs using Strings.
a. Reversing a String,
b. Checking Palindrome in a String,
Problem Solving & Python Programming 3
Program No: 1A
File Name: DEVELOPING FLOWCHARTS
Ex. No: ELECTRICITY BILLING
Date:
Problem Statement:
Start
Read Units
Print Amount
Stop
6 Problem Solving & Python Programming Laboratory
Program No: 1 B
File Name:
RETAIL SHOP BILLING
Ex. No:
Date:
Problem Statement:
Start
Tax=0.18
Rate_of_item
Display Items
Read Quantities
Cost=Rate_of_item * quantity+
Rate_of_item * quantity+ ……………..
Print BillAmount
Stop
|
Problem Solving & Python Programming 7
Program No: 1C
File Name:
SINE SERIES
Ex. No:
Date:
Problem Statement:
To evaluate the sine series. The formula used to express the Sin(x) as
Start
Read x, n
x=x*3.14159/180;
t=x;
sum=x;
t=(t*(-1)*x*x)/(2*i*(2*i+1));
sum=sum+t;
Stop
8 Problem Solving & Python Programming Laboratory
Program No: 1D
File Name:
Ex. No: WEIGHT OF A STEEL BAR
Date:
Problem Statement:
Start
Read Diameter D
W = D2 / 162
Print W
Stop
|
Problem Solving & Python Programming 9
Program No: 1E
File Name: COMPUTE ELECTRICAL CURRENT IN THREE
Ex. No: PHASE AC CIRCUIT
Date:
Problem Statement:
Start
VA = * Vline * Aline
Print VA
Stop
10 Problem Solving & Python Programming Laboratory
Program:
|
Problem Solving & Python Programming 11
Output:
Enter value of X: 67
Enter value of Y: 56
x = 67
Y= 56
x = 56
Y= 67
12 Problem Solving & Python Programming Laboratory
Program No: 2B
File Name: CIRCULATE THE VALUES OF N VARIABLES
Ex. No:
Date:
Aim:
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
|
Problem Solving & Python Programming 13
Program No: 2C
File Name: DISTANCE BETWEEN TWO VARIABLES
Ex. No:
Date:
Aim:
To write a python program to find distance between two variables.
Program:
import math
distance = math.sqrt(((x2-x1)**2)+((y2-y1)**2))
print("Distance = ",distance)
Output:
Enter a x1: 3
Enter a y1: 2
Enter a x2: 7
Enter a y2: 8
Distance = 7.211102550927978
14 Problem Solving & Python Programming Laboratory
Program No: 3 A
File Name: PROGRAMS USING CONDITIONALS AND
ITERATIVE LOOPS
Ex. No:
NUMBER SERIES
Date:
Aim:
Program:
Output:
Enter a number: 10
Sum = 385
|
Problem Solving & Python Programming 15
Program No: 3 B
File Name:
NUMBER PATTERN
Ex. No:
Date:
Aim:
To write a python program to print number pattern.
Program:
N=5
for i in range(1,N+1):
for j in range(1,i+1):
for l in range(i1,0,1):
print()
Output:
121
12321
1234321
123454321
16 Problem Solving & Python Programming Laboratory
Program No: 3C
File Name:
Ex. No: PYRAMID PATTERN
Date:
Aim:
Program:
m = (2 * n) - 2
print(end=" ")
print(" ")
|
Problem Solving & Python Programming 17
Output:
**
** *
** * *
** * * *
* * ** * *
** * * * * *
** * * * * * *
* ** * * * * * *
18 Problem Solving & Python Programming Laboratory
Program No: 4A
File Name: OPERATIONS OF LISTS
Ex. No:
Date:
Aim:
Program:
library =
['Books','Periodicals','Newspaper','Manuscripts','Maps','Prints','Documents','Ebooks']
print('Library: ',library)
library.append('Audiobooks')
|
Problem Solving & Python Programming 19
library.sort()
# popping an element
library.remove('Maps')
library.insert(2, 'CDs')
Output:
index of 'Newspaper': 2
|
Problem Solving & Python Programming 21
Program No:4B
File Name: OPERATIONS OF TUPLE
Ex. No:
Date:
Aim:
Program:
Output:
|
Problem Solving & Python Programming 23
Program No:5A
File Name: OPERATIONS OF SETS
Ex. No:
Date:
Aim:
Program:
# set union
# set intersection
# set difference
Output:
Program No:6A
File Name: FACTORIAL OF A NUMBER USING FUNCTION
Ex. No:
Date:
Aim:
Program:
def fact(n):
if n = = 1:
return n
else:
return n*fact(n1)
Output:
Enter a number: 5
|
Problem Solving & Python Programming 25
Program No:6B
File Name: FINDING LARGEST NUMBER IN A LIST USING
Ex. No: FUNCTION
Date:
Aim:
To write a python program to find the largest number in a list using functions.
Program:
def myMax(list1):
print("Largest element is:", max(list1))
list1 = []
num = int(input("Enter number of elements in list: "))
for i in range(1, num + 1):
ele = int(input("Enter elements: "))
list1.append(ele)
print("Largest element is:", myMax(list1))
Output:
Program No:6C
File Name: FINDING AREA OF A CIRCLE USING FUNCTION
Ex. No:
Date:
Aim:
Program:
def findArea(r):
PI = 3.142
return PI * (r*r);
num=float(input("Enter r value:"))
print("Area is %.6f" % findArea(num));
Output:
Enter r value:8
Area is 201.088000
|
Problem Solving & Python Programming 27
Program No:7A
File Name: REVERSING A STRING
Ex. No:
Date:
Aim:
Program:
def reverse(string):
string = "".join(reversed(string))
return string
Output:
Program No:7B
File Name: CHECKING PALINDROME IN A STRING
Ex. No:
Date:
Aim:
Program:
string = string.casefold()
rev_string = reversed(string)
if list(string) = = list(rev_string):
print("It is palindrome")
else:
Output:
It is not palindrome
|
Problem Solving & Python Programming 29
Program No:7C
File Name: COUNTING CHARACTERS IN A STRING
Ex. No:
Date:
Aim:
Program:
val = string.count(char)
print(val,"\n")
Output:
2
30 Problem Solving & Python Programming Laboratory
Program No:7D
File Name:
REPLACE CHARACTERS IN A STRING
Ex. No:
Date:
Aim:
Program:
Output:
Step 4:
|
Problem Solving & Python Programming 33
Program No:8A
File Name: PANDAS
Ex. No:
Date:
Aim
To write a python program to compare the elements of the two Pandas Series using
Pandas library.
Program:
import pandas as pd
print("Series1:")
print(ds1)
print("Series2:")
print(ds2)
print("Equals:")
print(ds1 == ds2)
print("Greater than:")
print("Less than:")
|
Problem Solving & Python Programming 35
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 said Series:
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
36 Problem Solving & Python Programming Laboratory
Program No:8B
File Name: NUMPY
Ex. No:
Date:
Aim:
To write a program to test whether none of the elements of a given array is zero
using NumPy library.
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 said 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 said array is zero:")
print(np.all(x))
Output:
Original array:
[1 2 3 4]
Test if none of the elements of the said array is zero:
True
Original array:
[0 1 2 3]
Test if none of the elements of the said array is zero:
False
|
Problem Solving & Python Programming 37
Program No:8C
File Name: MATPLOTLIB
Ex. No:
Date:
Aim:
Program:
import numpy as np
plt.plot(xpoints, ypoints)
plt.show()
Output:
38 Problem Solving & Python Programming Laboratory
Program No:8D
File Name: SCIPY
Ex. No:
Date:
Aim:
Program:
print(constants.hour)
print(constants.day)
print(constants.week)
print(constants.year)
print(constants.Julian_year)
Output:
60.0
3600.0
86400.0
604800.0
31536000.0
31557600.0
|
Problem Solving & Python Programming 39
Program No:9A
File Name: COPY FROM ONE FILE TO ANOTHER
Ex. No:
Date:
Aim:
Program:
Output:
Enter source file name: file1.txt
Enter destination file name: file2.txt
File copied successfully!
Sunflower
Jasmine
Roses
40 Problem Solving & Python Programming Laboratory
Program No:9B
File Name: WORD COUNT FROM A FILE
Ex. No:
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.
Program:
Output:
|
Problem Solving & Python Programming 41
Program No:9C
File Name: FINDING LONGEST WORD IN A FILE
Ex. No:
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.
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']
42 Problem Solving & Python Programming Laboratory
Program No:10A
File Name: DIVIDE BY ZERO ERROR USING EXCEPTION
Ex. No: HANDLING
Date:
Aim:
To write a python program to handle divide by zero error using exception handling.
Program:
Output:
|
Problem Solving & Python Programming 43
Program
No:10B File VOTERS AGE VALIDITY
Name:
Ex. No:
Date:
Aim:
Program:
import datetime
Year_of_birth = int(input("In which year you 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")
Output:
Program
No:10C File STUDENT MARK RANGE VALIDATION
Name:
Ex. No:
Date:
Aim:
Program:
Output:
|
Problem Solving & Python Programming 45
Program No:11
File Name: EXPLORING PYGAME
Ex. No:
Date:
PYGAME INSTALLATION
Steps
3. Click
pygame-1.9.3.tar.gz ~ 2M and download zar file
4. Extract the zar file into C:\Python36-32\Scripts folder
5. Open command prompt
6. Type the following command
46 Problem Solving & Python Programming Laboratory
|
Problem Solving & Python Programming 47
Program No: 12
File Name:
SIMULATE BOUNCING BALL USING PYGAME
Ex. No:
Date:
Aim:
Program:
import pygame
pygame.init()
window_w = 800
window_h = 600
FPS = 120
def game_loop():
block_size = 20
velocity = [1, 1]
pos_x = window_w/2
pos_y = window_h/2
48 Problem Solving & Python Programming Laboratory
running = True
while running:
pos_x += velocity[0]
pos_y += 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()
|