-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0053_dictionaries_intro.py
More file actions
101 lines (87 loc) · 2.88 KB
/
0053_dictionaries_intro.py
File metadata and controls
101 lines (87 loc) · 2.88 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
fruit = {
"orange": "a sweet, orange, citrus fruit",
"apple": "good for making cider",
"lemon": "a sour, yellow citrus fruit",
"grape": "a small, sweet fruit growing in bunches",
"lime": "a sour, green citrus fruit",
"apple": "round and crunchy" # replaces value of the first apple
} # format just like JSON, can access value via key in string format
# fruit.keys() = dict_keys(['orange',
# 'apple',
# 'lemon',
# 'grape',
# 'lime',
# 'apple'])
# fruit.values() = dict_values(['a sweet, orange, citrus fruit',
# 'good for making cider',
# 'a sour, yellow citrus fruit',
# 'a small, sweet fruit growing in bunches',
# 'a sour, green citrus fruit',
# 'round and crunchy'])
print(fruit)
print()
print(fruit["orange"])
print()
fruit["pear"] = "an odd-shaped fruit" # appending a new key-value pair
print(fruit)
fruit["pear"] = "great with tequila"
print()
print(fruit)
del fruit["lemon"] # deletes lemon item
# del fruit # deletes the entire dictionary
print()
print(fruit)
# fruit.clear() # removes all element in the dictionary. dictionary stil exists but empty
print()
print(fruit)
# print(fruit["tomato"]) # error. tomato does not exist in the dictionary
print()
# while True:
# dict_key = input("Please enter a fruit: ")
# if dict_key == "quit":
# break
# # description = fruit.get(dict_key, "We don't have a "+ dict_key) # like a ternary
# # fruit.has_key(dict_key) # alternative to "if dict_key in"
# # print(description)
# if dict_key in fruit:
# description = fruit.get(dict_key)
# print(description)
# else:
# print("We don't have a " + dict_key)
# # retrives the value based on the key entered.
# # like accessing through fruit["orange"]
# # print(description)
# for snack in fruit:
# print(fruit[snack])
# for i in range(10):
# for snack in fruit:
# print(snack + " is " + fruit[snack])
# print('-' * 40)
ordered_keys = sorted(list(fruit.keys()))
# ordered_keys.sort() # sorts the keys in place
for f in ordered_keys:
print(f + " - " + fruit[f])
# print(ordered_keys)
print()
for f in sorted(list(fruit.keys())):
print(f + " - " + fruit[f])
print()
for val in fruit.values():
print(val)
print()
for val in fruit.values():
print(val)
print('-' * 40)
print()
for key in fruit.keys():
print(key)
fruit["tomato"] = "not nice with ice cream"
print(tuple(fruit.keys()))
print('*' * 40)
print(tuple(fruit.items()))
print('*' * 40)
for snack in tuple(fruit.items()):
item, description = snack
print(item + " is " + description)
print('*' * 40)
print(dict(tuple(fruit.items())))