-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_8.py
82 lines (66 loc) · 1.91 KB
/
day_8.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# dictionaries
# creating dictionaries
import day_6
d = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'}
# exemplary dictionary
person = {
'first_name': 'Asabeneh',
'last_name': 'Yetayeh',
'age': 250,
'country': 'Finland',
'is_marred': True,
'skills': ['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
'address': {
'street': 'Space street',
'zipcode': '02210'
}
}
# shows that values can be of any data type
len(d) # 4
person.items() # shows all pairs in tuples
# accessing items:
d['key1'] # 'value1'
d['value1'] # error
d[1] # error
print(person['skills'][1]) # React
person.get('first_name') # 'Asabeneh'
# how to get key based on value in dict
# adding items to dict
person.update({'hobbies': ['cycling', 'reading', 'knitting']})
person['skills'].append('R')
person['hobbies']
person['hello'] = 'my name is'
person['hello'] # 'my name is'
# modifying items
person['hello'] = 'i am'
person['hello'] # 'i am'
person['skills'] = 'R', 'Python'
print(person)
# modifying keys
person['programs'] = 'R', 'Python'
del person['skills']
# removing pairs
person.pop('age')
person['age']
person.popitem() # removes last key and value pair?
d.copy()
d.clear()
d
# values and keys as list
# dict_values(['Asabeneh', 'Yetayeh', 'Finland', True, {'street': 'Space street', 'zipcode': '02210'}, ['cycling', 'reading', 'knitting'], 'i am'])
person.values()
# dict_keys(['first_name', 'last_name', 'country', 'is_marred', 'address', 'hobbies', 'hello'])
person.keys()
# Exercises
day_6.create_randint(1, 11, 3)
# [10, 4, 2]
# 2: Add name, color, breed, legs, age to the dog dictionary
dog = dict()
dog = {'name': 'ella', 'color': 'black', 'breed': [
'i', 'dont', 'know'], 'legs': 4, 'age': 7}
# 4: Get the length of the student dictionary
student = dog
print(len(student)) # 5
# 10: Delete one of the items in the dictionary
del student['legs']
print(student)