How to use the if not Python statement?

In this short tutorial, we learn about the ‘if not’ Python condition statement. We also look at its various use cases along with the code.

Before we look at the if not Python statement, we will first give a small brief about the ‘Not’ operator in Python. In case you are not familiar with it, do read along. Else, please feel free to jump straight to the Solution.

Table of Contents - If not Python

Not Operator - Recap

Similar to ‘and’ & ‘or’ the ‘not’ operator is another logical operator in Python. This operator returns True if the statement is not true and vice versa. Hence the ‘Not’ operation is commonly used along with the ‘if’ statement.

Based on this functionality, the Not operator is used in two major use cases. The first case is to check if an element is not in a string or any other iterable and the second is to check if a condition is not met. In this article, we look at the latter.

Although both these requirements can be met using other methods, using the ‘not’ operator improves the readability of your code.

Why do we use the ‘If not’ Python statement?

When it comes to checking if a particular condition is not met, the ‘if not’ Python operator is used extensively in two areas.

  • To negate the output of an if statement
  • And to check if an iterable is not empty

Let us look at real-world examples of both these cases. For the first case let us assume that you have an iterable; a list that contains information of all the blocked users on your application. When a user attempts to sign in, your code could check if the user is not in the Python list. In a conventional ‘if’ statement this logic would have to be written inside the ‘else’ statement however the not operator negates the output and outputs the inverse. Thereby increasing readability by allowing the user to write the logic under the ‘if’ statement.

For the second case, empty variables or items return falsy values, and similar to the previous use case, a normal ‘if’ statement would have logic within the ‘else’ statement. The ‘Not’ operator negates the output again making the code more readable.

Code and Explanation:

Using the ‘if not’ Python statement to check if it negates the output of an ‘if’ statement.

number = 2

if not number > 3:
    print('number is greater than 3')
    print('number is not greater than 3')

#Output - number is greater than 3

As you can see, the code under the ‘if’ block was returned although the condition returned false. This is because the ‘not’ operator negated its value.

Similarly, the code to check if an iterable is empty using the ‘if not’ Python statement is as follows.

List_1 = []

if not List_1:
    print('List_1 is empty.')

#Output - List_1 is empty

The ‘if not’ Python code can be used to check if a string, dictionary, set, and even a tuple.

If not Python - Limitations and Caveats:

  • There are no major limitations while using the ‘Not’ operator, however, since the logic is inverted I would recommend practicing the ‘if not’ Python statement a few times before you actually start using it in your code.

Do let me know your thoughts in the comments section below. :)