1. Write a program to display name, rollno, div, 1st semester marks, 10th and 12th marks using variables in interactive mode.
v Code:
name = "Atharva"
rollno = 21
div = "A"
sem1 = 9.68
marks10 = 90.40
marks12 = 75.67
print(name)
print(rollno)
print(div)
print(sem1)
print(marks10)
print(marks12)
v Output:
2. Write a program to display employee ID, employee phone no., employee name and designation using variables in interactive mode.
v Code:
id = 2305
name = "John"
phone = 9876547530
desg = "Manager"
print("Employee Details:")
print("ID:", id)
print("Name:", name)
print("Phone No.", phone)
print("Designation:", desg)
v Output:
3. Write a program to display addition of two numbers using variables in script mode.
v Code:
a=input("Enter first number:")
b=input("Enter second number:")
sum=a+b
print("Addition of given numbers is",sum)
v Output:
4. Write a program to calculate Simple Interest in script mode.
v Code:
p=float(input("Enter principal amount:"))
r=float(input("Enter rate of interest:"))
t=int(input("Enter time(in years):"))
si=p*r*t/100
print("Simple interest:",si)
v Output:
exp 2
EXPERIMENT 2
Code & Output
1. Write a program to calculate shopping bill with GST.
v Code:
cpa=90
cpb=100
cpc=80
qa=2
qb=3
qc=1
tc=(cpa*qa)+(cpb*qb)+(cpc*qc)
gst=tc*0.05
sp=tc+gst
print("Total cost with GST:",sp)
v Output:
2. Write a program to calculate square root of 56,78,90.
v Code:
a=56
b=78
c=90
exp=0.5
print("Square root of 56:",a**exp)
print("Square root of 78:",b**exp)
print("Square root of 90:",c**exp)
v Output:
3. Apply operator precedence on the following equations:
a) (10+20//5 * 3)
b) ((8+2) *5-10/ 2)
c) (100 % 30 + 5 * 2)
d) (40 // 5 *2 + 6)
e) (50-18// 3 * 2)
f) (25 % 7 * 3 + 5)
g) (24//4 *3 -2)
h) (2+ 3 ** 2)
v Code:
print(10+20//5 * 3)
print((8+2) *5-10/ 2)
print(100 % 30 + 5 * 2)
print(40 // 5 *2 + 6)
print(50-18// 3 * 2)
print(25 % 7 * 3 + 5)
print(24//4 *3 -2)
print(2+ 3 ** 2)
v Output:
4. Write a program to check whether number is positive negative or zero using comparison operator.
v Code:
a=int(input("Enter a number:"))
print("Positive: ",a>0)
print("Negative: ",a<0)
print("Zero: ",a==0)
v Output:
EXP 3
1. Write a program for traffic signal.
v Code:
colour = input ("Enter colour of traffic signal:")
if colour=="red" or colour == "RED" or colour == "Red":
print("Stop.")
elif colour=="yellow" or colour=="YELLOW" or colour=="Yellow":
print("Slow Down!!")
elif colour=="green" or colour=="GREEN" or colour=="Green":
print("Go")
v Output:
2. Write a program to book ticket check whether ticket is available or not.
v Code:
total=100
option=input("Do you want do book tickets now(y/n):")
while option=="y":
ticket=int(input("Enter number of tickets:"))
ticket_available=total-ticket
if ticket_available==0:
print("Houseful!!")
elif ticket_available<100:
print("Ticket Available:",ticket_available)
else:
print("All seats are empty")
option=input("Do you want do book tickets now(y/n):")
print("Thank You for Booking with us.")
v Output:
3. Write a program to book parking space.
v Code:
total=100
s=30
b=30
c=30
t=10
option=input("Do you want do book parking spot now(y/n):")
while option=="y":
space_type=(input("What type of vehicle is it?(Scooter(s),Bike(b),Car(c),Truck(t)):"))
if space_type == "s":
park_space=int(input("Enter number of spots:"))
park_available=s-park_space
if park_available==0:
print("All spots are full!!")
elif park_available<s:
print("Spots Available:",park_available)
else:
print("All spots are empty")
elif space_type == "b":
park_space=int(input("Enter number of spots:"))
park_available=b-park_space
if park_available==0:
print("All spots are full!!")
elif park_available<b:
print("Spots Available:",park_available)
else:
print("All spots are empty")
elif space_type == "c":
park_space=int(input("Enter number of spots:"))
park_available=c-park_space
if park_available==0:
print("All spots are full!!")
elif park_available<c:
print("Spots Available:",park_available)
else:
print("All spots are empty")
elif space_type == "t":
park_space=int(input("Enter number of spots:"))
park_available=t-park_space
if park_available==0:
print("All spots are full!!")
elif park_available<t:
print("Spots Available:",park_available)
else:
print("All spots are empty")
option=input("Do you want do parking spot now(y/n):")
print("Thank You for Booking with us.")
v Output:
4. Write a program to do operations using match case.
v Code:
a=10
b=30
choice=input("Enter the operator (+, - , * , / ):")
match choice:
case '+':
print ("Addition:",a+b)
case '-':
print ("Subtraction:",a-b)
case '*':
print ("Multiplication:",a*b)
case '/':
print ("Division:",a/b)
case _:
print ("Invalid Operator.")
v Output:
EXP 4
1. Write a program for Armstrong number using function without parameter with return value.
v Code:
def armstrong():
num=int(input("Enter a number:"))
temp=num
total=0
digits=len(str(num))
while temp > 0:
digit=temp%10
total=total+(digit**digits)
temp=temp//10
return total==num
if armstrong():
print("It is an Armstrong Number.")
else:
print("It is not an Armstrong Number.")
v Output:
2. Write a program for reverse string using function with parameters with return value.
v Code:
def reverse_string(text):
reverse = ""
for char in text:
reverse = char + reverse
return reverse
string = input("Enter a string: ")
result = reverse_string(string)
print("Reversed string:", result)
v Output:
3. Write a program banking system.
v Code:
balance=200000
def cb():
print("Available Balance: ",balance)
def dep():
global balance
am=int(input("Enter deposit amount: "))
balance=balance+am
print("Available Balance: ",balance)
def wd():
global balance
am=int(input("Enter withdrawal amount: "))
balance=balance-am
print("Available Balance: ",balance)
print("ATM Menu:")
print("1. Check Balance")
print("2. Deposit Money")
print("3. Withdraw Money")
choice=int(input("Enter your choice:"))
match (choice):
case 1:
cb()
case 2:
dep()
case 3:
wd()
case _:
print("Invalid Choice")
v Output:
4. Check whether number is palindrome or not.
v Code:
def palindrome(text):
reverse = text[::-1]
if text == reverse:
return True
else:
return False
string = input("Enter a string: ")
result = palindrome(string)
if result:
print("It is a Palindrome.")
else:
print("It is not a Palindrome.")
v Output:
5. Write a program to demonstrate math function using maths library module.
v Code:
import math
print("Absolute value of -14.5: " ,abs(-14.5))
print("2 raised to power 5: " ,pow(2,5))
print("Rounding off 145.982376: " ,round(145.982376))
print("Rounding off 145.982376 to 2 digits: " ,round(145.982376,2))
print("Max number from 10,30,25: " ,max(10,30,25))
print("Min number from 10,30,25: " ,min(10,30,25))
print("Square root of 49: " ,math.sqrt(49))
print("Factorial of 5: " ,math.factorial(5))
v Output:
EXP 5
1. Write a program to remove duplicate character from string, also maintain the order.
v Code:
text = input("Enter text: ")
result=""
ch=""
for ch in text:
if ch not in result:
result+= ch
print(result)
v Output:
2. Write a program to find longest word in a sentence.
v Code:
text = input("Enter a sentence: ")
words=text.split()
longest=""
for word in words:
if len(word) > len(longest):
longest = word
print(longest)
v Output:
3. Write a program to check anagram string.
v Code:
a = input("Enter first string: ")
b = input("Enter second string: ")
if sorted(a) == sorted(b):
print("Anagram")
else:
print("Not Anagram")
v Output:
4. Write a program to check all occurrence of a string.
v Code:
text = input("Enter text: ")
ch=input("Enter string for removal:")
result=text.replace(ch,"")
print(result)
v Output:
EXP 6
Write a program to print student results using tuple and return value.
def result(marks):
total = sum(marks)
return total
marks = (75, 80, 65, 90, 85)
total = result(marks)
print("Marks:", marks)
print("Total Marks:", total)
print("Percentage:", total / 5)
Write a program to count even and odd numbers using tuples.
numbers = (10, 15, 20, 25, 30, 35)
even = 0
odd = 0
for n in numbers:
if n % 2 == 0:
even += 1
else:
odd += 1
print("Even numbers:", even)
print("Odd numbers:", odd)
Write a program list and display elements.
numbers = [10, 20, 30, 40, 50]
print("Elements of list are:")
for n in numbers:
print(n)
Write a program to search for an element in a list.
numbers = [10, 20, 30, 40, 50]
search = int(input("Enter element to search: "))
if search in numbers:
print("Element found")
else:
print("Element not found")
Write a program to find the maximum and minimum element form list.
numbers = [25, 10, 45, 5, 30]
print("Maximum element:", max(numbers))
print("Minimum element:", min(numbers))
Write a program duplicate element from list.
numbers = [10, 20, 10, 30, 20, 40, 30]
new_list = []
for n in numbers:
if n not in new_list:
new_list.append(n)
print("Original list:", numbers)
print("List after removing duplicates:", new_list)
EXP 7
Write a program to search for a key and add a new key value pair. Also display the sum of all values and all key value pairs.
student = {"Maths": 80, "Python": 75, "Java": 85}
key = input("Enter key to search: ")
if key in student:
print("Key found")
else:
print("Key not found")
new_key = input("Enter new key: ")
new_value = int(input("Enter value: "))
student[new_key] = new_value
print("All key-value pairs:")
print(student)
print("Sum of all values:", sum(student.values()))
Write a program to check if values exist, if exist remove that key value pair copy the dictionary to another dictionary.
student = {"Maths": 80, "Python": 75, "Java": 85}
value = int(input("Enter value to search: "))
found = False
for key in list(student):
if student[key] == value:
del student[key]
found = True
print("Key-value pair removed")
if not found:
print("Value not found")
new_dict = student.copy()
print("Original dictionary:", student)
print("Copied dictionary:", new_dict)
Write a program to multiply all the values in the dictionary. Also find the minimum and maximum value. Also update the minimum value by 100.
numbers = {"a": 10, "b": 20, "c": 5, "d": 30}
product = 1
for value in numbers.values():
product = product * value
print("Multiplication of all values:", product)
minimum = min(numbers.values())
maximum = max(numbers.values())
print("Minimum value:", minimum)
print("Maximum value:", maximum)
for key in numbers:
if numbers[key] == minimum:
numbers[key] = 100
print("Dictionary after updating minimum value:", numbers)
Write a program to find the sum of even numbers in a dictionary.
numbers = {"a": 10, "b": 15, "c": 20, "d": 25, "e": 30}
sum_even = 0
for value in numbers.values():
if value % 2 == 0:
sum_even = sum_even + value
print("Sum of even numbers:", sum_even)
EXP 8
1. Write a Python program to demonstrate file handling operations using different file modes such as x, r, w, a, r+, w+, and a+ for creating, reading, writing, and appending data to a file.
# x mode - Create a new file
f = open("demo.txt", "x")
f.write("Hello Python\n")
f.close()
# r mode - Read the file
f = open("demo.txt", "r")
print("Using r mode:")
print(f.read())
f.close()
# w mode - Write to the file
f = open("demo.txt", "w")
f.write("This is written using w mode.\n")
f.close()
# a mode - Append to the file
f = open("demo.txt", "a")
f.write("This is appended using a mode.\n")
f.close()
# r+ mode - Read and write
f = open("demo.txt", "r+")
print("Using r+ mode:")
print(f.read())
f.write("Data added using r+ mode.\n")
f.close()
# w+ mode - Write and read
f = open("demo.txt", "w+")
f.write("Data written using w+ mode.\n")
f.seek(0)
print("Using w+ mode:")
print(f.read())
f.close()
# a+ mode - Append and read
f = open("demo.txt", "a+")
f.write("Data appended using a+ mode.\n")
f.seek(0)
print("Using a+ mode:")
print(f.read())
f.close()
2. Write a Python program for a voter-ID eligibility system that raises the exception when the entered age is below 18 and displays an appropriate message when the person is eligible.
class InvalidAgeError(Exception):
pass
try:
age = int(input("Enter age: "))
if age < 18:
raise InvalidAgeError("Age is below 18. Not eligible for voter ID")
else:
print("You are eligible for voter ID")
except InvalidAgeError as e:
print("Invalid age error:", e)
except ValueError:
print("Please enter a valid age.")
EXP 9
Write a Python program to implement Regular Expressions using the re module and demonstrate operations such as match(), search(), findall(), sub(), and split() on a given string.
import re
text = "Python is easy. Python is powerful. I am learning Python."
# 1. match()
print("1. Match:")
result = re.match("Python", text)
if result:
print(result.group())
else:
print("No match")
# 2. search()
print("\n2. Search:")
result = re.search("easy", text)
if result:
print(result.group())
# 3. findall()
print("\n3. Findall:")
result = re.findall("Python", text)
print(result)
# 4. finditer()
print("\n4. Finditer:")
result = re.finditer("Python", text)
for i in result:
print(i.group(), "at position", i.start())
# 5. split()
print("\n5. Split:")
result = re.split(" ", text)
print(result)
# 6. sub()
print("\n6. Substitute:")
result = re.sub("Python", "Java", text)
print(result)
# 7. fullmatch()
print("\n7. Fullmatch:")
result = re.fullmatch("[A-Za-z]+", "Python")
if result:
print("Full string matched")
else:
print("No full match")
# 8. compile()
print("\n8. Compile:")
pattern = re.compile("Python")
result = pattern.findall(text)
print(result)2 views