DETAILED EXPLANATION OF LOOPS IN PYTHON – 6

loops in python | Innovate Yourself

OVERVIEW

In this blog, you will learn,

  • What are loops in python?
  • Types of loops in python?
  • While loop in python
  • for loop in python

Now let’s understand all about the loops in python one by one in detailed.

What are loops in python?

To understand about loops, let’s take an example and understand it in details. For example you have a task to perform the addition of two numbers(it can be fixed numbers or you can the user to enter the values). So for that you will write a small program to display the result of it in a proper format as shown below.

a,b=5,6
print("Addition of {} and {} is {}.".format(a,b,a+b))
Output:
Addition of 5 and 6 is 11.

This is all that you have learned in the previous blogs. Now, let’s modify our task a bit, now you have to perform the same task(addition of two numbers) repeatedly (let’s say for 100 times). Now, you have two options either write the code to perform the same task for 100 times or use some technique that will tell the interpreter to perform the task 100 times and write the task only one time. So, obviously you will select the second choice. But what is that technique that will tell the interpreter to perform the task 100 times and write the task only one time, that technique is known as loop.

Types of loops in python?

Now in python, we have two types of loops that will help us to achieve and code for this task. Those are:

  • While loops in python
  • For loops in python
  • Nested loops in python

For more detailed explanation and clarification on loops check this video,

Now, let’s understand about both the loops one by one in details.

While Loops in Python

Firstly, let’s understand how does a while loop works in python. To understand it check this image,

While Loops in Python

I think from the above image now you must have got an idea of what a while loop is and how it works. But let me add my words to it and to explain it in details.

So, start from the top you have entered the loop and on entering the loop you have a test expression/condition which will either give you the output as True or False, and based on the output you will be taking your next action. If the condition gives the output as True then, you will enter the loop and will perform the task assigned to you(which in our case is adding two numbers) and then it will again go up and check for the condition given to you.

Here the condition can generate same or the variable results based on the task and code written for it and will take actions repeatedly until the condition gets False and when the condition gives the result as False you will be exited from the while loop.

Here, is the syntax for the while loop,

while <condition>:
    <code-for-the-task>
else:
    <code-for-False-case>

Now, as you know how to write code for while loop, so let’s write the code for our task to perform the addition of two numbers.

a,b,count=5,6,1
while count<=100:
    print(count,"Addition of {} and  {} is {}.".format(a,b,a+b))
    count+=1
else:
    print("Addition of {} and {} has been performed 100 times")

Now, from the above example you must have got an idea that how to use the while loops in python. Also you must have learnt that how to manage the number of iterations of the while loop. Now, let’s understand about the second loop, that is, for loop.

For Loops in Python

Now let’s understand how does a for loop works in python. To understand it check this image,

For Loops in Python | Innovate Yourself

Now, from the above image you can understand and got a point that how the for loop works. Now let me add my words for the explanation.

Let’s start from the top of the image, first we enter the loop and for the for loop we first create a variable that will hold the value temporarily and on every iteration the value for the variable changes as per the iterable passed, the possible iterables are string, list, tuple, dictionary, sets, range function, etc. For every iterator as per the items in it, the value is stored one after the other on every iteration.

For example the values stored in the variable x for the given list, that is, [1,2,3,4,5] on every iteration will be 1 on first iteration, 2 on second iteration, 3 on third iteration and so on. So in this way you will be able to access all the elements of the iterable and can further use it as per the logic. For the above explanation it is clear that all the loop will run only the number of times the length of the iterator.

Here is the syntax of the for loop,

for <variable> in <iterator>:
    <code-for-the-task>
else:
    <code-after-end-of-sequence>

Now, as you know how to write code for, for loop, so let’s write the code for our task to print the values inside the sequence. There are different ways to generate the sequence/iterator, one of the commonly used way is to use range function,

for i in range(10):     # to print the values from 0 to 9
    print(i)

for i in range(5,11):     # to print the values from 5 to 10
    print(i)

for i in range(1,11,2):     # to print all the alternate values from 1 to 10
    print(i)

The above is one way of using the for loop, the other way is to use with the other datatypes/iterators, those are, string, list, tuple, dictionary or sets.

print("For loop for String iterator: ")
for i in "hello":     
    print(i)

print("For loop for list iterator: ")
for i in [1,2,3,4]:     
    print(i)


print("For loop for tuple iterator: ")
for i in (5,6,7,8):     
    print(i)

So, above were the examples for the for loop and the various ways to create a for loop. You can also use the else statement with the for loop just like we did for the while loop.

Nested loops in python

The last category is nested loop, which simply means that you can create one or more loop inside the other loop to expand the functionality. Below is the example to understand it better.

print("Prints the square of the number from 5 to 10")
for i in range(5,11):
for j in range(5, 11):
if i==j:
print(i*j)
output of nested for loops in python

Time to wrap up now. Hope you liked our content on loops in python. See you in our next blog, thanks for reading our blog and do leave a comment below to help us improve the content to serve you all of our experience with you. Stay tuned with us for more python programming contents.

Also check out our other playlist Rasa Chatbot, Internet of things, Docker, Python Programming, etc.
Become a member of our social family on youtube here.

Leave a Reply

Your email address will not be published. Required fields are marked *