← Dictionary
Code·basic

Loop

Code that runs multiple times — a `for` or `while` statement.

A loop runs a block of code repeatedly.

colors = ["red", "green", "blue"]

for color in colors:
    print(color)

That loop prints each color on its own line. for goes through each item in the list, one at a time, running the body once per item.

while is the other kind:

count = 0
while count < 3:
    print(count)
    count += 1

A while loop keeps running as long as its condition is true. Be careful — if the condition never becomes false, you've written an infinite loop.

Related