Learning Objectives
Students will be able to:
- Use lists, tuples as containers for data
- Use list comprehensions to create lists
- Use a range and a for statement to loop through a range of integers
- Create subsets of a sequence using the slice operator
General Purpose Containers
As you know by now, applications frequently need to maintain collections of data within a container data type.
<aside>
🧠What did we use in JS to hold collections of data?
</aside>
In this lesson, we're going to review the following Python built-in types commonly used as containers:
- lists
- tuples
Lists
Purpose
- Lists are to Python as arrays are to JS.
- A list provides a container for zero or more items (elements).
- Lists can contain items of different types, including dictionaries and nested lists.
- List have a class (type) of
list.
Basic Syntax
Like arrays in JS, a list is created with a set of square brackets:
colors = ['red', 'green', 'blue']
The number of items in a list is returned using the built-in len() function:
len(colors)
# > 3