x

Python Iteration Statements

PREV     NEXT

Before going to discuss about loops, we need to learn range() function concept in Python.


Range() Function:

  • range function is used to iterate the elements in sequence manner
  • range function is used in for loops
  • range function makes programmer to index free while iterating the loops

Range() Function arguments:

range() function has two set of arguments. They are,

1. Single argument range function:

Syntax:

range (stop)

Stop:

To generate the numbers in sequence up to “stop-1” numbers. It will start from zero.

Ex:

2. Two argument range function:

Syntax:

range (start, stop)

Start:

Generating the numbers starting from “start”

Stop:

To generate the numbers in sequence up to “stop-1” numbers .

3. Three argument range function

Syntax:

range([start], stop[, step])

Start:

Generating the numbers starting from “start”

Stop:

To generate a numbers in sequence up to “stop-1” numbers .

Step:

difference between each number in the sequence

For Loop:

A for loop that is used to iterate over the elements in a sequence manner, when we want to repeat the piece of code multiple times, then we are going to use the for loop.

Syntax:

          for data in sequence:

                   body of the loop

Use case of for loop for different data types:

Range Function:

1. For loop by using range function with single argument

Example:

Output:

0
1
2
3
4
5
6
7
8
9

2. For loop by using range function with two arguments

Example:

Output:

1
2
3
4

3. For loop by using range function with three arguments:

Example:

Output:

1
3
5
7
9

String:

To get each character in a string using for loop.

Example:

Output:

p
y
t
o
n

Example:

Output:

p
y
t
o
n

List:

To iterate the element in a list, we can use as below.


example:

Output:

1
2
3
4
5

Tuple:

To iterate the element in a tuple, we can use as below.

Example:

Output:

1
2
3
4
5

Dictionary:

To iterate the element in a dictionary, we can use as below.

Example:

Output:

1
2
3

While loop:

  • By using while loop in python, we can iterate over a block of code till test condition becomes false.
  • Once test condition became as false, then entering into the while loop body of the code will not execute, it will come out from the loop.
  • we can normally use the while loop when we don’t know how many time we can iterate the data.

Syntax:

          while condition:

                   body of the loop

How to Work with while loop?

  • When the while loop code execution comes to picture in code, first it will check the condition. If condition is true, then only it will enter into the body of the while loop. Else, it will come out from the loop and proceed with next statements.
  • Body of the while will start from indent and end with the unindent line of code. If we do not indent after writing while loop, it will not be as part of the while loop. So, indent is mandatory when you are writing the code using loops.

Here,

  • True means, it is a non-zero value
  • False means, zero or none

The following is the basic example for while loop

Example:

Output:

0
1
2
3
4

Creating infinite loop by using while loop

Example:

Output:

infinite loop
infinite loop
infinite loop
infinite loop
infinite loop
infinite loop
infinite loop
infinite loop
infinite loop

This program will keep on printing an “infinite loop” word until you terminate using kill system call by using keyboard (ctrl + c)

While loop with conditional statements:

Below example is illustrating about the use case “else” with while loop. Here, we have taken two variables sum, num and we are checking the condition up to number 10 and then we are adding the sum at every iteration as well as we increment the num. Once num became 10, then loop will terminate and else condition will execute. 

Example 1:

Output:

sum of 9 numbers is:45

Example 2:

Below example is illustrating about use case “if and else” with while loop. Here, we have taken two variables sum, num and we are checking the condition  up to number 10 and then we are adding the sum at every iteration as well as we increment the num. In this program, we intentionally breaking the loop once num is reached to 6.

Output:

sum of 6 nums is:15

PREV     NEXT



Like it? Please Spread the word!