 ## Python Loops

If a task is repetitive then its a loop. For example, if I want to print 100 for 100 times, then there are 100 loops. If I want to print all the names of class tenth 250 students, then here 250 loops are there.

Now imagine, I need to print 10000 times the addition value of 100 and 200, then the simple approach is to write print function for 10000 times. To ease the effort of writing we can copy once and paste 10000 times.

Example

print(100+200)

print(100+200)

print(100+200)

.

.

.

.

print(100+200) —-> 10000 time

Now,what if I tell you there is an alternate approach exist to handle such loop tasks and with just two line of codes we can achieve the exact result from the above example

The best approach to handle loop task is for loop

## For Loop

Syntax

for condition upto when loop should continue:

Example

Lets print addition value of 100 and 200 for 10000 times using for loop

Solution

for _ in range(10000):

print(100+200)

Output:

300

300

300

.

.

.

300 —–>10000 times

In the above example:

for _ in range(10000): In this line of code for is the main root keyword. It tells python engine the next block of code is a for loop code block. So every for loop code of block must begin with the keyword for.

The underscore is a reference name here. It holds the loop count value. You can take any name you want. Then the keyword “in” is also important. range is special function in python which sets the limit.

In simple word we are saying python to set the limit of loop 10000 .

Example

Can you print 1 to 10000 using for loop?

Solution

for number in range(1,10000):

print(number)

1

2

3

4

5

.

.

.

.

10000

Here we have set the range 1 to 10000. In the previous example we have set the only one limit 10000 but here we have set starting value limit and ending value limit. We set these two limit separated by comma. As I have mentioned above the word between for and in is a reference name to the each loop count. So if we print the number then it would print the loop count value. Therefore the above block of code is going to print 1 to 10000.

Remember:

1. for loop statement must end with colon(:)
2. Corresponding block of code after the for loop statement must follow indent (minimum one space)
3. Indent matters the most in Python programming. Otherwise it will throw error. So after if statement, for loop statement, corresponding block of code must follow indent.
4. For loop continues till the set range completes.

Is not it very powerful?

Yes it is. Alike for loop, we have while loop.

## While Loop

While loop is also used to handle tasks that are repetitive. It is slightly different than for loop. Let see how!

Syntax

while loop condition:

Until unless the loop condition is meeting, the corresponding block of code is going to execute.

Example

For example, lets print 1 to 100 using while loop.

Solution:

number=1

while number<=100:

print(number)

number+=1

In the above example I have declared the starting value number1=1. In the while statement I have set the loop condition and that is loop until unless the value of number is less than 100. Inside each loop, I printed the number. And also at the end of each loop, the value of number is incremented by 1.

Can you imagine, what will happen, If I remove the number+=1 ?

As I have mentioned above while loop is slightly different than for loop. In while loop, the while loop continues until unless the loop condition is met. Here the loop condition is the loop should continue until unless the number is less than 100 and we have declared the starting value of number is 1. So by removing number+=1 from the above code, the value of number is going to remain 1 in each loop and therefore the while loop is going to loop through for infinite period of time.

However by adding the line of code number+=1, the value of number is incremented by 1 at each loop. So a point will come when the value of number becomes 101 and it breaks the loop condition number<=100 and the while loop stops.

So when ever you use while loop you must have to be very careful. Otherwise your python program may land in infinite loop.

Example

Print 100 to 1 in reverse order using while loop

Solution

number = 100

while number>=1:

print(number)

number-=1

Output

100

99

98

97

.

.

.

1