Professional Documents
Culture Documents
Python Assignments
Python Assignments
my_dict = {}
my_dict[1] = 1
my_dict['1'] = 2
my_dict[1.0] = 4
sum = 0
for k in my_dict:
sum += my_dict[k]
print (sum)
Answers:
1-> 4
5
1
2->Type Error
3->30
{(1, 2): 12, (4, 2, 1): 10, (1, 2, 4): 8}
4->6
5->4
6->Key Error
7->1
8->4
9->TypeError: ‘tuple’ object does not support item assignment
10-><class ‘str’>
11->(‘1’, ‘2’, ‘3’, ‘4’)
12->True
13->22
14->2 3 4 5 6 6
15->[4,3,2]
16->ValueError: attempt to assign sequence of size 6 to extended slice of size 5
17->[1,3,5,7,9]
18-><type 'int'>
Miscellaneous :
Q-1 Write a Python program to get the key, value and item in a dictionary.
dict_num = {“a”: 10, “b”: 20, “c”: 30, “d”: 40, “e”: 50, “f”: 60}
key value
a 10
b 20
c 30
d 40
e 50
f 60
solution:
dict_num = {“a”: 10, “b”: 20, “c”: 30, “d”: 40, “e”: 50, “f”: 60}
print(' key'+” ”+”value”)
for i in dict_num:
print(str(i)+" "+str(dict_num[i]))
Q-3 Write a Python program to check if a given key already exists in a dictionary.
Solution
d = {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
if x in d:
print('Key is present in the dictionary')
else:
print('Key is not present in the dictionary')
solution
for i in range(5):
print("* "*(i+1))
solution :
for i in range(5):
print(" "*(5-i)+"*"*(i+1))