You are on page 1of 2

ANSWERS OF PROGRAMS:

6)
def reverse_num(num):
if num < 10:
return num
else:
return (num % 10) * (10 ** len(str(num // 10))) + reverse_num(num // 10)
num = int(input("Enter an integer: "))
print("Reverse of", num, "is", reverse_num(num))

Output:
Enter an integer: 1234
Reverse of 1234 is 4321

7)
for i in range(5):
if i == 0 or i == 4:
print("*" * 5)
else:
print("*" + " " * 3 + "*")

Output:
*****
* *
* *
* *
*****

12)
my_list = ['a', 'b', 'c']
my_tuple = tuple(my_list)
print(my_tuple)
new_list = list(my_tuple)
print(new_list)

Output:
('a', 'b', 'c')
['a', 'b', 'c']

13)
friend_dict = {'Alice': '1985-05-20', 'Bob': '1990-08-15', 'Charlie': '1988-02-29'}

sorted_dict = dict(sorted(friend_dict.items()))

print("Sorted Dictionary:")
for name, dob in sorted_dict.items():
print(name, dob)

name = input("Enter a name: ")

if name in friend_dict:
print(name, "has birthday on", friend_dict[name])
else:
dob = input("Enter date of birth (yyyy-mm-dd): ")
friend_dict[name] = dob
print(name, "has been added to the dictionary with birthday on", dob)

sorted_dict = dict(sorted(friend_dict.items()))
print("Updated Sorted Dictionary:")
for name, dob in sorted_dict.items():
print(name, dob)

Output:
i)(If you enter a name which is already in the dictionary:)

Sorted Dictionary:
Alice 1985-05-20
Bob 1990-08-15
Charlie 1988-02-29
Enter a name: Alice
Alice has birthday on 1985-05-20
Updated Sorted Dictionary:
Alice 1985-05-20
Bob 1990-08-15
Charlie 1988-02-29

ii)(If you enter a name which is not in the dictionary:)

Sorted Dictionary:
Alice 1985-05-20
Bob 1990-08-15
Charlie 1988-02-29
Enter a name: Dave
Enter date of birth (yyyy-mm-dd): 1995-11-01
Dave has been added to the dictionary with birthday on 1995-11-01
Updated Sorted Dictionary:
Alice 1985-05-20
Bob 1990-08-15
Charlie 1988-02-29
Dave 1995-11-01

You might also like