Python Program for How To Use While Not

In the previous article, we have discussed Python Program for trunc() Function
Iteration is the process of repeatedly running the same block of code, potentially many times. Loops are built using Python’s while statement.

In Python, a while loop is used to iterate over a block of code as long as the test condition) is True. The While Loop executes the set of statements as long as the condition is True. The while loop instructs a computer to perform an action as long as the condition is met or holds True.

Python’s While Not loop:

In Python, a while not loop executes the loop’s body until the condition for loop termination is met. If the condition evaluates to False, use the syntax while not condition with the condition as a boolean expression to execute the loop’s body.

Program for How To Use While Not in Python

Method #1: Using While Not Loop (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Iterate till the given number is not equal to 0 using the while not loop.
  • Inside the loop, print the given number.
  • Decrement the given number by 1 and store it in the same variable.
  • The Exit of Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 8
# Iterate till the given number is not equal to 0 using the while not loop.
while not (gvn_numb == 0):
    # Inside the loop, print the given number.
    print(gvn_numb)
    # Decrement the given number by 1 and store it in the same variable.
    gvn_numb = gvn_numb - 1

Output:

8
7
6
5
4
3
2
1

Method #2: Using While Not Loop (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Iterate till the given number is not equal to 0 using the while not loop.
  • Inside the loop, print the given number.
  • Decrement the given number by 1 and store it in the same variable.
  • The Exit of Program.

Below is the implementation:

# Give the number as user input using the int(input()) function and store it in a variable.
gvn_numb = int(input('Enter some random number = '))
# Iterate till the given number is not equal to 0 using the while not loop.
while not (gvn_numb == 0):
    # Inside the loop, print the given number.
    print(gvn_numb)
    # Decrement the given number by 1 and store it in the same variable.
    gvn_numb = gvn_numb - 1

Output:

Enter some random number = 4
4
3
2
1

If the variable is not iterable, use the syntax “while variable not in” iterable to execute the loop’s body.

gvn_lst = [2, 6, 7, 3, 4]
while 8 not in gvn_lst:
    gvn_lst.append(len(gvn_lst) + 1)
print(gvn_lst)

output:

[2, 6, 7, 3, 4, 6, 7, 8]

break and continue statements in Python:

The Python break statement terminates a loop completely.

The Python continue statement terminates the current loop iteration immediately.

While else clause in Python:

We can use the else statement to run a block of code only once when the condition is no longer true.

Syntax:

while <expr>: 
    <statement(s)> 
else: 
    <additional_statement(s)>

When the while loop ends, the additional statement(s)> specified in the else clause will be executed.

Method #3: Using While else Clause (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Iterate till the given number is less than 8 using the while loop.
  • Inside the loop print the given number.
  • Increment the given number by 1 and store it in the same variable.
  • Else, print “The given number is greater than 8”.
  • The Exit of Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 3
# Iterate till the given number is less than 8 using the while loop.
while gvn_numb < 8:
    # Inside the loop print the given number.
    print(gvn_numb)
    # Increment the given number by 1 and store it in the same variable.
    gvn_numb += 1
else:
    # Else, print "The given number is greater than 8".
    print("The given number is greater than 8")

Output:

3
4
5
6
7
The given number is greater than 8

Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.