Sequences in Python#
Variables can contain multiple values, those are called sequences. One example we already introduced is a string-typed variable, containing an immutable ordered collection of Unicode items.
The other three basic sequence types are:
Understanding Lists#
Lists are mutable sequences, typically used to store collections of homogeneous typed elements, but elements can also be of mixed types. The elements within a list can be modified after the list is created.
Initialization with Data#
# Creating a list with some elements
my_list = [1, 2, 3, 4, 5]
print("Initialized list:", my_list)
Initialized list: [1, 2, 3, 4, 5]
Accessing Elements#
Entries can be accessed by their index via the bracket operator []
. Indices in such sequences start at 0
:
# Accessing the first element
first_element = my_list[0]
print("First element:", first_element)
First element: 1
# Accessing the last element using a negative index
last_element = my_list[-1]
print("Last element:", last_element)
Last element: 5
Entries can be changed (lists are mutable) by assigning a new value to an index:
my_list[0] = 10
print("Updated list:", my_list)
Updated list: [10, 2, 3, 4, 5]
Built-in Methods#
The list object provides several methods to work with the underlying data
# Adding an element to the list
my_list.append(6)
print("List after appending 6:", my_list)
List after appending 6: [10, 2, 3, 4, 5, 6]
# Removing an element from the list
my_list.remove(3)
print("List after removing 3:", my_list)
List after removing 3: [10, 2, 4, 5, 6]
# Sorting the list
my_list.sort()
print("Sorted list:", my_list)
Sorted list: [2, 4, 5, 6, 10]
Understanding Tuples#
Tuples are immutable sequences, typically used to store collections of heterogeneous data. Once a tuple is created, its elements cannot be changed.
Initialization with Data#
# Creating a tuple with some elements
my_tuple = (1, 2, 3, 4, 5)
print("Initialized tuple:", my_tuple)
Initialized tuple: (1, 2, 3, 4, 5)
Accessing Elements#
Works just like for lists…
# Accessing the first element
first_element = my_tuple[0]
print("First element:", first_element)
# Accessing the last element
last_element = my_tuple[-1]
print("Last element:", last_element)
First element: 1
Last element: 5
Now try to assign a new value to the first element of the tuple:
Understanding Ranges#
Ranges are immutable collections of numbers, commonly used for counting in loops (later more).
Ranges can be constructed via range(start,end,step)
, where start
defaults to 0
, end
is not inclusive, and step
is an optional parameter for a step width.
Initialization with Data#
# Creating a range of numbers from 0 to 9
my_range = range(10)
print("Initialized range:", list(my_range))
Initialized range: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Accessing Elements#
Works just like for lists…
# Accessing the first element
first_element = my_range[0]
print("First element:", first_element)
# Accessing the last element
last_element = my_range[-1]
print("Last element:", last_element)
First element: 0
Last element: 9
Exercises#
Now that you have learned about lists, tuples, and ranges, try to solve the following exercises to practice your understanding.
Create a list of your favorite fruits. Try to combine it with the numerical list from the top, remember how you did combine strings. Print the result.
Create a tuple with every second number from 0 to 200 and print the last element.