-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6. dictionaries.py
45 lines (35 loc) · 913 Bytes
/
6. dictionaries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
student = {'name': 'Tanishka', 'age': 19, 'courses': ['computer', 'maths']}
print(student)
print(student['courses'])
print(student['age'])
print(student['name'])
#error print(student['phone'])
print(student.get('name'))
print(student.get('phone'))#returns none
print(student.get('phone', 'not_found'))#returns not found
#to add
student['phone'] = '5555-24333'
print(student)
#if we change info
student['name'] = 'Tanu'
print(student)
#to update info in one shot
student.update({'name': 'tanya', 'age': 20, 'phone': '66666-45454'})
print(student)
#remove key or value
del student['phone']
print(student)
age = student.pop('age')
print(student)
print(age)
#to find number of keys
print(student)
print(len(student))
print(student.keys())
print(student.values())
print(student.items())#for both key and values
#looping
for keys in student:
print(keys)
for keys,value in student.items():
print(keys,value)