How to Use While Not in Python
Iteration means executing the same block of code over and over, potentially many times. Python’s while statement is used to construct loops.
A while loop in Python is used to iterate over the block of code as long as the test condition) is True. The While Loop is used to execute the set of statements as long as the condition is True. The while loop tells a computer to do something as long as the condition is met or holds True.
While Not in Python
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.
Example of while not
data = 5 while not (data == 0) : print(data) data = data - 1
5 4 3 2 1
You can use the syntax “while variable not in” iterable to execute the loop’s body if the variable is not iterable.
listA = [1, 2, 3] while 7 not in listA: listA.append(len(listA) + 1) print(listA)
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>: <statement(s)> else: <additional_statement(s)>
The <additional_statement(s)> specified in the else clause will be executed when the while loop terminates.
That is it for while not loop in Python.