Comprehensive notes on Iterative Statements Python Class 11 article will help you to understand the concept on notion of iteration computation and control flow in python for class 11. This topic is applicable for Computer Science Class 11 and Informatics Practices Class 11. So let’s start it!
There are two types of common statements used in python. You can read about the conditional statements by following below given link.
In this post, we will discuss Iterative Statements Python Class 11. Read complete post and learn about the statement that playing an important role in Python programming. So get ready for learning!
Iterative Statements Python Class 11
So first of all understand the meaning of iterative statements for the article Iterative Statements Python Class 11.
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.
Iteration in python meaning
So now let’s talk about iteration in python meaning! The Iterative 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.
Parts of python iterative statements
There are three parts of a loop.
Iterator or Starting Point: An iterator or Starting point is a statement that 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:
iterator while(condition): Statements
i=1 while(i<=5): print(i) i=i+1
The output will be:
1 2 3 4 5
As you have seen that in the while loop iterator initialize the value first. Then while the keyword checks 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.
def wloop_exp(): i=int(input("Enter the start value:")) n = int(input("Enter the stop value:")) while(i<=n): print(i) i=i+1 wloop_exp()
The output will be:
Enter the start value: Enter the stop values: 5 6 7 8 9 10
Watch this video lesson to understand the concept for Iterative Statements Python Class 11:
Now the next section will say about while loop with else for Iterative Statements Python Class 11.
While Loop with else
In python, you can use while loop with else also. The syntax looks as following:
iterator while(condition): Statements else: Statements
def wloop_exp(): i=1 while(i<=8): print(i) i=i+1 else: print("While loop ends") wloop_exp()
The infinite while loop is such a loop that doesn’t have the update statement or the while condition is always true. So now read the following portion of Iterative Statements Python Class 11 to understand it.
Infinite While Loop
The loop without an update statement is called an infinite loop. A while which has such condition which never evaluates to false called infinite while loop.
def iwloop_exp(): i=1 while(i<=10): print(i) iwloop_exp()
In the next section of Iterative Statements Python Class 11 we will talk about for loop in python.
For Loop in python
The for loop iteration in python 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, dictionaries, tuple, string etc. So have a look in the structure of for loop:
for variable in <object or range()>: Statements
for i in range(1,5): print(i)
So now understand how the range() will be used in for loop in python in this article Iterative Statements Python Class 11.
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 carries forward the next step of a series of range. It is also an optional parameter of the range function. The default value is 1.
Look at the following example to understand Iterative Statements Python Class 11 in deep.
Examples of range() function
Example 1: For loop with range() function with one mandatory parameter
def floop_exp(): for i in range(10): print(i,end=" ") floop_exp()
The output will be:
0 1 2 3 4 5 6 7 8 9
Example 2: For loop with range() function with two parameters
def floop_exp(): for i in range(10,18): print(i,end=" ") floop_exp()
The output will be:
10 11 12 13 14 15 16 17
Example 3: For loop with range() function with all three parameters with step value 5
def floop_exp(): for i in range(10,50,5): print(i,end=" ") floop_exp()
The output will be :
10 15 20 25 30 35 40 45
Example 4: For loop with range() function with reverse output
for i in range(50,10,-5): print(i,end=" ") floop_exp()
50 45 40 35 30 25 20 15
Watch this video to understand how for loop can be used:
Thank you for reading the article Iterative Statements Python Class 11. If you have any doubts you can always comment. Like the post and share with learners.