-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5-dictionaries.py
More file actions
54 lines (42 loc) · 934 Bytes
/
5-dictionaries.py
File metadata and controls
54 lines (42 loc) · 934 Bytes
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
46
47
48
49
50
51
52
53
54
# A Dictionary is a collection which is unordered, changeable and indexed. No duplicate members.
# simple effect
person = {
'first_name': 'Jonathan',
'last_name': 'Jones',
'age' : 90
}
print(person)
# Using a constructor
client1 = dict ( first_name = 'Alex', last_name = 'Naval', age = 20 )
print(client1)
print(client1['first_name'])
print(client1.get('first_name'))
#add key /value
client1['phone'] = '333-234-3424'
print(client1)
#print only keys
print(person.keys())
#print only values
print(person.items())
#make a copy
person2 = person.copy()
print(person2)
person2['city'] = 'Vancuver'
print(person2)
#remove item
del(person2['city'])
person2.pop('age')
print(person2)
# Clear
person2.clear()
print(person2)
print(len(person2))
print(person)
print(person2)
# List of dict
people = [
{'name': 'Alice Closer', 'age': 10},
{'name': 'Bob Bryant', 'age': 30}
]
print(people)
print(people[1]['name'])