List Values¶
There are several ways to create a new list. The simplest is to enclose the
elements in square brackets ( [
and ]
).
[10, 20, 30, 40]
["spam", "bungee", "swallow"]
The first example is a list of four integers. The second is a list of three strings. As we said above, the elements of a list don’t have to be the same type.
As you would expect, we can also assign list values to variables and pass lists as parameters to functions.
Check your understanding
-
rec-5-1: A list can contain only integer items.
- False
- Yes, unlike strings, lists can consist of any type of Python data.
- True
- Lists are heterogeneous, meaning they can have different types of data.