top of page

How to work with While and For Loop Python?

For loops python, while loops, reverse loops, double and multidimensional loops.

This page will go through different types of Looping. This, in combination with IF statements is a powerful combination and works as a great founding block to any programmer regardless of the programming language.

However, for massive loops I would consider starting to use array matrix operations instead as that will go much faster. 1 line of code to add a "column calculation" versus looping through the entire matrix.

Loops can be divided in to two types of loops: For Loops or While Loops. For Loops are definitely the ones I use the most. Whilst creating While loops can be a good thing, sometimes it can also get your code into an annoying endless circle as it is basically just a 'For loop' to infinity.

For Loops examples

# Looping 0 to 9, which is 10 times. 
for x in range(10):

the "Range" syntax above is useful for looping to a number or between certain numbers. In python, the first index is 0. which is recurring for many other python syntaxes. The range syntax works like the following.

class range(stop)

class range(start, stop[, step])

This means that we can use the second class option above to write the python statement so that our loop starts at 1 and goes to 10. Note that our first loop stopped at 9 so in order for our loop to stop and include 10 we need to change the stop number to 11 like below.

# Looping 1 to 10, which is 10 times. 
for x in range(1,11):

We can also skip numbers according to the second class for the "Range" syntax by adding one more parameter. [step]

# Looping 1 to 10, with an interval of 2, which technically gives us 9 as our last number.
for x in range(1,11,2):

Reverse Looping

# Looping 10 to 2. 9 times as last number is 2.
for x in range(10,1,-1):
# Looping 10 to 1. 10 times as last number is 1.
for x in range(10,0,-1):

Loop through a list.

# Will write out each textfield.
MyVar = ["Please", "Fix", "Thx"]
for x in MyVar:

Loop through a word.

# Will write out each letter in the word.
MyVar = "PleaseFixThx"
for x in MyVar:

Double looping (a loop in a loop.. I know, Inception right!!)

The inner y loop will be executed completely before going to the next iteration of the outer loop.

# Double looping.
for x in range(1,3):
   for y in range(1,3):
       print (x * y)
# Result: 1, 2, 2, 4

While Loop.

# need to increment i with 1 for each loop.
# otherwise this loop will continue forever.
# increment of i could also have been written as i = i + 1.
i = 1
while i < 10:
  i += 1 

Loop with IF statement.

# Will only write out the letter "F"
MyVar = "PleaseFixThx"
for x in MyVar:
   if x == "F":

Loop with Break statement.

The "Break" syntax will stop the loop execution when a condition is met.

# Will write out each letter in the word until and including the letter "F".
MyVar = "PleaseFixThx"
for x in MyVar:
   if x == "F":

Loop with Continue statement.

The "Continue" syntax skips and jumps to the next iteration.

# Will skip "Fix" and write out "Please" and "Thx"
MyVar = ["Please", "Fix", "Thx"]
for x in MyVar:
    if x == "Fix":
    print (x)

Learn more about Python here for all my posts:

If you have found this article or website helpful. Please show your support by visiting the shop below.

53 views0 comments

Recent Posts

See All


bottom of page