In Python, not is a logical operator that evaluates to True if the expression used with it is False. This operator is used along with the if statement, called if not statements. In this article, we will learn more about this in detail.
If not statements
Let us look at this block of code –
Example 1
x = 7
if not x:
print("This is True")
else:
print("This is False")
Output
This is False
In this example, the variable x is assigned the value of 7. The expression, not x can mean True or False. Numeric zero (0), empty value or a None object assigned to a variable are all considered False, or True in Python. As x here is True, not x makes it False. Hence, the Else part of the code is executed.
Similarly, if the value of x would have been 0, the output would have been This is True.
Example 2
today='Raining'
if not today=='Raining':
print('You don’t need an umbrella')
else:
print('You need an umbrella')
Output
You need an umbrella
Here, today==’Raining’ evaluates to True. But the If not statement turns this into False, and thus the Else part of the code is executed.