While not Python | Example code

While not loop works the same as a simple while loop, it will repeatedly execute the loop’s body until the condition for loop termination is matched. Where boolean expression to execute the loop’s body if the condition evaluates to False.

count= 3


while not (count== 0) :

   print(count)

   count= count- 1

As above code loop will execute until the count is zero.

Output:

While not Python

While not Python example with iterable object

The above example code was only used a single variable. Let’s execute the loop’s body if the variable is not present in the iterable object.

a_list = [1, 2, 3]

while 3 not in a_list:
    a_list.append(len(a_list) + 1)

print(a_list)

Output: [1, 2, 3]

Do comment if you have any doubts and suggestions on this Python while loop topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

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