Document With Solutions
Document With Solutions
Document With Solutions
if number == 0:
print(number, "is zero")
elif number < 0:
print(number, "is negative")
elif number > 0:
print(number, "is positive")
2. Create a program that asks for a student's marks and determines the
grade based on the following criteria:
90 and above: A
80 to 89: B
70 to 79: C
60 to 69: D
Below 60: F
while a > 1:
a -= 1
number = a*number
print(number)
String Manipulation
1. Write a Python program that takes a string input and prints it in reverse
order.
reversed = string[::-1]
print(reversed)
2.Create a program that counts the number of vowels in a given string.
print(count)
6. Create a program that takes a list of numbers and returns a new list with
each element squared.
def square_elements(lst):
return [x**2 for x in lst]
7. Write a program that merges two lists into a single list, removing any
duplicates.
def merge_lists(lst1, lst2):
return list(set(lst1 + lst2))
Tuple Manipulation
9. Write a Python program to create a tuple and print its elements.
tup = (1, 2, 3, 4, 5)
for element in tup:
print(element)
10. Create a program that finds the index of a given element in a tuple.
tup = (10, 20, 30, 40, 50)
element = int(input("Enter the element to find its
index: "))
if element in tup:
print(f"Index of {element}: {tup.index(element)}")
else:
print(f"{element} not found in the tuple.")
11. Write a program to convert a list into a tuple and vice versa.
lst = [1, 2, 3, 4, 5]
tup = tuple(lst)
print("Tuple:", tup)
new_lst = list(tup)
print("List:", new_lst)
12. Create a program that joins two tuples and sorts the resulting tuple.
tup1 = (1, 3, 5)
tup2 = (2, 4, 6)
merged_tup = tuple(sorted(tup1 + tup2))
print("Merged and sorted tuple:", merged_tup)
Dictionary Manipulation
13. Write a Python program to create a dictionary and print its keys and values.
my_dict = {'a': 1, 'b': 2, 'c': 3}
for key, value in my_dict.items():
print(f"Key: {key}, Value: {value}")
14. Create a program that updates the value of a specific key in a dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
key = input("Enter the key to update: ")
new_value = int(input("Enter the new value: "))
if key in my_dict:
my_dict[key] = new_value
print(f"Updated dictionary: {my_dict}")
else:
print(f"Key '{key}' not found.")
16. Create a program that finds the key with the maximum value in a
dictionary.
my_dict = {'a': 1, 'b': 3, 'c': 2}
max_key = max(my_dict, key=my_dict.get)
print(f"Key with the maximum value: {max_key}")