So far, each variable we've used holds just one piece of data, like a single number or string.
But what if you need to store multiple related items together, like all the names in a class or items on a shopping list?
Engagement Message
How might you group related values?
Python provides a way to store an ordered collection of items: a list.
You create lists using square brackets []
, with items separated by commas. For example: [1, 2, 3]
.
Engagement Message
What punctuation separates items inside a list?
Lists are very flexible. They can hold items of different data types, including integers, floats, strings, and even Booleans, all in the same list!
Example: my_stuff = [10, "hello", 4.5, True]
Engagement Message
Can a list contain both the number 50
and the string "score"
?
Just like other data types, you assign a list to a variable using the equals sign =
.
high_scores = [1050, 980, 955]
ingredients = ["flour", "sugar", "eggs"]
Engagement Message
How would you create a variable holding the numbers 2, 3, and 5?
