1- For - Else
1+ `` for/else ``
22----------
33
44Loops are an integral part of any language. Likewise ``for `` loops are
55an important part of Python. However there are a few things which most
6- beginners do not know about them. We will discuss a few of them one by
7- one.
6+ beginners do not know about them. We will discuss a few of them one-by-one.
87
9- Let's first start of by what we know. We know that we can use for loops
8+ Let's first start off with what we know. We know that we can use `` for `` loops
109like this:
1110
1211.. code :: python
@@ -19,24 +18,24 @@ like this:
1918 # Banana
2019 # Mango
2120
22- That is the very basic structure of a for loop. Now let's move on to
21+ That is the very basic structure of a `` for `` loop. Now let's move on to
2322some of the lesser known features of ``for `` loops in Python.
2423
25- ``else `` clause:
24+ ``else `` Clause
2625^^^^^^^^^^^^^^^^^^^^
2726
28- For loops also have an ``else `` clause which most of us are unfamiliar
29- with. The ``else `` clause executes when the loop completes normally.
30- This means that the loop did not encounter any ``break ``. They are
31- really useful once you understand where to use them. I myself came to
27+ `` for `` loops also have an ``else `` clause which most of us are unfamiliar
28+ with. The ``else `` clause executes after the loop completes normally.
29+ This means that the loop did not encounter a ``break `` statement . They are
30+ really useful once you understand where to use them. I, myself, came to
3231know about them a lot later.
3332
3433The common construct is to run a loop and search for an item. If the
35- item is found, we break the loop using ``break ``. There are two
34+ item is found, we break out of the loop using the ``break `` statement . There are two
3635scenarios in which the loop may end. The first one is when the item is
3736found and ``break `` is encountered. The second scenario is that the loop
38- ends. Now we may want to know which one of these is the reason for a
39- loops completion. One method is to set a flag and then check it once the
37+ ends without encountering a `` break` statement . Now we may want to know which one of these is the reason for a
38+ loop's completion. One method is to set a flag and then check it once the
4039loop ends. Another is to use the ``else `` clause.
4140
4241This is the basic structure of a ``for/else `` loop:
@@ -64,8 +63,7 @@ documentation:
6463 break
6564
6665 It finds factors for numbers between 2 to 10. Now for the fun part. We
67- can add an additional ``else `` block which catches the numbers which are
68- prime and tells us so:
66+ can add an additional ``else `` block which catches the numbers which have no factors and are therefore prime numbers:
6967
7068.. code :: python
7169
0 commit comments