Loops in python is the idea of controlled repetition.

Loops in python is used for performing some task for repeatedly.  So, in the loop statements or the block of codes are executing sequentially.  

Generally, we all the basic idea about loops. In the tradition format of loop is first you have to put initialization then condition and then increment and decrements. Right!!


                     For (initialization; condition; increment-decrement)

                    {

                                 //block of code

                    }


Same here in python you will put initialization, condition and increment and decrement. And also, we all know that how to declare that.


But in increment and decrement section there is the condition. Normally for increment and decrement we are using ++, +=, + 1. for e.g.  i++ , i += 1 or i = i + 1


In Python ++ thing is not working. So therefore, you have to can use i += 1 or i = i + 1

With the use of : (colon) you can start the body of loop.

And the cursor is automatically blink in the body. If you remove that space and start the code exact under the for loop then the code is count as out of loop.

There are few types of loops:

  • For loop
  • While loop
  • Break
  • Continue


1. For loop:

  • For loop is used when we want to iterate the values from list, tuples, dictionaries.
  • And it iterates until the end of sequence of number.

       Syntax:

            for <<new variable>> in <<Sequence>>:

                        #block of code

     Example:

for

In Above exampleList is the list. And the list is pass into the for loop and print it. And after that the print(‘’I am out of loop”) is the out of loop.

Range()  in for loop:

  • I discuss about range() in my another blog.

      Example:

for_range

For loop with else:

  • You can also use for option “else” in the for loop. Else part is execute only when the loop is completed.
for_else

Nested For loop:

  • The one for loop is inside the another for loop is called nested for loop.

Example:

nested_for

2. While loop:

  • While loop is used when we want to iterate the block of codes repeatedly executed until the given condition become false.
  • In while loop first check the condition, if condition is true then it will execute the inside code.
  • If the condition is false then it will terminate the loop and jump to next line after end of the loop.

       Syntax:

                   While << condition >> :

                                   # body of while

       Example:

while

While loop with else block:

while_else

Nested while loop:

nested_while
loops in python

3. Break Statement:

  • Break statement is used when we want to terminate the loop at certain condition is met.
  • You can use break statement in loops and condition statements also like (if, if..else..etc).

      Syntax:

                         break

     Example:

  • Let’s say, you are finding 5 number in the list. So, the loop is executed util we find 5 number. When we find, break is executing and it terminate the loop. And come out from the loop.
break_stmt

4. Continue Statement:

  • Continue statement is used in the loop. It’s terminated the current statement and jump to the next starting line.
  • We can use continue statement when we want to skip come type of output from the loop.

       Syntax:

                        continue

      Example:

continue_stmt