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:
1 |
range(5)=[0,1,2,3,4] |
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:
1 2 3 |
for num in range(10): print num |
Output:
0
1 2 3 4 5 6 7 8 9 |
2. For loop by using range function with two arguments
Example:
1 2 3 |
for num in range(1,10): print num |
Output:
1
2 3 4 |
3. For loop by using range function with three arguments:
Example:
1 2 3 |
for num in range(1,10,2): print num |
Output:
1
3 5 7 9 |
String:
To get each character in a string using for loop.
Example:
1 2 3 |
for char in “python”: print char |
Output:
p
y t o n |
Example:
1 2 3 |
for char in 'python': print char |
Output:
p
y t o n |
List:
To iterate the element in a list, we can use as below.
example:
1 2 3 4 5 |
list = [1,2,3,4,5] for ele in list: print ele |
Output:
1
2 3 4 5 |
Tuple:
To iterate the element in a tuple, we can use as below.
Example:
1 2 3 4 5 |
tuple = (1,2,3,4,5) for ele in tuple: print ele |
Output:
1
2
3
4
5
|
Dictionary:
To iterate the element in a dictionary, we can use as below.
Example:
1 2 3 4 5 |
dic={'one':1,'two':2,'three':3} for key in dic: print dic[key] |
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:
1 2 3 4 5 6 7 |
num=0 while num < 5: print num num+=1 |
Output:
0
1 2 3 4 |
Creating infinite loop by using while loop
Example:
1 2 3 |
while True: print "infinite loop" |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
sum=0 num=0 while num < 10: sum+=num num+=1 else: print "sum of %d numbers is:%d"%(num-1,sum) |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
sum=0 num=0 while num < 10: sum+=num num+=1 if( num == 6 ): break else: print "sum of %d nums is:%d"%(num-1,sum) print "sum of %d nums is:%d"%(num,sum) |
Output:
sum of 6 nums is:15
|