Pyt 33
Pyt 33
Now that we've covered the basic data types in Python, let's start covering the built-in data
structures. First, we have lists.
To define a list, we use square brackets [] with the elements separated by a comma.
💡 Tip: It's recommended to add a space after each comma to make the code more readable.
[1, 2, 3, 4, 5]
["a", "b", "c", "d"]
[3.4, 2.4, 2.6, 3.5]
Lists can contain values of different data types, so this would be a valid list in Python:
my_list = [1, 2, 3, 4, 5]
letters = ["a", "b", "c", "d"]
🔸 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.
Addition: +
>>> 5 + 6
11
>>> 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
''
Nested Lists
Lists can contain values of any data type, even other lists. These inner lists are called nested
lists.
>>> my_list[0]
[1, 2, 3]
>>> my_list[1]
[4, 5, 6]
Nested lists could be used to represent, for example, the structure of a simple 2D game board
where each number could represent a different element or tile:
List Length
We can use the len() function to get the length of a list (the number of elements it contains).
For example:
>>> len(my_list)
4
<list_variable>[<index>] = <value>
For example:
>>> letters = ["a", "b", "c", "d"]
>>> letters
['z', 'b', 'c', 'd']
We can add a new value to the end of a list with the .append() method.
For example:
>>> my_list.append(5)
>>> my_list
[1, 2, 3, 4, 5]
For example:
>>> my_list.remove(3)
>>> my_list
[1, 2, 4]
💡 Tip: This will only remove the first occurrence of the element. For example, if we try to
remove the number 3 from a list that has two number 3s, the second number will not be
removed:
>>> my_list.remove(3)
>>> my_list
[1, 2, 3, 4]
List Indexing
We can index a list just like we index strings, with indices that start from 0:
>>> letters[0]
'a'
>>> letters[1]
'b'
>>> letters[2]
'c'
>>> letters[3]
'd'
List Slicing
We can also get a slice of a list using the same syntax that we used with strings and we can
omit the parameters to use their default values. Now, instead of adding characters to the slice,
we will be adding the elements of the list.
<list_variable>[start:stop:step]
For example:
>>> my_list = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
>>> my_list[2:6:2]
['c', 'e']
>>> my_list[2:8]
['c', 'd', 'e', 'f', 'g', 'h']
>>> my_list[1:10]
['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
>>> my_list[4:8:2]
['e', 'g']
>>> my_list[::-1]
['i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']
>>> my_list[::-2]
['i', 'g', 'e', 'c', 'a']
>>> my_list[8:1:-1]
['i', 'h', 'g', 'f', 'e', 'd', 'c']