While loop in Python

We have some basic data types in python programming language, which we use to do our work or can say that we use to write our code in python. But for how long we have been working on single data type. Working with a single datatype like x = 10 is simply the most basic thing. Further we need to work with a lot of data types or with a lot of single data type values, in this case we require data structures, for instance  x = [1, 2, 3, 4, 5, ”hello”]  is our list, which consists of multiple elements stored in a single variable x. So these data structures which we use are actually iterables. Iterables are those on which we can perform iteration or in other words, we can say that we can iterate through/ loop through them. To do the iteration, python programming language has mainly two looping methods, one of them is for loop and other is while loop. Both has different working concept respectively. For loop works on a particularly defined range whereas while loop works on the existence of a conditional statement. While is also a keyword defined in python. It is used to state a loop. This while loop iterates if a conditional is only True. It will not do the iteration process if the conditional is False. For instance lets state a while loop and put down a simple print statement under it.

So as u can see that this while loop isn’t stopping by itself because the condition we stated is True. Here, understand the logic, we are saying that while is conditional is True, if we talk in binary conditional will be stated as 1 not 0 and we didn’t change that conditional anywhere in our small code. Which means the conditional will remain true for infinite, so this is called as an infinite loop,  a loop which never ends by itself.

But to end it after one single iteration, we can use a simple keyword called as break. Python will read the first line of while True condition then it will execute the print function after that it will see there is break keyword and it will break the while loop because of that break keyword. Here is an instance –

Now, if we want to loop up to a specified range with while loop, we need to do it something like this as beneath

Here, we stated a initial value of x as 0 then we said while x is smaller than 5, print Hello then we made an increment in the value of x, because if we don’t increase the value of x the condition we are putting of x < 5 while be true and it will be true for infinite. So we have change the value of x after every single iteration.

Conclusion –

This was a brief introduction about how we can use while loop in Python language. It is used in a normal range, specially when we have to state our conditions as True or False.


Codec Networks provides IT Trainings from EC Council CEH ECSA, LPT, CHFI, Network Security, Penetration Testing, ISACA, ISC2, PECB ISO 27001LA LI, Cisco Networking CCNA CCNP, Linux Administration RHCE, Prog Languages JAVA, Advanced Java, android development. We also offer B2B Industry Solutions and Services in IT | Information|Cyber Security in Delhi NCR India.

View all posts by

Leave a Reply

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