How to Use While Not in Python

Python while not in

A while not loop in Python repeatedly executes the loop’s body until the condition for loop termination is met. Use the syntax while not condition with the condition as a boolean expression to execute the loop’s body if the condition evaluates to False.

Iteration means executing the same code block repeatedly, potentially many times.

Python’s while statement is used to construct loops. 

while loop is used to iterate over the block of code as long as the test condition) is True.

The While Loop executes statements as long as the condition is True.

The while loop tells a computer to do something if the condition is met or holds True.

Example 1

data = 5

while not (data == 0) :
  data = data - 1



You can use the syntax “while variable not in” iterable to execute the loop’s body if the variable is not iterable.

Example 2

listA = [1, 2, 3]

while 7 not in listA:
  listA.append(len(listA) + 1)



1, 2, 3, 4, 5, 6, 7]

Python break and continue Statements

Python break statement immediately terminates a loop entirely. Python continue statement immediately terminates the current loop iteration.

Python while else clause

Python concedes an optional else clause at the end of a while loop. This is a novel feature of Python, not found in most other programming languages.


while <expr>:

The <additional_statement(s)> specified in the else clause will be executed when the while loop terminates.

That’s it.

Leave a Comment

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