Select Page

ABIOLA RASP – Raspberries for African School Projects

Wie können wir helfen?

< Alle Themen
Print

Python – for loop

no-165 au-04

Phyton „for“ loop

This is a for loop. It has a heading that begins with for, followed by a variable name (in this case i), the word in, a sequence. As with function definitions and other heading lines, the colon at the end of the line indicates that a consistently indented block of statements follows to complete the for loop.

  1. The block of lines is repeated once for each item in the sequence, so in this example, the two lines of the indented block are repeated three times. Also, the variable in the heading (here i) can be used in the block, and on each pass it takes the next value in the sequence.
  2. So on the first pass of the loop count is 0, then 1, and finally 2. Look at the output again and see that it matches this sequence.

When executing step-by-step, note that the for loop heading serves two purposes: After each execution of the heading line, the statements in the indented block are executed, generally using the new value for the variable assigned in the heading

Inhaltsverzeichnis