In this Python tutorial, we will discuss the if not
Syntax in Python programming language.
But before that let’s learn about the if
and not
operators one by one, then we will discuss what is if not
Syntax in Python, and why is it used or what are its best use cases?
Table if Contents
The “not” operator in Python
The not
operator in Python, is used to perform the logical Not operation in Python. The not
is used to manipulate the Boolean values, or to changes True into False and False into True. This logical operator returns True if the statement is not True, but the value of the variable does not change. See an Example code below:
CODE :
# initialized a boolean value. a = True # type() will print the data type of var a which happens to be of class Boolean. print('Data Type of var a :',type(a),'& value of var a is: ',a) # using Not operator to change the boolean value. print(not a) print(a)
OUTPUT :
Data Type of var a : bool'> & value of var a is: True False True
In the above example, the not
operator has been used to change the value of var a
but the boolean value does not change permanently.
Frequently Asked:
- Combine For Loop & if-else statement in Python
- Multiline conditions with ‘if’ statements in Python
- The “If not” syntax in Python explained
- Python – if…elif…else statement
The “if” statement in Python
If statement is a conditional statement, that is used in every programming language. This is used to execute a command if certain criteria is matched, which is inside the indentation/block of the if block
.
Now we will discuss “If not” syntax in Python Programming language.
The “If not” statement in Python
In Python, we can use the if
conditional and not
operator together. These are mainly used in two case scenario which are:
- To change the output of If conditional : As we know the
not
operator changes/alters the value of Boolean, like if a given boolean is True, thenot
operator will return it as false. When used it with theif
block, thenot
operator alters/changes the output ofif
block.
See an example below:
CODE :
# initialized some variables with integers values. b = 10 c = 2 d = b+c print('Sum of b+c :', d) if d > 10: print('D is greater than 10') else: print(' D is not greater than 10') if not d > 10: print('D is greater than 10') else: print('D is not greater than 10')
OUTPUT :
Sum of b+c : 12 D is greater than 10 D is not greater than 10
In the above example, the d is greater than 10. In the if-else
block, you can see the if
block gets executed because the if
block says d > 10. Now in another if-else block which has not
operator in the if
block, you can see the else
block gets executed here. This is because if with not operator
alters the return value to False. Here, If
statement returns True for d>10 but the not
operator alters/changes it to False and python thinks d < 10 and that’s why code moves to else block. Below is another code example.
CODE :
b = 10 c = 2 d = b+c if not d < 10: print('D is greater than 10.') else: print('D is not greater than 10.')
OUTPUT :
D is greater than 10.
Now in the above example, the d is greater than 10, but the if-not
return False
, which would have been True
in the case of if operator only
.
- To check if given variable is empty or not : “if not” syntax can also be used to check if the given variable is empty or not. See an example code below.
CODE :
When given variable is not empty.
# initialized a list and some variables with integer values. a = [2,3,4] b = 10 c = 2 # Checking if a is empty. if not a: print(b+c) else: # executes else block if Not empty. print(a)
OUTPUT :
[2, 3, 4]
CODE :
When the given variable is empty
# initialized a list and some variables with integer values. a = [2,3,4] b = 10 c = 2 # Initialized an empty list. d = [] # Checking if a is empty. if not d: print(b+c) else: # executes else block if Not empty. print(a)
OUTPUT :
12
Summary
In this Python tutorial, we learned about If statement
and not
statements. Also we learned about “if-not” syntax in Python programming language. The not
operator changes the value to True
if the Statement is False
and vice-versa. Python considers all objects to be True
. If the objects are numeric zero
or an empty
container than it considers them as False
.
In the above examples, we saw that the when if-not
used together, it alters the return value to opposite and also this can be used to check if the given object is empty or not. We suggest you to make modifications in the above examples, and test them for better understanding. Thanks.