`for` Loop

Let's learn the syntax and implementation of `for` and `Foreach` loop.

What are loops?

Loops are sets of instructions used to execute a code block repeatedly. Let’s start by discussing the for loop.

for loop

We will often come across scenarios where we will need to repeat or iterate a piece of code over and over without writing the code block multiple times. The for loop is the simplest and most popular type of loop, but there are various ways in which it can be used.

Using the range() method

In Python, for loops can iterate over a number sequence using the range() function. The range function returns a list of numbers within a range that is specified as the function parameter(s).

Python Syntax for for loop:

for i in range(n):
    statement to iterate

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy