Python – For and while loops

Loops are used for repeating an action over and over. If you want to print the numbers from 1 to hundred, one option is using 100 print statements as follows:

print(1)
print(2)
print(3)
...
print(100)

The above code will have 100 lines. Instead, you can use a loop to do the same.

i  = 1
while i <= 100:
  print(i)
  i += 1

In this example, there are 4 steps:

  1. Initialize the variable i to 1
  2. Start a while loop with a condition that “i” should be “less than or equal to” 100
  3. print the value of “i”
  4. increment “i” by 1

The while loop executes repeated actions and we generally call them “iterations”. For each iteration of the above loop, the value is printed as well as incremented.

There are two common types of loops in Python. In most cases, you can either use For or While loop with a little bit of variation in syntax Let’s take a detailed look at each of these.

For loops

The general syntax of a “for loop” is the following:

for variable in sequence:
   statements

The “sequence” contains a list of items. When a loop starts, the first item in the list is assigned to the variable. Then “statements” within the loop are executed and the next iteration starts. This time the second item in the list is assigned to the variable and statements are again executed. This continues until all items from the sequence are assigned to the variable.

Here is an example of printing a list of numbers.

numbers = [10, 20, 30, 40, 50]
for i in numbers:
  print(i)

First, the variable “numbers” is initialized with a list of numbers. The for-loop iterates through each number and prints its value.

If you want to print the first 100 hundred numbers using a for a loop, you can use the range() function. This function returns a sequence of numbers that we can use in a loop.

for i in range(100):
   print(i+1)

The default action of range() function is to start from 0, increment by 1, and stop before the specified number. The specified number for range(100) is 100 hence it will return a sequence from 0 to 99. We increment “i” by 1 since we want to start printing from 1 to 100.

While loop

Just like the for-loop, while-loop is used for executing repeated actions. The difference is that while-loop runs based on a condition. If the condition is met, the while-loop will run otherwise it won’t.

The general syntax for a while-loop is the following:

while expression:
  statements

When the while-loop is run, the expression is evaluated. If it is True, the statements are executed. This continues till the expression is False.

As you can see, it is very easy to get into an indefinite or never-ending loop with a while statement so be careful when you use it.

Here is how you will count-down from 10 using a while loop.

start = 10
while start > 0:
  print(start)
  start =- 1

We start by initializing a variable to 10 and then the loop is started. The expression “start > 0” is checked. If it is true, the statements are executed. In the first iteration, the value of start is 10 so the expression is true. The value is printed and start is decremented by 1. The iterations are continued till the value of start drops down to 1. At this stage, the expression is not True anymore so the loop is broken.

Break and continue

You can end the loop by using the “expression” in a while-loop or proper set of sequences in the for-loop. You can also forcefully break the loop using aptly named “break” statement.

Here is another way to print the first 100 numbers using while-loop with a break statement.

i = 1
while True:
   if i > 100:
     break
   print(i)
   i += 1

In this case, an expression “True” is used indicating that while-loop will run for ever. However, the loop will break if the value of “i” goes above 100.

The continue statement is used to end the execution of the current statement and starts the execution again from the top of the loop.

This can be used for selectively executing certain statements with a loop.

for i in range(100):
   if i%2:
     continue
   print(i+1)

This code prints only the odd numbers ( 1, 3, 5, 7 …), and this is done by doing a check. The modulus(%) operator is used to check whether the number is odd or even. If the number is even, the continue statement is called which skips the print statement and starts the execution from the top of the loop.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *