In Python, not keyword is one of the logical operator. If the given condition is True it will result in False. If the given condition is False it will result in True. It is a case-sensitive keyword.
The not keyword operation is performed only with one operand. It can be used in conditional statements, loops, functions to check the given condition.
SyntaxHere, is the basic syntax of Python not keyword −
not condition
Let us consider A be the condition. If the A is True then it will return False. When A is False it will return True. Here, is the Python not keyword truth table −
A not A True False False True ExampleFollowing is an basic example of Python not keyword −
var1=1 result_1=not var1 print("The Result of not operation on",var1,":",result_1) var2=0 result_2=not var2 print("The Result of not operation on",var2,":",result_2)Output
Following is the output of the above code −
The Result of not operation on 1 : False The Result of not operation on 0 : TrueUsing 'not' in if statement
We can use not keyword along with if-statement. When the condition is False the if block of code is not executed, To make the code executable we need to make the False condition to True we use not operator before the condition.
ExampleHere, we have assigned the value to the variable it is considered as a True value so when we placed a not before the condition it resulted as False so else block is executed −
var1=45 if not var1: print("Hello") else: print("Welcome to TutorialsPoint")
Output
Following is the output of the above code −
Welcome to TutorialsPointUsing 'not' in while loop
The not can be used with while loop. We can iterate while a specified condition is not fulfilled by using the not operator in a while loop.
ExampleIn the following example we have used not in while loop −
def prime_num(n): i = 2 while not i > n: for j in range(2, i//2): if i%j == 0: break else: print(i,end=' ') i += 1 # Calling the function prime_num(10)
Output
Following is the output of the above code −
2 3 4 5 7
python_keywords.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4