Hello Everybody, and welcome to Hacoder. Today, we are going to discuss about Loops in Python. It mainly includes For loop and while loop.

If you have no prior knowledge of Python and still haven’t checked out our previous Python Tutorials, then click here.

So, without further ado, lets get started!

Loops in Python:-

Generally, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. There can be a situation when you want to execute a block of code several number of times.

So what the heck are Loops?

Normally, if we humans are given a task to do a particular task continuously, we will get bored after a particular interval of time. Its like “You are given a homework to write ‘Hello World’ phrase 200 or 2000 times, you will get bored after writing it 20 or 30 times.” But, the case is different with computers. Computers can perform the same task again and again without getting bored.

A loop statement allows us to execute a statement or group of statements multiple times.

 

In Python, there are generally two types of Loops:

  • For Loop
  • While Loop

Let’s study these two types of Loops in detail.

For Loop:-

The For Loop has the ability to iterate over the items of any sequence, such as a list or a string.

Let’s look at the code for For Loop.

So, in the code above, we have taken a variable f. What this f variable does is Loop through the list hacoder and print each element one by one. Here, we have used thelen() function also. The len() function will print the length of each element of the list.

Here is its logic:

The variable f will loop through the list hacoder. The first time f goes through the list, the value of f is change to hacking and in the loop, the current value of f will printed.
Then, next time f goes through the list, the value of f will change to coding and so on…

Output for the above code is:

hacking
7
coding
6
webster
7

Remember, the purpose of the above Python code is to print each element of the listhacoder and its length also.

While Loop:-

A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition is true.

The syntax of while loop is:

Here, we have used a variable named “counter” and set its initial value to 1. Then, we have placed counter under while loop, and iterated it. Every time the condition is satisfied(the value of counter is less than 10), it prints the current value of counter and increment the current value by 1.

Output:-

1
2
3
4
5
6
7
8
9

We have to use The Loop with caution because a loop becomes infinite loop if a condition never becomes FALSE. You must use caution when using while loops because of the possibility that this condition never resolves to a FALSE value. This results in a loop that never ends. Such a loop is called an infinite loop.

Also, an infinite loop might be useful in client/server programming where the server needs to run continuously so that client programs can communicate with it as and when required.

That was all about Loops in Python. Learn more about Loops in Python from the Official Python Documentation.


Any questions, comments or suggestions are welcomed.