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.
A 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.
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.