Python Lab Programs
Python Lab Programs
by the customer for Challenger Computers Store. A customer buying two numbers of
SSD device, one SSD device cost is Rs. 3575/-. The stores offer 15% of the total cost. The
customer has to pay 9% CGST, and 9% SGST. Prepare the Net Amount to be payable
by the customer.
total=2*3575
discount=total*15/100
total=total-discount
cgst=total*9/100
sgst=total*9/100
netamt=total+cgst+sgst
print(netamt)
Design a python script to compute and generate the electricity bill as per the following
slab rates. Collect the meter reading inputs, such as current unit and previous unit.
0-200 3.0
201-250 4.5
251-300 5.2
301-400 6.5
n=int(input("Enter n
value:")) s=0
for i in range(n):
x=int(input("Enter
element:")) if x%4==0:
s=s+x
print("Sum of elements=",s)
4.
Corner home delivers vegetarian and non-vegetarian combos to its
customer based on order. A vegetarian combo costs Rs.120 per plate and a
non-vegetarian combo costs Rs.150 per plate. Their non-veg combo is really
famous that they get more orders for their non-vegetarian combo than the
vegetarian combo. Apart from the cost per plate of food, customers are also
charged for home delivery based on the distance in kms from the
Given the type of food, quantity (no. of plates) and the distance in kms
from the restaurant to the delivery point, write a python program to
calculate the final bill amount to be paid by a customer. The below
information must be used to check the validity of the data provided by the
customer.
• Type of food must be 'V' for vegetarian and 'N' for non-
vegetarian.
city.append("Chittor")
print(city)
city.append("Nellore")
city.append("Guntur")
print(city)
city.insert(2, "Hyderabad")
print(city)
city.remove("Nellore")
city.remove("Kurnool")
print(city) city =
[c.upper() for c in city]
print(city)
b) Design a python script for given an integer tuple, for each element in
the tuple, check whether there exists a smaller element on the next
immediate position of the tuple. If it exists print the smaller element. If
there is no smaller element on the immediate next to the element then print
-1.
Example:
Input: 4 2 1 53
Output: 2 1 -1 3-1
a=(4,2,1,5,3) for
i in range(len(a)-
1):
if a[i]>a[i+1]:
print(a[i+1],end=" ")
else:
print(-1,end=" ")
print(-1)
6)
a) Sets n1 has the data {1, 3, 5, 7, 9), n2 has the data {9, 5, 6, 8},
wd1=set(["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]),
wd2=set(["Mon", "Tue", "Wed"]).
import re
Wel_str = "Welcome to AI
ML DS"
matcher=re.finditer('AI',Wel
_str) for m in matcher:
print(m.start(),m.end(),m.group())
7)
a) Design a python script for the mathematical puzzle, Towers of Hanoi.
The puzzle has three rods and n disks. To move the entire stack to another
rod, obeying the three rules (i) Only one disk can be moved at a time, (ii)
Each move consists of taking the upper disk from one of the stacks and
placing it on top of another stack i.e., a disk can only be moved if it is the
uppermost disk on a stack, (iii) No disk may be placed on top of a smaller
disk.
def TowerOfHanoi(n,source,destination,auxiliary):
if n==1:
print ("Move disk 1 from source",source,"to destination",destination)
return
TowerOfHanoi(n-1, source, auxiliary, destination)
print ("Move disk",n,"from source",source,"to destination",destination)
TowerOfHanoi(n-1, auxiliary, destination, source)
n = int(input("Enter n value:"))
TowerOfHanoi(n,'A','B','C')
b) Design a python script to display the numbers that do not appear in the
Fibonacci series of n numbers where n is given by the user. (If n is 8 then
up to 8 Fibonacci numbers has to be printed Ex: 1 1 2 3 5 8 13 21 and in
this series missing numbers should be traced and printed, Ex: missing
numbers are: 4 6 7 9 10 11 12 14 15 16 17 18 19 20.
a=0
b=1
n=8
x=[]
print("Fibbo
series: ") for i in
range(n+1):
c=a+b
a=b
b=c
x.append(c)
print(c,end=" ")
print("\nFibbo missing
series: ") for i in
range(max(x)+1):
if i not in x:
print(i,end=" ")
8.
a) Design a function Learner_Age_Days with two formal parameters name,
age and it computes Learner's age in days, then displays learners name and
age in days.
(i) Design a driver code to call the function using positional arguments,
keyword arguments
odd_numbers = list(filter(lambda x: x % 2 != 0,
range(1, 11))) print("Odd numbers from 1 to 10:",
odd_numbers) negative_numbers = list(filter(lambda x:
x < 0, range(-7, 8))) print("Negative numbers from -7
to 7:", negative_numbers) max_negative_number =
max(negative_numbers) print("Max negative number
in the list:", max_negative_number)
9)
file1.open("feat_python1.txt","r")
file2.open("feat_python2.txt","w")
file2.write(file1.read())
file1.close()
file2.close()
print("File copied successfully")
10.
a) Construct a Python script to implement the below requirements.
Create a base class Basic_Info with data members name, rollno, gender
and two member functions getdata() and display(). Derive a class
Physical_Fit from Basic_Info which has data members height and weight
and member functions getdata() and display(). Display all the information
using object of derived class.
class Basic_Info:
def getdata(self,name,rollno,gender):
self.name=name
self.rollno=rollno
self.gender=gender
def display(self):
print("Name=",self.name)
print("Rollno=",self.rollno)
print("Gender=",self.gender)
class Physical_Fit(Basic_Info):
def getdata(self,name,rollno,gender,height,weight):
super().getdata(name,rollno,gender)
self.height=height
self.weight=weight
def display(self):
super().display()
print("Height=",self.height)
print("Weight=",self.weight)
p1=Physical_Fit()
p1.getdata("rajesh",111,"Male",165,48)
p1.display()
Private members
Admno : 4-digit admission number
Name : 20 characters
Public members
READINFO() function to accept values for Adno, Name, Marks. Invoke the
function GETAVG ().
DISPLAYINFO() function to display all data members of report on the
screen. You should give function definitions. Write driver code to
demonstrate all the functions.
class Report:
def readInfo(self,adno,name,marks):
self.adno=adno
self.name=name
self.marks=marks
def displayInfo(self):
print("Admission no=",self.adno)
print("Name=",self.name)
print("Marks=",self.marks)
print("Average=",self.getAvg(marks))
def getAvg(self,marks):
return sum(marks)/5
r1=Report()
adno=int(input("Enter admission no:"))
name=input("Enter name:")
marks=[]
for i in range(5):
x=float(input("Enter marks:"))
marks.append(x)
r1.readInfo(adno,name,marks)
r1.displayInfo()
11)
a) The below scenarios will create Logical Error/Exception, and it will
forcibly stop the execution in middle of the program. Design a Python
Script the to handle these operations exceptions effectively, and avoid to
stop the script execution in the middle.
i. The variable num has the data 100, the value of num dividing by the
value 0. ii. To importing a library file matheqn, this library file not
available in Python. iii. A num_List has the
values[10,20,30].To print the fifth value of num_List[5] iv. A dictionary has
the data, Dict_Univ = {'1':"MBU", '2': "Tirupathi", '3': "CSE"). to print
the fifth key value Dict Univ[5]
try:
print(100/0)
except ZeroDivisionError:
print("Second no cannot be zero")
try:
import matheqn
except ModuleNotFoundError:
print("Module not available")
try:
num_list=[10,20,30]
print(num_list[5])
except IndexError:
print("List index out of range")
try:
Dict_Univ={'1':"MBU",'2':"Tirupathi",'3':"CSE"}
print(Dict_Univ[5])
except KeyError:
print("Key is not available")
class Negative(Exception):
def __init__(self,arg):
self.msg=arg
python_mark=[]
for i in range(10):
x=int(input("Enter marks:"))
if x<0:
raise Negative("Marks must be positive")
else:
python_mark.appen