Python Lab (1)
Python Lab (1)
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")
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)
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))
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))
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))
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")
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)
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())
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()
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())
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)
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)
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")