Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn For Loops | Loops
Python Ninja

bookFor Loops

What is a For Loop?

A for loop is a control flow statement that allows code to be executed repeatedly based on a given condition. The loop will continue to execute as long as the condition is true. Once the condition is false, the loop will stop.

The basic syntax of a for loop in Python is as follows:

12
for i in range(5): print("Iteration:", i)
copy
  • Initialization: the range(5) function generates a sequence of numbers from 0 to 4. This implicitly initializes the loop variable i to 0 at the start of the loop;

  • Condition: the loop continues as long as there are numbers left in the sequence generated by range(5). In this case, it will iterate over 0, 1, 2, 3, and 4;

  • Increment: The loop variable i is automatically incremented by 1 after each iteration, as it progresses through the sequence generated by range(5).

ninja.py

ninja.py

index.html

index.html

preset.py

preset.py

copy

In this example, the ninja picks up sushi six times as the loop variable i goes from 0 to 5. The loop runs while i is less than 6.

Task

Swipe to start coding

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 1
ninja.py

ninja.py

index.html

index.html

preset.py

preset.py

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

bookFor Loops

Swipe to show menu

What is a For Loop?

A for loop is a control flow statement that allows code to be executed repeatedly based on a given condition. The loop will continue to execute as long as the condition is true. Once the condition is false, the loop will stop.

The basic syntax of a for loop in Python is as follows:

12
for i in range(5): print("Iteration:", i)
copy
  • Initialization: the range(5) function generates a sequence of numbers from 0 to 4. This implicitly initializes the loop variable i to 0 at the start of the loop;

  • Condition: the loop continues as long as there are numbers left in the sequence generated by range(5). In this case, it will iterate over 0, 1, 2, 3, and 4;

  • Increment: The loop variable i is automatically incremented by 1 after each iteration, as it progresses through the sequence generated by range(5).

ninja.py

ninja.py

index.html

index.html

preset.py

preset.py

copy

In this example, the ninja picks up sushi six times as the loop variable i goes from 0 to 5. The loop runs while i is less than 6.

Task

Swipe to start coding

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 1
ninja.py

ninja.py

index.html

index.html

preset.py

preset.py

some-alt