Python loop statements

Python loop statements

A loop statement used to execute a statement or group of statements multiple times based on condition.

Python has following types of loop statements :

  • While loop

  • For loop

  • Nested loop

And except it Python has following loop control statement :

  • Break

  • Continue

  • Pass

Python While loop

It will repeat the statement till the condition is true, and tests the condition before executing it.

Syntax :

while condition:
      statement1
      statement2

[pastacode lang=”python” manual=”n%20%3D%200%0A%20%20%20%20while%20n%20%3C%2011%3A%0A%20%20%20%20%20%20%20%20print(n)%0A%20%20%20%20%20%20%20%20n%20%2B%3D%201″ message=”sample program for while loop” highlight=”” provider=”manual”/]

Python For Loop

The for..in statement is another looping statement which iterates over a sequence

of objects i.e. it will go each time through an object.

To implement For loop, you need three things :

  • Initialization
  • Test of condition
  • Update(Inc/Dec)

Syntax: 

for initialization in range(start,end,increment):
    statement1
    statement2

[pastacode lang=”python” manual=”for%20i%20in%20range(1%2C%205)%3A%0Aprint%20i%0Aelse%3A%0Aprint%20’The%20for%20loop%20is%20over'” message=”Sample program with For loop” highlight=”” provider=”manual”/]

Output:

1
2
3
4
The for loop is over

Benefits of For loop

  • Number of times loop should run is known.
  • Use of a counter
  • Can use a False condition to terminate the loop.

Nested Loop in Python

Nested loop means loop in side a loop.One loop is called as inner loop and one is called as outer.

Example of while nested loops

[pastacode lang=”python” manual=”%23!%2Fusr%2Fbin%2Fpython%0A%0Ax%20%3D%202%0Awhile(x%20%3C%2050)%3A%0A%20%20%20y%20%3D%202%0A%20%20%20while(y%20%3C%3D%20(x%2Fy))%3A%0A%20%20%20%20%20%20if%20not(x%25y)%3A%20break%0A%20%20%20%20%20%20y%20%3D%20y%20%2B%201%0A%20%20%20if%20(y%20%3E%20x%2Fy)%20%3A%20print%20x%2C%20%22%20%20%20is%20%20prime%22%0A%20%20%20x%20%3D%20x%20%2B%201%0A%0Aprint%20%22You%20did%20it!%22″ message=”Finding prime numbers using nested loop” highlight=”” provider=”manual”/]

Output:

2    is  prime
3    is  prime
5    is  prime
7    is  prime
11    is  prime
13    is  prime
17    is  prime
19    is  prime
23    is  prime
29    is  prime
31    is  prime
37    is  prime
41    is  prime
43    is  prime
47    is  prime
You did it!

Which loop you should use?

  • For Loop : It is a pretest loop and that is used in expressions for initializing, testing and updating a counter. It is very convenient to use the for loop where number of times the loop should execute is known.
  • While Loop : While loop is ideal for situations where you don’t want loop to iterate if condition is false. Best example of it is checking health in a game until someone dies.

Loop control statement : Break

The break statement is used to break out of a loop statement ie. it will stop the execution of looping statement, even if condition has not become false.

Let’s see one best use of Break statement through a program. We are going to write a program that will take string as input from the user and returns length of the string. If user wants to quit then simple user will give input as quit, and program will stop the execution.

[pastacode lang=”python” manual=”while%20True%3A%0A%09str%20%3D%20raw_input(‘Enter%20something%20%3A%20’)%0A%09if%20str%20%3D%3D%20’quit’%3A%0A%09%09break%0A%09print%20’Length%20of%20the%20string%20is’%2C%20len(str)%0A%09%0Aprint%20’See%20you%20again'” message=”Program to illustrate break statement” highlight=”” provider=”manual”/]

Output:

Enter something : How are you?
Length of the string is 12
Enter something : I am good. How are you?
Length of the string is 23
Enter something : quit
See you again

Python Continue Statement

The continue statement is used to tell Python to skip the rest of the statements in the current loop block and to continue to the next iteration of the loop.

Python Pass Statement

Pass is a null statement. The difference between a comment and pass statement is that, while the interpreter ignores a comment entirely, pass is not ignored.

However, nothing happens when pass is executed. It results into no operation.

Hope You enjoyed the tutorial. Comment for further questions?

Next Tutorial :

Function in Python

One Reply to “Python loop statements”

Leave a Reply