Python for loop – Examples of using for loop in Python

For loop in Python

Loops are the way to execute specified block of code again and again to a given number of times. There will be scenarios in programming life when you need one or the other type of loop in order to achieve certain task.

Two types of loops are supported in Python:

  1. for loop
  2. while loop

This chapter will explain about for loop in Python. Links for other loops can be found at bottom of page.

Structure of for loop in Python

This type of loop (for loop) will iterate over given sequence, as follow:

for loop_variable in sequence:

   statements(s) to be executed

The flow of For loop will  be, first of all if sequence has a list, for example numbers (1,2,3,4..) the first number will be assigned to loop_variable. After evaluation the block of statements will be executed and pointer will be moved to next item of list in sequence until all items are entertained. The statements to be executed must be at same indentation.

If second statement is not at same indentation or written at root (without any space) it will be treated outside of loop.

Simple example of using for loop in Python with number sequence

Output will be:





Example of for loop with string characters

Output will be:





Another example with words

Output will be







Using Python loop control statements

Loops can be controlled by using loop control statements. Following control statements are supported in Python.

1-      Break

2-      Continue

3-      Pass statement

Was this article helpful?

Related Articles

Leave A Comment?