Pyt 44
Pyt 44
Now let's start diving into dictionaries. This built-in data structure lets us create pairs of
values where one value is associated with another one.
To define a dictionary in Python, we use curly brackets {} with the key-value pairs separated
by a comma.
The key is separated from the value with a colon :, like this:
The keys of a dictionary must be of an immutable data type. For example, they can be strings,
numbers, or tuples but not lists since lists are mutable.
Strings: {"City 1": 456, "City 2": 577, "City 3": 678}
Numbers: {1: "Move Left", 2: "Move Right", 3: "Move Up", 4: "Move
Down"}
Tuples: {(0, 0): "Start", (2, 4): "Goal"}
The values of a dictionary can be of any data type, so we can assign strings, numbers, lists,
tuple, sets, and even other dictionaries as the values. Here we have some examples:
Dictionary Length
>>> len(my_dict)
4
🔸 Python Operators
Great. Now you know the syntax of the basic data types and built-in data structures in
Python, so let's start diving into operators in Python. They are essential to perform operations
and to form expressions.
>>> 0 + 6
6
💡 Tip: The last two examples are curious, right? This operator behaves differently based on
the data type of the operands.
When they are strings, this operator concatenates the strings and when they are Boolean
values, it performs a particular operation.
In Python, True is equivalent to 1 and False is equivalent to 0. This is why the result is 1 +
0 = 1
Subtraction: -
>>> 5 - 6
-1
>>> 10 - 3
7
>>> 5 - 6
-1
>>> 4.5 - 7
-2.5
Multiplication: *
>>> 5 * 6
30
>>> 6 * 7
42
>>> 10 * 100
1000
>>> 4 * 0
0
>>> 4 * (-6)
-24
>>> "Hello" * 4
'HelloHelloHelloHello'
>>> "Hello" * 0
''
>>> "Hello" * -1
''
<variable_with_dictionary>[<key>]
This expression will be replaced by the value that corresponds to the key.
For example:
print(my_dict["a"])
To update the value associated with an existing key, we use the same syntax but now we add
an assignment operator and the value:
<variable_with_dictionary>[<key>] = <value>
For example:
>>> my_dict["b"] = 6
The keys of a dictionary have to be unique. To add a new key-value pair we use the same
syntax that we use to update a value, but now the key has to be new.
<variable_with_dictionary>[<new_key>] = <value>
For example:
>>> my_dict["e"] = 5
del <dictionary_variable>[<key>]
For example:
Dictionary Methods
These are some examples of the most commonly used dictionary methods:
>>> my_dict.get("c")
3
>>> my_dict.items()
dict_items([('a', 1), ('b', 2), ('c', 3), ('d', 4)])
>>> my_dict.keys()
dict_keys(['a', 'b', 'c', 'd'])
>>> my_dict.pop("d")
4
>>> my_dict.popitem()
('c', 3)
>>> my_dict
{'a': 1, 'b': 2, 'f': 25}
>>> my_dict.values()
dict_values([1, 2, 25, 3, 4, 5])
>>> my_dict.clear()
>>> my_dict
{}
To learn more about dictionary methods, I recommend reading this article from the
documentation.
To learn more about dictionary methods, I recommend reading this article from the
documentation.
To learn more about dictionary methods, I recommend reading this article from the
documentation.