0% found this document useful (0 votes)
2 views5 pages

Pyt 44

The document provides an overview of dictionaries in Python, explaining their syntax, key-value pairs, and how to define them using curly brackets. It covers operations such as retrieving, updating, adding, and deleting key-value pairs, as well as common dictionary methods. Additionally, it briefly introduces Python operators and arithmetic operations.

Uploaded by

sandhyadevit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Pyt 44

The document provides an overview of dictionaries in Python, explaining their syntax, key-value pairs, and how to define them using curly brackets. It covers operations such as retrieving, updating, adding, and deleting key-value pairs, as well as common dictionary methods. Additionally, it briefly introduces Python operators and arithmetic operations.

Uploaded by

sandhyadevit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Dictionaries in Python

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:

{"a": 1, "b": 2, "c"; 3}

You can assign the dictionary to a variable:

my_dict = {"a": 1, "b": 2, "c"; 3}

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:

{"product_id": 4556, "ingredients": ["tomato", "cheese", "mushrooms"],


"price": 10.67}
{"product_id": 4556, "ingredients": ("tomato", "cheese", "mushrooms"),
"price": 10.67}
{"id": 567, "name": "Emily", "grades": {"Mathematics": 80, "Biology": 74,
"English": 97}}

Dictionary Length

To get the number of key-value pairs, we use the len() function:

>>> my_dict = {"a": 1, "b": 2, "c": 3, "d": 4}

>>> 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.

Arithmetic Operators in Python

These operators are:


Addition: +
>>> 5 + 6
11

>>> 0 + 6
6

>>> 3.4 + 5.7


9.1

>>> "Hello" + ", " + "World"


'Hello, World'

>>> True + False


1

💡 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 - 5.6 - 2.3


-3.3999999999999995

>>> 4.5 - 7
-2.5

>>> - 7.8 - 6.2


-14.0

Multiplication: *
>>> 5 * 6
30

>>> 6 * 7
42

>>> 10 * 100
1000

>>> 4 * 0
0

>>> 3.4 *6.8


23.119999999999997

>>> 4 * (-6)
-24

>>> (-6) * (-8)


48

>>> "Hello" * 4
'HelloHelloHelloHello'

>>> "Hello" * 0
''

>>> "Hello" * -1
''

Get a Value in a Dictionary

To get a value in a dictionary, we use its key with this syntax:

<variable_with_dictionary>[<key>]

This expression will be replaced by the value that corresponds to the key.

For example:

my_dict = {"a": 1, "b": 2, "c": 3, "d": 4}

print(my_dict["a"])

The output is the value associated to "a":

Update a Value in a Dictionary

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 = {"a": 1, "b": 2, "c": 3, "d": 4}

>>> my_dict["b"] = 6

Now the dictionary is:

{'a': 1, 'b': 6, 'c': 3, 'd': 4}


Add a Key-Value Pair to a Dictionary

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 = {"a": 1, "b": 2, "c": 3, "d": 4}

>>> my_dict["e"] = 5

Now the dictionary has a new key-value pair:

{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

Delete a Key-Value Pair in a Dictionary

To delete a key-value pair, we use the del statement:

del <dictionary_variable>[<key>]

For example:

>>> my_dict = {"a": 1, "b": 2, "c": 3, "d": 4}

>>> del my_dict["c"]

Now the dictionary is:

{'a': 1, 'b': 2, 'd': 4}

Dictionary Methods

These are some examples of the most commonly used dictionary methods:

>>> my_dict = {"a": 1, "b": 2, "c": 3, "d": 4}

>>> 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.setdefault("a", 15)


1
>>> my_dict
{'a': 1, 'b': 2}

>>> my_dict.setdefault("f", 25)


25

>>> my_dict
{'a': 1, 'b': 2, 'f': 25}

>>> my_dict.update({"c": 3, "d": 4, "e": 5})

>>> 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.

You might also like