{"id":24167,"date":"2021-10-14T09:16:53","date_gmt":"2021-10-14T03:46:53","guid":{"rendered":"https:\/\/python-programs.com\/?p=24167"},"modified":"2021-11-05T19:28:50","modified_gmt":"2021-11-05T13:58:50","slug":"python-program-for-how-to-use-while-not","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-how-to-use-while-not\/","title":{"rendered":"Python Program for How To Use While Not"},"content":{"rendered":"
In the previous article, we have discussed Python Program for trunc() Function<\/a> 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.<\/p>\n Python’s While Not loop:<\/strong><\/p>\n 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.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n If the variable is not iterable, use the syntax “while variable not in” iterable to execute the loop’s body.<\/p>\n output:<\/strong><\/p>\n break and continue statements in Python:<\/strong><\/p>\n The Python break statement terminates a loop completely.<\/p>\n The Python continue statement terminates the current loop iteration immediately.<\/p>\n While else clause in Python:<\/strong><\/p>\n We can use the else statement to run a block of code only once when the condition is no longer true.<\/p>\n Syntax:<\/b><\/p>\n When the while loop ends, the additional statement(s)> specified in the else clause will be executed.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Find a Comprehensive Collection of Python Built in Functions<\/a> that you need to be aware of and use them as a part of your program.<\/p>\n 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 …<\/p>\n
\nIteration is the process of repeatedly running the same block of code, potentially many times. Loops are built using Python’s while statement.<\/p>\nProgram for How To Use While Not in Python<\/h2>\n
\n
Method #1: Using While Not Loop (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_numb = 8\r\n# Iterate till the given number is not equal to 0 using the while not loop.\r\nwhile not (gvn_numb == 0):\r\n # Inside the loop, print the given number.\r\n print(gvn_numb)\r\n # Decrement the given number by 1 and store it in the same variable.\r\n gvn_numb = gvn_numb - 1\r\n<\/pre>\n
8\r\n7\r\n6\r\n5\r\n4\r\n3\r\n2\r\n1<\/pre>\n
Method #2:\u00a0Using While Not Loop (User Input)<\/h3>\n
\n
# Give the number as user input using the int(input()) function and store it in a variable.\r\ngvn_numb = int(input('Enter some random number = '))\r\n# Iterate till the given number is not equal to 0 using the while not loop.\r\nwhile not (gvn_numb == 0):\r\n # Inside the loop, print the given number.\r\n print(gvn_numb)\r\n # Decrement the given number by 1 and store it in the same variable.\r\n gvn_numb = gvn_numb - 1\r\n<\/pre>\n
Enter some random number = 4\r\n4\r\n3\r\n2\r\n1<\/pre>\n
gvn_lst = [2, 6, 7, 3, 4]\r\nwhile 8 not in gvn_lst:\r\n gvn_lst.append(len(gvn_lst) + 1)\r\nprint(gvn_lst)\r\n<\/pre>\n
[2, 6, 7, 3, 4, 6, 7, 8]<\/pre>\n
while <expr><\/span>: \r\n <statement(s)><\/span> \r\nelse:<\/span> \r\n <additional_statement(s)><\/span><\/pre>\n
Method #3: Using While else Clause (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_numb = 3\r\n# Iterate till the given number is less than 8 using the while loop.\r\nwhile gvn_numb < 8:\r\n # Inside the loop print the given number.\r\n print(gvn_numb)\r\n # Increment the given number by 1 and store it in the same variable.\r\n gvn_numb += 1\r\nelse:\r\n # Else, print \"The given number is greater than 8\".\r\n print(\"The given number is greater than 8\")\r\n<\/pre>\n
3\r\n4\r\n5\r\n6\r\n7\r\nThe given number is greater than 8<\/pre>\n
\n