Python3 Dictionary: Everything you need to know
ages = {"Dave": 24, "Mary": 42, "John": 58}
print(ages["Dave"])
print(ages["Mary"])
OR
primary = {
"red": [255, 0, 0],
"green": [0, 255, 0],
"blue": [0, 0, 255],
}
pairs = {1: "apple",
"orange": [2, 3, 4],
True: False,
None: "True",
}
print(pairs.get("orange"))
print(pairs.get(7))
print(pairs.get(12345, "not in dictionary"))
...........
>>>
[2, 3, 4]
None
not in dictionary
>>>
squares = {1: 1, 2: 4, 3: "error", 4: 16,}
squares[8] = 64
squares[3] = 9
print(squares)
-----------------
{1: 1, 2: 4, 3: 9, 4: 16, 8: 64}
nums = {
1: "one",
2: "two",
3: "three",
}
print(1 **in** nums)
print(4 **not in** nums)
print(**not** 4 **in** nums)
-----------------
True
True
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.