DeskFluent
Module 3 Pro8 min read

Lists and loops

Doing the same thing to a thousand items without typing it a thousand times.

Module 1 said all software is built from three ideas: do something, choose between things, repeat things. You have now done the first two in real Python — variables and, in a moment, if. This lesson is the third idea, and the third is the reason software matters at all.

Lists: boxes in a row

A variable holds one thing. A list holds many, in order:

students = ["Amit", "Priya", "Sara"] print(students)

Square brackets around the outside, commas between the items. Our three students from Module 1 are back — except this time you are writing the code, not just reading it.

You can pick out one item by its position:

print(students[0]) print(students[2])

That prints Amit, then Sara. Yes, you read that correctly: the first item is number 0, not number 1.

Why zero? Historical reasons to do with how computers measure distance from the start of a list. Do not fight it — just remember: position 0 is first, and asking for a position that does not exist (like students[10] here) produces an IndexError, which we will meet properly in the debugging lesson.

Keep reading with Pro

You are reading the free preview. Module 1 of every course is free forever — Pro unlocks the rest of this lesson, every other module of every course, removes ads, and adds workbooks and certificates.