1)Reverse Each Word in a String
sentence = input("Enter a sentence: ")
words = sentence.split()
for word in words:
print(word[::-1], end=" ")
2) Character Frequency Counter
string = input("Enter a string: ")
freq = {}
for char in string:
if char != " ":
if char in freq:
freq[char] += 1
else:
freq[char] = 1
for char, count in freq.items():
print(char, count)
3) Python program that simulates a contact book using a dictionary,
allowing for adding, removing, updating, and searching contacts:
contacts = {}
def add_contact(name, phone):
contacts[name] = phone
def remove_contact(name):
if name in contacts:
del contacts[name]
def update_contact(name, phone):
if name in contacts:
contacts[name] = phone
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
input_str = input("Enter a string: ")
print("Number of vowels:", count_vowels(input_str))
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")
# Create object of child class
d = Dog()
d.speak() # Inherited from Animal
d.bark() # From Dog
7)Constructor and decorator
def decorator(func):
def wrapper(self):
print("Let me introduce myself")
func(self)
print("Thank you")
return wrapper
class Student:
def __init__(self,name,age):
self.name = name
self.age = age
@decorator
def disp(self):
print("Myself {self.name} and i am {self.age} years old ")
s = Student("Rutuja", 20)
s.disp()
8) Maximum Number in Tuple using User Input
user_input = input("enter nos separated by space : ")
ntuple = tuple(map(int,user_input.split(" ")))
maxnum = max(ntuple)
print("The tuple is:", ntuple)
print("maximum number : " , maxnum)
9) Student Marks Lookup using Dictionary
student_marks = {}
n = int(input("Enter number of students: "))
for i in range(n):
name = input("Enter name of student: " )
marks = float(input("Enter marks of " + name ))
student_marks[name] = marks
search_name = input("\nEnter the name of the student to look up: ")
if search_name in student_marks:
print(search_name + " marks: " + str(student_marks[search_name]))
else:
print("Student not found.")
10) List manipulation
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)