AppDividend
Latest Code Tutorials

Python If not | Difference between if not and is not in Python

0

With “not” keyword in Python, we invert an expression, so if it returns False, it is now True. We mostly use not in if-statements in Python. Sometimes, we want to invert or flip the value of the boolean variable, and in that case, if not is very useful. With not keyword, we change the meaning of expressions.

Python If not Example

Not operator along with if statement can be used to execute a block of condition when the condition evaluates to FalseTo use logical not operator in Python If boolean expression, use the following syntax.

if not variable:
        # whatever statement you want to execute
	print('variable is false.')

Example

# not boolean value
data = False

if not data:
    print('data is indeed False.')

# not boolean condition
data = 19

if not data == 19:
    print('data is not 19')
else:
    print('data is 19')

Output

data is indeed False.
data is 19

In this example, first, we have defined a boolean value to the data variable, which is False.

Then we used if not condition to check if the data = False and yes data is indeed False. So it executes that block and prints the statement.

Then in the next part, we assigned the integer value 19 to the data variable and checked the condition if data is not equal to 19, but it is 19 so, it executes else block.

Python program that uses not method call

See the following code.

def equals(first, second):
    # Return true if the two ints are equal.
    return first == second


data = 19
data2 = 21

if data != data2:
    # This is reached.
    print(False)

if not equals(data, data2):
    # This is reached.
    print(False)

if data == 0:
    print(0)
elif not equals(data, data2):
    # This is reached.
    print(False)

Output

False
False
False

In this first example, we use the “if not” clause to test that a function returns False. So if equals() method returns False, then the if-block is entered.

Elif: We can also use “not” inside an elif clause. The not keyword can be added to a start of any expression.

Here: The 3 “False” values are printed, as the three print statements are reached when this program executes in the Python interpreter.

Python if not vs. Python is not or !=

Let’s see an example.

def hp(data=None):
    if not data:
        data = 21
        return data


print(hp())

In this example, we have defined a function hp() which takes data = None parameter that means at the time of the function call, even if we don’t pass any parameter it will still count as data = None.

So inside the hp() function if not data returns True because we are passing data = None in the function argument. So it will return 21.

If we pass data other than None, then it will return None because we have not written else block in the function. So it will return None in the output.

def hp(data=None):
    if not data:
        data = 21
        return data


val = 1
print(hp(val))

Output

None

Now, here is the confusion between if not and != operator in Python.

We get the different output by doing if data != None or if data is not None.

def hp(data=None):
    if data is not None:
        data = 21
        return data


val = 1
print(hp(val))

In this example, we are checking if the data is not None. The data is not None because it is 1, which is not None, so if condition returns True and assigns 21 to data and returns the data.

So if not condition checks positively towards the None value.

Whereas the is not operator checks negative towards the None value.

Yes, if data is not None is more explicit, and thus better, assuming it is indeed what you want.

That’s not always the case; there are subtle differences: if not data: will execute if data is any kind of zero or an empty container, or False. Many people do use not data where they do mean data is not None.

Empty is not equal to None in Python.

To check if an empty list is not equal to None in Python, use the following code.

def hp(data=None):
    if data is not None:
        data = [11, 19, 21]
        return data


val = []
print(hp(val))

Output

[11, 19, 21]

In this example, we are passing the empty list and check if empty is not None, which returns True. 

That means the empty list is not equal to None and returns the data list filled with values.

Python program that uses not to invert booleans

To change the value of the boolean data type, we just have to put not in front of the variable, and it switches to other boolean value. Like if the data is set to True and if we write data = not True, then data becomes False.

data = True
print(data)

# Change True to False with not.
data = not data
print(data)

# Invert the data back to True.
data = not data
print(data)

Output

True
False
True

We can invert our boolean value with not by applying “not” to get the inverse of our boolean.

Python program that uses not in

Python ‘in’ operator is used to check if a value exists in a sequence or not. It evaluates to True if it finds the variable in the specified sequence and False otherwise.

Python ‘not in’ operator evaluates to True if it does not finds a variable in the specified sequence and False otherwise.

data = ["Eleven", "Mike"]

# Use not in on a list.
if "Dustin" not in data:
    print(True)

Output

True

In this example, Eleven and Mike are on the list, but Dustin is not on the list. So we are check if Dustin is not on the list, and it returns True.

Python program that uses not not

Not not operator is something that is not often going to be useful. But we can chain multiple “nots” at the start of an expression.

data = True
print(data)

val = not not data
print(data)

Output

True
True

In this example, first, we have initialized a variable data to True and then printed that variable.

Then we have used not not operator to change the value of True two times. This means for the first time, True becomes False and second time, it becomes True again, and then we printed the val in the console.

Conclusion

In this tutorial, we have seen what is if not, and why we use if not instead of is not or !=.

Then we have seen empty is not equal to None, Python not, in, not in, and not not operators.

That is it for the Python if not statement.

See also

Python not equal operator

Python operators

Python XOR Operator

Python Comparison Operators

Python Modulo Operator

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.