0% found this document useful (0 votes)
2 views

Python Lab (1)

The document outlines various programming experiments including calculating electricity bills, exchanging variable values, finding distances between points, generating Fibonacci series, and more. Each experiment includes an aim, algorithm, program code, and results indicating successful execution. The experiments utilize different programming concepts and libraries such as Python, pandas, numpy, and matplotlib.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Lab (1)

The document outlines various programming experiments including calculating electricity bills, exchanging variable values, finding distances between points, generating Fibonacci series, and more. Each experiment includes an aim, algorithm, program code, and results indicating successful execution. The experiments utilize different programming concepts and libraries such as Python, pandas, numpy, and matplotlib.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Sample Output: Experiment 1: Tamilnadu electricity board has 4 tariff categories

Enter the units consumed based on the consumer’s energy consumption. Find the electricity
260 bill based on the consumer’s energy consumption.
Your Bill is : 410
Per Unit rate in TNEB
Fixed Charges Per
Scheme Unit Per unit(₹)
Flowchart: Month
0 to 100 0-100 0 0
0-100 0
0 to 200 20
101-200 1.5
0-100 0
0 to 500 101-200 2 30
201-500 3
0-100 0
101-200 3.5
> 500 50
201-500 4.6
>500 6.6

Aim:
To find the electricity bill based on the consumer’s energy
consumption.
Algorithm:
Step 1: Start
Step 2: Read u, the number of units consumed
Step 3: if u is upto 100 then bill=0
Step 4: if u is more than 100 and upto 200 then
bill=(u-100)*1.5+20
Step 5: if u is more than 200 and upto 500 then
bill=100*2+(u-200)*3+30
Step 6: else bill=100*3.5+300*4.6+(u-500)*6.6+50
Step 7: display the bill for the consumed units
Step 8: Stop

Program:
u=int(input())
if u<=100:
bill=0
elif u>100 and u<=200:
bill=(u-100)*1.5+20
elif u>200 and u<=500:
bill=100*2+(u-200)*3+30
else:
bill=100*3.5+300*4.6+(u-500)*6.6+50
print("Your Bill is :",bill)

Result:
The above program has been executed successfully.
Sample Output: Experiment 2 (a): Exchange the value of two variables
Enter the value of a : 10
Enter the value of b : 20 Aim:
After exchange value of a : 20 To Exchange the value of two variables using third variable
After exchange value of b : 10 Algorithm:
Step 1: Start
Step 2: Read the two variables a and b
Step 3: Assign t=a
Step 4: Assign a=b
Step 5: Assign b=t
Step 6: Display the new value of a and b
Step 7: Stop

Program:
a=int(input("Enter the value of a : "))
b=int(input("Enter the value of b : "))
t=a
a=b
b=t
print("After exchange value of a : ", a)
print("After exchange value of b : ", b)

Result:
The above program has been executed successfully.
Sample Output: Experiment 2 (b): Distance between two points
Enter the value of x1 : 1
Enter the value of y1 : 2 Aim:
Enter the value of x2 : 3 To find the distance between two points
Enter the value of y2 : 4 Algorithm:
The distance between two points is : 2.8284 Step 1: Start
Step 2: Read value of x1, y1, x2, y2 for two points
Step 3: Calculate the distance between two points
Step 4: Display the distance between two points
Step 5: Stop

Program:
x1=int(input("Enter the value of x1 : "))
y1=int(input("Enter the value of y1 : "))
x2=int(input("Enter the value of x2 : "))
y2=int(input("Enter the value of y2 : "))
d=((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))**0.5
print("The distance between two points is : ", d)

Result:
The above program has been executed successfully.
Sample Output: Experiment 2 (c): Circulate the value of n variables
The list after circulating the values : [40, 50, 10, 20, 30]
Aim:
To circulate the value of n variables
Algorithm:
Step 1: Start
Step 2: Read a list as input
Step 3: Circulate the values of list using slice operator
Step 4: Display the list after circulating the values
Step 5: Stop

Program:
list=[10,20,30,40,50]
x=2 #Shift 2 location in clockwise direction
y=list[-x: ]+list[ :-x]
print("The list after circulating the values : ",y)

Result:
The above program has been executed successfully.
Sample Output: Experiment 3 (a): First n elements of Fibonacci series
Enter the value of 'n': 5
Fibonacci Series: Aim:
0 To print First n elements of Fibonacci series
1 Algorithm:
1 Step 1: Start
2 Step 2: Read value of n
3 Step 3: Assign a=0, b=1 and count=0
Step 4: Apply loop and print n elements of Fibonacci series
Step 5: Stop

Program:
n=int(input("Enter the value of 'n': "))
a,b=0,1
count=0
print("Fibonacci Series: ")
while(count<n):
print(a)
c=a+b
a=b
b=c
count=count+1

Result:
The above program has been executed successfully.
Sample Output: Experiment 3 (b): Half pyramid pattern of numbers
1 1
12 12
123 123
1234 1234
12345 12345
Aim:
To print the half pyramid pattern of numbers
Algorithm:
Step 1: Start
Step 2: Assign number of rows=5
Step 3: Apply nested for loop and print the half pyramid
pattern of numbers
Step 4: Stop

Program:
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end=' ')
print( )

Result:
The above program has been executed successfully.
Sample Output: Experiment 4 (a): Find the minimum in a list of numbers
43825 Aim:
The list is [4, 3, 8, 2, 5] To find the minimum in a list of numbers
The minimum number is 2
Algorithm:
Step 1: Start
Step 2: Read list a of numbers
Step 3: Assign min=a[0]
Step 4: Apply for loop for length of list numbers
Step 5: Find the minimum number in the list
Step 6: Display the minimum number
Step 7: Stop

Program:
a=[int(i) for i in input( ).split( )]
print("The list is ",a)
min=a[0]
for j in range(1,len(a)):
if a[j]<min:
min=a[j]
print("The minimum number is ",min)

Result:
The above program has been executed successfully.
Sample Output: Experiment 4 (b): Search a book from unarranged library books
The tuple of books is ('ENGLISH', 'C', 'MATHS', Aim:
'JAVA', 'PYTHON') To search a book from unarranged library books using linear
JAVA search
Book found
Algorithm:
Step 1: Start
Step 2: Read a tuple of books and a searching book s
Step 3: Assign f=0
Step 4: Apply for loop for length of tuple
Step 5: compare s linearly with each book of tuple
Step 6: display whether book found or not found
Step 7: Stop

Program:
a=("ENGLISH","C","MATHS","JAVA","PYTHON")
print("The tuple of books is ",a)
s=input( )
f=0
for i in range(len(a)):
if(a[i]==s):
f=1
print("Book found ")
break
if(f==0):
print("Book not found")

Result: The above program has been executed successfully.


Sample Output: Experiment 5: Suppose in a college technical festival three
All participants : {'Hari', 'Ram', 'Ravi', 'Kavin', 'Navin', events are going to happen and in each event any number of
participants are taking part. Find all the participants that are
'Tarun'} Participants taking Part in all events : {'Kavin'}
taking part in a technical festival and find the participants that are
taking part in all three events using set data type.
Aim:
To find all the participants that are taking part in a college
technical festival and to find the participants that are taking part
in all three events using set data type.
Algorithm:
Step 1: Start
Step 2: Read three events participants using set data type
Step 3: Apply union on three events participants to find all the
participants that are taking part in a college technical festival
Step 4: Apply intersection on three events participants to find
the participants that are taking part in all three events
Step 5: display the participants that are taking part in a technical
festival and the participants that are taking part in all three events
Step 6: Stop

Program:
e1={"Ram","Hari","Kavin"}
e2={"Navin","Ravi","Tarun","Kavin"}
e3={"Kavin","Navin"}
c=e1|e2|e3
d=e1&e2&e3
print("All participants : ",c)
print("Participants taking Part in all events : ",d)

Result: The above program has been executed successfully.


Sample Output: Experiment 6 (a): Find the factorial of a number using function
Enter the number : 5 Aim:
Factorial of 5 is 120 To find the factorial of a number using function
Algorithm:
Step 1: Start
Step 2: Define a recursive function to find the factorial of a number
Step 3: Read the number n
Step 4: Display the factorial of a number by calling function
Step 5: Stop

Program:
def factorial(n):
if (n==1 or n==0):
return 1
else:
return n*factorial(n - 1)
n=int(input("Enter the number : "))
print("Factorial of",n,"is",factorial(n))

Result: The above program has been executed successfully.


Sample Output: Experiment 6 (b): Find the area of circle using function
Enter the radius : 2 Aim:
The area of circle is : 12.56 To find the area of circle using function
Algorithm:
Step 1: Start
Step 2: Define a function to find the area of circle
Step 3: Read the radius r
Step 4: Display the area of circle by calling function
Step 5: Stop

Program:
def area(r):
A=3.14*r*r
return A
r=float(input("Enter the radius : "))
print("The area of circle is : ",area(r))

Result: The above program has been executed successfully.


Sample Output: Experiment 7 (a): Find the reverse of the given string
Enter the string : RAM Aim:
The original string is : RAM To find the reverse of the given string
The reversed string is : MAR
Algorithm:
Step 1: Start
Step 2: Define a function to find the reverse of the given string using
for loop
Step 3: Read the string s
Step 4: Display the reversed string by calling function
Step 5: Stop

Program:
def reverse(s):
str = ""
for i in s:
str = i + str
return str
s = input("Enter the string : ")
print ("The original string is : ", s)
print ("The reversed string is : ",reverse(s))

Result: The above program has been executed successfully.


Sample Output: Experiment 7 (b): Check whether the given string is Palindrome
Enter the string : MAAM or not
Palindrome Aim:
To check whether the given string is Palindrome or not
Algorithm:
Step 1: Start
Step 2: Define a function to find the reverse of the given string using
for loop
Step 3: Read the string s
Step 4: if the given string and the reversed string is same then
palindrome
Step 5: else string is not palindrome
Step 6: Stop

Program:
def reverse(s):
str = ""
for i in s:
str = i + str
return str
s = input("Enter the string : ")
if s==reverse(s):
print("Palindrome")
else:
print("Not Palindrome")

Result: The above program has been executed successfully.


Sample Output: Experiment 8 (a): Dataframe from dictionary data using pandas
weight birthyear children hobby library
alice 68 1985 NaN Biking Aim:
bob 83 1984 3.0 Dancing To find the dataframe from dictionary data using pandas library
charles 112 1992 0.0 NaN
Algorithm:
Step 1: Start
Step 2: import the pandas library
Step 3: Take the dictionary data
Step 4: Find dataframe from the dictionary data
Step 5: Display dataframe
Step 6: Stop

Program:
import pandas as pd
people_dict = { "weight": pd.Series([68, 83,
112],index=["alice", "bob", "charles"]), "birthyear":
pd.Series([1984, 1985, 1992],
index=["bob", "alice", "charles"], name="year"),
"children": pd.Series([0, 3], index=["charles", "bob"]),
"hobby": pd.Series(["Biking", "Dancing"], index=["alice",
"bob"]),}
people = pd.DataFrame(people_dict)
print(people)

Result: The above program has been executed successfully.


Sample Output: Experiment 8 (b): Maximum element in an array using numpy
Largest element is: 9 library
Sum of all array elements: 38 Aim:
To find the maximum element in an array using numpy library
Algorithm:
Step 1: Start
Step 2: import the numpy library
Step 3: Take an array using numpy library
Step 4: Find the maximum element using max function and sum
of all array elements using sum function
Step 5: Display the maximum element and sum of all array
elements
Step 6: Stop

Program:
import numpy as np
arr = np.array([[1, 5, 6], [4, 7, 2], [3, 1, 9]])
print ("Largest element is:", arr.max())
print ("Sum of all array elements:", arr.sum())

Result: The above program has been executed successfully.


Sample Output: Experiment 8 (c): Plot the histogram using Matplotlib library
Aim:
To plot the histogram using Matplotlib library
Algorithm:
Step 1: Start
Step 2: import the matplotlib library
Step 3: Take a list of numbers and number of
bins Step 4: Plot the histogram using hist
function Step 5: Stop

Program:
import matplotlib.pyplot as plt
x = [21,22,23,4,5,6,77,8,9,10,31,32,33,34,35,36,37,18,49,50,100]
num_bins = 5
plt.hist(x, num_bins, facecolor='blue')
plt.show()

Result: The above program has been executed successfully.


Sample Output: Experiment 9 (a): Copy data from one file to another file
Copied data from another file is : I am good Aim:
To Copy data from one file to another file
Algorithm:
Step 1: Start
Step 2: Open already existing first file in read mode
Step 3: Open the second file in write mode
Step 4: Copy the data from first file to second file
Step 5: Close the second file after writing
Step 6: Display the second file data
Step 7: Stop

Program:
f=open('abm.txt','r')
f1=open('aba.txt','w')
for l in f:
f1.write(l)
f1.close()
f1=open('aba.txt')
print("Copied data from other file is : ",f1.read())

Result: The above program has been executed successfully.


Sample Output: Experiment 9 (b): Count the total words of a file
Data of the file is : I am good Aim:
Total words count is : 3 To Count the total words of a file
Algorithm:
Step 1: Start
Step 2: Open already existing first file in read mode
Step 3: Assign n =0
Step 4: Count the total number of words after splitting the file into
words
Step 5: Display the file data and the total number of words in file
Step 6: Stop

Program:
f=open('abm.txt','r')
n=0
for l in f:
w=l.split()
n=n+len(w)
f.seek(0)
print("Data of the file is : ",f.read())
print("Total words count is : ",n)

Result: The above program has been executed successfully.


Sample Output: Experiment 10 (a): Divide by zero Exception
Enter First Number: 5 Aim:
Enter Second Number: 0 To check Divide by zero Exception
division by zero
Algorithm:
Step 1: Start
Step 2: Read two numbers from user
Step 3: If the entered data is not integer, throw an exception
Step 4: If the denominator is 0 throw divide by zero exception
Step 5: If no exception is there then return the result as division of
two numbers
Step 6: Stop

Program:
try:
num1 = int(input("Enter First Number: "))
num2 = int(input("Enter Second Number: "))
result = num1 / num2
print(result)
except ValueError as
e:
print("Invalid Input Please Input Integer")
except ZeroDivisionError as e:
print(e)

Result: The above program has been executed successfully.


Sample Output: Experiment 10 (b): Voter age validity using exception handling
Enter the age of voter : abc Aim:
age must be a valid number To validate the voter age using exception handling
Algorithm:
Step 1: Start
Step 2: Read age from user
Step 3: If the entered age is not integer, throw an exception
Step 4: If the entered age is 18 or more than 18 then eligible for
voting
Step 5: If the entered age is less than 18 then Not eligible for
voting
Step 6: Stop

Program:
try:
age=int(input("Enter the age of voter : "))
if age>=18:
print("Eligible to vote")
else:
print("Not eligible to vote")
except ValueError:
print("age must be a valid number")

Result: The above program has been executed successfully.

You might also like