Variables: List

Attention: Our videos have subtitles in all partner languages. You can switch on subtitles in the youtube player by clicking the gear wheel and selecting your preferred language.

In this section we explain the variable type “list” and show you examples of how to use it. It would be unsustainable to have a variable for every piece of data. A list allows for one variable to point to various pieces of data. It's possible to choose a piece of data you want by pointing the code to a value on a list.

Multiplication in a loop
Create a list called numbers containing a few integers (e.g., [1, 2, 3, 4, 5]). Implement a for loop to do the following: a. Multiply each number in the list by 2. b. Print the result of each multiplication.
Solution
 # Create the list of numbers
numbers = [1, 2, 3, 4, 5]

# Multiply each number in the list by 2 and print the result
for num in numbers:
    result = num * 2
    print(result)