Lesson 8 of 27

for loops

for loops

A for loop repeats once per item in a sequence. range(n) gives the numbers 0 to n − 1 — perfect for counting.

Python — Try it Yourself
Loading editor…
Output
Click Run to execute the code.

Key points

  • for x in sequence: runs once per item.
  • range(5) is 0,1,2,3,4.
  • You can loop straight over a list.

Your turn: change range(5) to range(1, 6).

Q&A · 0

Enrol to ask questions and join the discussion.

No questions yet — be the first to ask.