How to Loop in Python

Posted on

Introduction

Another important feature to have in a programming language is the existence of loop functionality. Fortunately, like any other general programming language, Python also have that loop functionality. In this article, the main focus is to discuss about loop functionality in Python programming language. Actually, there are several reserved keyword available in Python programming language which has the function for performing looping. Those are the for reserved keyword and also the while reserved keyword. Both of them exist in Python programming language to provide looping functionality. In this context, looping functionality is a feature which is allowing process execution several times right from the start to the end and then continue again from the start to the end multiple times like a loop.

How to use loop in Python

Actually, in order to use loop, there must be a requirement for executing certain process in a certain amount of times. This kind of loop is using an exact reserved keyword in Python programming language. Normally, the ‘for’ loop is possible to perform that kind of looping with a certain amount of times or repetitions. Or in the other hand, there is also another kind of looping without having to define the number or the amount times of the execution processes. So, it is an execution for certain process as long as it meet the requirement. So, the number of the times for the execution do not have a clear definition. That kind of loop without having to define the number of the execution times is available in Python programming language using ‘while’ loop. Although, in some case, the while loop can also be have an exact number of repetition times. Especially if the condition for repeating the process is in the numerical description in the ‘while’ loop.

How to use for to loop in Python

So, first of all is the loop functionality using ‘for’ reserved keyword. As exist in the previous definition, normally the ‘for’ reserved keyword is fit with the looping process with a definite number of times. Before getting on to the real example on the Python programming language basically the source code using ‘for’ reserved keyword, below is the pseudo code statement :

for identifier in range_collection : 
    perform_action

The following is an example using the ‘for’ reserved keyword for performing the loop function :

for x in range(6) : 
    print("This is ",x+1)

Using the above source code, the following is the execution of that source code in order to perform loop processes :

C:\>python
Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> for x in range(6):
... print("This is execution number : ",x+1);
...
This is execution number : 1
This is execution number : 2
This is execution number : 3
This is execution number : 4
This is execution number : 5
This is execution number : 6
>>>

In the above command execution, x acts as the identifier as the increment factor for the looping process. The translation to the source code above are just iterate all the process exist in the for block. The number of the iteration is decided by the increment value of the identifier which in this context it is ‘x’. How many or how long does it take for the iteration takes place ?. The limit for the iteration actually exist in the ‘range(6)’. It means, as long as the x identifier’s increment process does not surpass the range, just perform the execution. In this context, the range will start from 0 to 6. In this case, to avoid confuse in the number of times the execution run, the above value from the ‘x’ identifier is added by 1.

How to use while to loop in Python

The second one is the loop functionality using the ‘while’ reserved keyword. It is also useful for performing repeatable action or process in a certain number of times. Before getting in further to the Python programming language representing the source code, below is the pseudo code of the scenario for the example :

while the_condition_or_requirement_is_met : 
    perform_action

Using the above pseudo code, the following source code is the translation for it in order to show an example :

i = 0;
while i < 6 : 
    print("This is execution number : ",i+1);
    i+1;

The above source code is actually will have the same output with the previous source code. But instead of using ‘for’ loop, the above is using ‘while’ loop. So, using the above source code, the following is the actual execution of it :

>>> i = 0;
>>> while i < 6 :
... print("This is execution number : ",i+1);
... i+=1;
...
This is execution number : 1
This is execution number : 2
This is execution number : 3
This is execution number : 4
This is execution number : 5
This is execution number : 6
>>>

Leave a Reply