Python for loop

In programming, ‘for’ loop is used to repeatedly execute a block of code for certain conditions.

Similarly, in Python ‘for’ loop, we iterate over Python sequences such as list, tuple, set, string, etc. This iteration helps us to execute the statements one by one for Python Sequences.

Let’s see some examples of ‘for’ loop:

Looping through a list:
With the help of for loop, we are going to print the list items.

Output of the above program is

a
b
c
d

This process is also called iteration.

Looping through a tuple: Let’s see a simple Example to loop through the Python Tuple.

Output of the above program is

a
b
c
d

Looping through a set: We can also loop through the set values by the help of the for loop. Let’s see a simple example.

Output of the above program is

a
d
c
b

Looping through a string: We can simply iterate a string as well by the help of the for loop. Let’s see a simple example.

Output of the above program is

T
u
t
o
r
i
a
l
s
C
l
a
s
s


Break statement

With the help of the break statement, we can terminate the loop. We can used it will terminate the loop if the condition is true. By the break keyword we describe the break statement.

Let’s see a simple example of break statement in the for loop:
We are going to print a series, and we want to stop the loop after executing two times.

Output of the above program is

a
b

Note: The break statement only terminates the loop, it does not completely stop the flow of the program.
Let’s see an example.

Output of the above program is

a
b
WELCOME


Python for loop – Continue statement

With the help of the continue statement, we can terminate any iteration and it returns the control to the beginning again. Suppose we want to skip/terminate further execution for a certain condition of the loop. By using the continue keyword we defines the continue statement.

Let’s see a simple example of continue statement in the for loop:
We are going to print a series, and we want to skip the loop at the second iteration where ‘b’ character found.

Output of the above program is

a
c
d

Note: The continue statement only skip on an iteration of the loop, it does not completely stop the flow of the program. Therefore, the statements outside loops will still run if exists.
Let’s see an example of it.

Output of the above program is

a
c
d
WELCOME


Python For Loop – with else statement

Like the if statement, we can also use else statement with the for loop. Let’s see the basic format of else statement with for loop :

If the condition of a for loop is satisfied then the loop statement will execute. Otherwise, the else statement will execute.
Let’s see a simple example of it:

Output of the above program is

a
c
d
finished


Python For Loop – Range Function

When we want to repeat a block of code number of times, then we use range() function.
Example of range() function:

Let’s see a simple example of range() function with the ‘for’ loop. Here, the default starting value of range is 0 if we pass only one value because the single argument will be treated as stop value.

Output of the above program is

a
b
c

Here it starts at 0 and returned till the 3rd element.

Some key points about range() function:

  • We can also specify the starting of range function such as range(2,6).
  • By default, the increment in range function’s range is 1, we can modify it as well passing 3rd argument to the function such as range(30,65,3)

Let’s see some examples of the range() function:

Output of the above program is

b
c

e
g
j


Congratulations! Chapter Finished. Do you want to practice more?
Exercises & Assignments
  • No Content Found.
  • Interview FAQs
  • No Content Found.