Loops in python

Hello Techies!!!

Let’s continue our journey of learning python and use it to eventually solve real world problems technically.

What does looping mean in programming?

Looping is basically executing a sequence of statements repeatedly. If we do this based on some condition that means we are controlling the loops (control loops)

There are two primitive type of loops in python :

  • while loop
  • for loop

WHILE LOOP :

By using while loop, we can execute statements as long as condition is true.

Problem : We need to print all the numbers from 1 to 20 without writing print statement 20 times.
Solution :

while loop

Let’s break it down, to understand the code in detail :

First we needed a counter variable which will keep on increasing to print all the required numbers in sequence.
We started counter variable i from 1.
while is a keyword used to specify that the looping has begun, following the keyword comes the condition which will tell how long should the loop run?
i<=20 means, loop will check if i is less than or equal to 20, if this condition returns true, then, it will enter the loop and execute the statements inside it.

Statement is print ( i ) which will print value of i to the console.

i=i+1 is increment value of i by 1.

Let's dry run our code – means to test out our code without actually executing it.

i=1
Check i<=20 , that is, 1<=20, true
print (i)
increment value of i by 1, i=1+1=2

i=2
Check i<=20 , that is, 2<=20, true
print (i)
increment value of i by 1, i=2+1=2

….. and so on till

i=20
Check i<=20 , that is, 20<=20, true
print (i)
increment value of i by 1, i=20+1=21

i=21
Check i<=21 , that is, 21<=20, false
EXIT OUT OF LOOP!!

FOR LOOP :

for loops works same as while loop. You can choose any method for looping out statements. This is mainly used for iterating over a sequence.

Let’s see how same example work using for loop.

Problem : We need to print all the numbers from 1 to 20 without writing print statement 20 times.
Solution :

for loop.JPG

Let’s break it down, to understand the code in detail :

To loop through certain set of code multiple times, we can use range() function.

range ( initial value, excluded final value, steps or incremented by )

default initial value is 0, default step is 1.

We started i from 1.
for is a keyword used to specify that the looping has begun,

range(1,21) means numbers from 1 to 20

Statement is print ( i ) which will print value of i to the console.

BREAK AND CONTINUE STATEMENTS :

This is also a very important concept to learn to understand looping and control looping better.

Break statement is used to break out of the loop.

Continue statement is used to keep on running the loop without executing the rest of the statements.

Let’s understand this more by example.

break and continue statements.JPG

Let’s break down the above solution :

As soon as the i takes the value 10, break helps to exit the loop instantly.

In the second example,

As soon as i takes the value 10, continue helps to skip all the statements after executing continue statement, that is, if you notice carefully we didn’t execute print statement when value of i becomes 10, instead print all the other values.

P.S. You can also notice how useful comments are to understand code as we read it.

There is one more concept left ( NESTED LOOPS – LOOPS WITHIN LOOPS ) to teach in looping which we will see in later tutorials.

So, stay in touch and keep learning!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.