Skip to content

Lists (list)

Going to make 30 separate variables to store 30 students’ names? name1, name2, name3… No! That’s what a list is for — an ordered box that holds lots of data in one variable.

Square brackets [ ] plus comma-separated items:

Inputs (for input())

One value per line — read in order by input().

Output
 

len(...) tells you the length of the list (how many items it holds). A list can store anything: text, numbers, even a mix.

The for from module 03 works beautifully with a list — it walks each item one by one:

Inputs (for input())

One value per line — read in order by input().

Output
 

Read it as: “for each friend in the friends list: greet them.” Whether the list held 3 names or 300, the code would stay the same. That’s the superpower. 💪

Mission

A list of foods

+10 XP

Build a list called foods — with at least 3 of your favorite foods in it — and print it.

Inputs (for input())

One value per line — read in order by input().

Output
 
Mission

Greet everyone

+10 XP

With for, greet every friend in the list on its own line: Hi, Aysel! and so on.

Inputs (for input())

One value per line — read in order by input().

Output
 
Mission

How many are we?

+15 XP

Use len() to find the length of the class list and print it like There are 5 students in the class.

Inputs (for input())

One value per line — read in order by input().

Output
 

Next lesson: how do you grab a single item out of a list? The index — and Python counting from 0 again. Continue →