Loops in python

If we have to do same thing again and again then we have not to write code for each time. We will write code only once and will use that code rapidly. We do this by using loops. In Python we have two types of loops: while loop and for loop. We will start with a simple example. Lets assume we want to print numbers from 1 to 100. We already know that we have to use print() function for it.

While Loop:

In while loop, we have to define a condition which will be checked every time and until that condition is not satisfied our code will be run. In above example, we have our number is less than 101 so we will check that we print until we have not reach at 101. Lets check this example:

  1. #programe to print numbers from 1 to 100
  2. # initializing variable
  3. number = 1
  4. #checking condition
  5. while number < 101:
  6. #writing repeating code
  7. print(number)
  8. #changing condition
  9. number = number + 1

It is clear from code that we have to write condition which are same as conditions in conditional statements. Then we have to write code which we want to execute again and again. And at last change the condition which will control the code.

NOTE: We can also write condition before code (inside loop), it will not effect the program.

For loop:

It is also similar to while loop. Above example can also be written as:

  1. #programe to print numbers from 1 to 100
  2. for number in range(100):
  3. #writing repeating code
  4. print(number+1)

Here we are using for loop instead of while loop. range function of python is a set, i.e. it contains values from 0 to 99 (100 values). Every time number is initialized with next value of range. Because number is starting from 0 so we are printing number + 1.

How is it similar to while loop?

In while loop, first we check value / condition here also we check condition that number is in range. Similarly here also we are writing code inside loop and changing value of number is automatically handled by range. Look at this example:

  1. #printing the list
  2. # we are initializing a list
  3. list = ['name','harish','age',20,'number',10]
  4. # in for loop we are using list instead of range
  5. for temp in list:
  6. # priting each value of list
  7. print(temp)

Now you should understand that inside for loop statements, we have two variables:

  1. One iterable variable like list, luple, set or dictionary. This variable will assign value to another variable.
  2. Another variable which will take value ( in above example: temp)

Loops with else statement:

If you are coming from other programming language like C, C++, Java or PHP then you will be surprised that in python loops has also else statements. It is slightly different for if else statement. In loop, we check the condition if condition satisfies then code of loop will be executed otherwise code of else statement will be executed. It is obvious that code of else statement in loops will be execute every time after loop.

  1. #example of else statement
  2. # we are initializing a list
  3. list = ['name','harish','age',20,'number',10]
  4. # in for loop we are using list instead of range
  5. for temp in list:
  6. # priting each value of list
  7. print(temp)
  8. else:
  9. print('list finished')
  10. condition = False
  11. while condition:
  12. print('condition true')
  13. else:
  14. print('condition false')

Output is:

  1. name
  2. harish
  3. age
  4. 20
  5. number
  6. 10
  7. list finished
  8. condition false

Nested loops:

Same as conditional statements, you can write loop inside loop.

  1. #this is nested loop
  2. for number in range(10):
  3. while number < 5:
  4. print(number)
  5. number = number +1;
  6. # another code inside for loop
  7. print('outside while loop')

Output is:

  1. 0
  2. 1
  3. 2
  4. 3
  5. 4
  6. outside while loop
  7. 1
  8. 2
  9. 3
  10. 4
  11. outside while loop
  12. 2
  13. 3
  14. 4
  15. outside while loop
  16. 3
  17. 4
  18. outside while loop
  19. 4
  20. outside while loop
  21. outside while loop
  22. outside while loop
  23. outside while loop
  24. outside while loop
  25. outside while loop

Understanding this output is your homework. Think about it :P

Break Statement:

Assume a loop is in execution and we want to stop it then we use break statement to come out the loop. After breaking code after break statement will not be executed.

  1. # break statement
  2. number = 1
  3. # infinite loop
  4. while True:
  5. print('this is while loop')
  6. # increasing number by one
  7. number += 1
  8. if number == 5:
  9. # if number is 5 then break loop
  10. break
  11. print('loop does not break till now')

In this example, last print statement 'loop does not break till now will not be printed when number is 5.

  1. this is while loop
  2. loop does not break till now
  3. this is while loop
  4. loop does not break till now
  5. this is while loop
  6. loop does not break till now
  7. this is while loop

NOTE: If you are coming from such a programming language which has do while loop then above example is of do while loop in python because code inside the loop will always execute atleast one time.

Continue Statements:

It is used to skip some code from execution.

  1. # continue statement
  2. # for loop from 0 to 9
  3. for number in range(10):
  4. if number == 5:
  5. continue
  6. print(number)

In this example, we are skipping 5 from printing. Output is:

  1. 0
  2. 1
  3. 2
  4. 3
  5. 4
  6. 6
  7. 7
  8. 8
  9. 9


About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

In this tutorial, we will write our first python program. We will start from python interpreter and also learn how to...
First Python Program
Python is a general purpose, high-level, interpreted, interactive and object-oriented language. Easy to learn, read ...
Introduction to Python
Python has five standard data types: Numbers, Strings, Lists, Tuple and Dictionary.Every data type has its own proper...
Variable and Data Types in Python

Login or Sign up to leave comment.