Python Using For Loop

Sovary June 4, 2022 300
2 minutes read

We have understand how while loop is work, the same as while loop. This article you will understand between using while loop and for loop in Python. I will explain step by step with simple concept for beginner.

thumbnail_python_for_loop_usage_example

For Loop Statement

"For loop" is used to repeated block of code and iterate over sequence. For loop is required specific number of time to run which is different from while loop.

Syntax

for one_element in multi_element :
    # to do statment

Example #1:

# name list
names = ["Heng","John","Sok","Sao","Dara"]
for item in names :
    print("item=> ",item)

Output #1:

item=>  Heng
item=>  John
item=>  Sok
item=>  Sao
item=>  Dara

Explain #1:

Line 3: the loop start take one element from names variable and store in item variable and print out the item and start repeat the loop. This for loop does not required index.

For Loop with range() Function

Using for loop with range() function we can loop through bunch of elements with specified number of times. range() function generate a sequence of numbers with parameters.

range(start,stop,step) we pass parameters with start index by default is zero, where to stop and by default step to run is one.

Example #1:

for i in range(5):
  print(i)

Output #1:

0
1
2
3
4

range(5) the second parameter is requred that is to know where to stop by default start value is zero and the result should list in 0-4 which is 5 elements.

Example #2:

for x in range(3, 6):
  print(x)

Output #2:

3
4
5

range(3, 6) third parameter default is one range with start value 3 and stop at 5

Example #3:

for x in range(3, 6, 2):
  print(x)

Output #3:

3
5

range(3, 6, 2) the step define as 2 and initialize value is 3 plus with step 2 is 5, and next loop is out of range so print line only 3 and 5

For Loop with Else

We can use else with for loop statement which is executed after loop is finished.

Example #1:

people = ["Heng","John","Sok","Sao","Dara"]
# get length of list which is 5
size = len(people)

for i in range(size) :
    print("My name is",people[i])
else :
  print("There are",size,"people")

Output #1: 

My name is Heng
My name is John
My name is Sok
My name is Sao
My name is Dara
There are 5 people

Using else with loop will not executed if the loop interrupted by break statement

Example #2:

for i in range(5):
  if i == 2:
    break
  print(i)
else :
  print("Not print because break statment")

Output #2:

0
1

Nested For Loop Statement

We can write while loop inside other for loop statement. The combination help to solve problem in different algorithm.

Example:

# first loop
for i in range(3):
  # second loop
  for j in range(2):
    print (i,j)

Output:

0 0
0 1
1 0
1 1
2 0
2 1

We can use loop with multiple levels, but becareful the more levels more resources are expand.

Hope you learn and help you to understand basic how to using for loop statement in Python.

Python 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him