Python exps questions (1)
Python exps questions (1)
contacts = {}
def remove_contact(name):
if name in contacts:
del contacts[name]
def search_contact(name):
if name in contacts:
print(name, contacts[name])
def show_contacts():
for name, phone in contacts.items():
print(name, phone)
# Example usage
add_contact("Alice", "1234567890")
add_contact("Bob", "9876543210")
show_contacts()
update_contact("Bob", "1112223333")
search_contact("Bob")
remove_contact("Alice")
show_contacts()
4) String-Vowel Analyzer
def count_vowels(text):
vowels = "aeiouAEIOU"
count = 0
for char in text:
if char in vowels:
count += 1
return count
5) Arithmetic Operations
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
if b != 0:
print("Division:", a / b)
print("Modulus (Remainder):", a % b)
else:
print("Division and Modulus not allowed with 0")
6) Inheritance
# Parent class
class Animal:
def speak(self):
print("Animal makes a sound")
# Child class
class Dog(Animal):
def bark(self):
print("Dog barks")
def decorator(func):
def wrapper(self):
func(self)
print("Thank you")
return wrapper
class Student:
def __init__(self,name,age):
self.name = name
self.age = age
@decorator
def disp(self):
s = Student("Rutuja", 20)
s.disp()
maxnum = max(ntuple)
student_marks = {}
for i in range(n):
student_marks[name] = marks
if search_name in student_marks:
else:
my_list = []
my_list.append("apple")
my_list.append("banana")
my_list.append("cherry")
print("After append:", my_list)
my_list.insert(1, "orange")
print("After insert at index 1:", my_list)
my_list.remove("banana")
print("After removing 'banana':", my_list)
last_item = my_list.pop()
print("Popped item:", last_item)
print("After pop:", my_list)
if "orange" in my_list:
index = my_list.index("orange")
print("Index of 'orange':", index)
my_list.append("apple")
count = my_list.count("apple")
print("Count of 'apple':", count)
my_list.sort()
print("Sorted list:", my_list)
my_list.reverse()
print("Reversed list:", my_list)
my_list.clear()
print("After clearing:", my_list)