There are two types of common statements used in python. You can read about the conditional statements
. In this post, we will discuss Iterative Statements in Python. Read complete post and learn about the statement that playing an important role in Python programming. So get ready for learning!
What are the iterative statements?
When this question comes in your mind, just think about a situation which is occurring again and again due to such conditions. For example, you can consider any cycle of any process which generally starts, continue with few steps and finally stop at a specific level such as seasons of a year like Winter, Summer, Monsoon etc.
word is a verb means that repetitive statements in a program which executes again and again till the condition is true. It is also known as Loop.
There are three parts of a loop.
- Iterator or Starting Point: Iterator or Starting point is a statement which assigns a value which to the variable to point a start of a loop.
- Condition: Condition specifies the condition for executing the loop.
- Update Statement: Update statement change the value of iterator in each step and become iterator for another step until the condition evaluates to false.
What are the types of iterative statements?
There are two types of iterative statements in python.
- While Loop
- For Loop
While Loop in python
While loop follows below given syntax:
As you have seen that in while loop iterator initialize the value first. Then while keyword check the value as per the condition written next to while block if it evaluates true then the body of the loop will execute and print the value of i, then the update statement updates the value of i and again these all statements executes repeatedly until the update statement changes the value of i less than 5.
An important question coming in my mind when I am teaching loops.
Why we are always using i variable in the loop generally?
The answer is, you can use any variable in loops, but variable i is a standard variable for loops and you may see this variable in many books and contents in loops. Another reason I feel like i stands for iterator that is why mostly used in loop programs.
i=int(input(“Enter the start value:”))
n = int(input(“Enter the stop value:”))
While Loop with else
In python, you can use while loop with else also. The syntax looks like as following:
print(“While loop ends”)
Infinite While Loop
The loop without an update statement is called an infinite loop. A while which has such condition which never evaluates to true false called infinite while loop.
For Loop in python
It is the most common and popular loop in python. Programmers prefer for loop over while in most of the series based programs. The reason behind this is the structure of for loop. In addition to this for loop can be used with different objects of python such as lists, dictionary, tuple, string etc. So have a look in the structure of for loop:
for variable in <object or range()>:
for i in range(1,5):
Python range() function
In the above example, I have used range(1,5) function. This function is used to generate a series of numbers specified in the parameter list. Let’s have a look the syntax of range function:
range(start, stop, step)
The python range() function accepts three parameters.
- Start: It is a number which starts a series or range. It is an optional parameter for range function. The default value is 0.
- Stop: It is a number which ends a series or range. It is a mandatory parameter for range function.
- Step: It is a number which carry forward the next step of a series of range. It is also an optional parameter of the range function. The default value is 1.
Examples of range() function
Example 1: For loop with range() function with one mendatory parameter
for i in range(10):
Example 2: For loop with range() function with two parameters
for i in range(10,18):
Example 3: For loop with range() function with all three parameters with step value 3
for i in range(10,50,5):
Example 4: For loop with range() function with reverse output
for i in range(50,10,-5):
Thank you for reading the complete post. If you have any doubts you can always comment. Like the post and share with learners.