Loops in Python: A loop is considered to be a group of statements that are executed in a repeated manner. In Python we can use mainly two types of loops namely for loop and while loop. Here we are trying to explain the following two loops that are used in Python:-
- while Loop
- for Loop
At first, we shall discuss the while loop in python.
While Loops in Python
The Syntax of while loop is as follows:-
while test expression:
In a while loop, first, the condition or the test expression is checked and if it is found true then the loop gets started and all the statements under the loop are executed till the condition is true. The moment the condition becomes falls the loop will stop its execution.
Also, it is important to note that if the condition is failed in the very first attempt then the loop will not be started. Let us try to explain the whole thing with the following code snippet –
While Loop in Python Example 1:-
while counter <=10:
here in the above code, we have set the initial value of the variable counter as 1- so the condition associated with while is True now – hence the loop will be started. So the output will be –
While Loop in Python Example 2:-
Here the loop will not be started at all as in the first attempt the condition will be checked. But since the counter variable is not equal to 10 so the loop will not be executed.
While Loop in Python Example 3:-
In this example, an infinite loop will be created. The reason is that although the counter variable’s value is increased 2 every time its value is not equal to 10 which means an infinite loop will be created. In such a situation if you run the code soon after starting the application your system may become hang.
For Loop in Python
So now we are about to start for loop in python
The general syntax of for loop is as follows:-
for <variableName> in <IterableObject> range(…):
Body of Loop
For loop requires an iterable object ( means which can contain multiple values) to work on.
We can also use range() to mention the starting value for <variableName> which works as a counter, upper limit and the increament/decreament as well. Let us try to discuss it through some examples.
For Loop in Python Example 1:-
orgName=”Innovative Technology Solutions”
for x in orgName range(0, len(orgName),1):
if( orgName[x]==” “):
print(“Total Words = %d “ %(Words))
Here in the above example an initial value of x will be 0 and it will continue till the last character of orgName, which is s, and in case any space is encountered then we are increasing the value of variable Words by 1.
At last we are printing the value of the variable Words. The output will be No of Words=3
Python For Loop Example 2:-
for x in Myword range( len(Myword)-1 , -1 ,-1):
In this example, the for loop in python will start its iteration from the last character and it will be continued till the first character – means that the iteration will be in the backward direction and hence in the range() the third parameter is set to -1.
So the output will be – atikiN
I hope you are clear about both while loop & for loop in python.