-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdictionaries.py
49 lines (42 loc) · 922 Bytes
/
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
46
47
48
49
# # A Dictionary is a collection which is unordered, changeable and indexed. No duplicate members.
# # Create dict
person = {
'first_name': 'John',
'last_name': 'Doe',
'age': 30
}
# print(person)
# # Use constructor
# person2 = dict(first_name='Sara', last_name='Williams')
# print(person2)
# # Get value
# print(person['first_name'])
# print(person.get('last_name'))
# # Add key/value
person['phone'] = '555-555-5555'
# print(person)
# # Get dict keys
# print(person.keys())
# # Get dict items
# print(person.items())
# # Copy dict
person2 = person.copy()
person2['city'] = 'Boston'
# print(person2)
# # Remove item
del(person['age'])
# print(person)
person.pop('phone')
# print(person)
# # Clear
# person.clear()
# print(person)
# # Get length
# print(len(person))
# # List of dict
people = [
{'name': 'adeel', 'age': 27},
{'name': 'najam', 'age': 25}
]
print(people)
print(people[0]['name'])