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 Pyth…


This content originally appeared on DEV Community and was authored by hrishikesh1990

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')
else:
    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.')
else:
    print(List_1)

#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. :)


This content originally appeared on DEV Community and was authored by hrishikesh1990


Print Share Comment Cite Upload Translate Updates
APA

hrishikesh1990 | Sciencx (2021-08-02T07:37:03+00:00) How to use the if not Python statement?. Retrieved from https://www.scien.cx/2021/08/02/how-to-use-the-if-not-python-statement/

MLA
" » How to use the if not Python statement?." hrishikesh1990 | Sciencx - Monday August 2, 2021, https://www.scien.cx/2021/08/02/how-to-use-the-if-not-python-statement/
HARVARD
hrishikesh1990 | Sciencx Monday August 2, 2021 » How to use the if not Python statement?., viewed ,<https://www.scien.cx/2021/08/02/how-to-use-the-if-not-python-statement/>
VANCOUVER
hrishikesh1990 | Sciencx - » How to use the if not Python statement?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/02/how-to-use-the-if-not-python-statement/
CHICAGO
" » How to use the if not Python statement?." hrishikesh1990 | Sciencx - Accessed . https://www.scien.cx/2021/08/02/how-to-use-the-if-not-python-statement/
IEEE
" » How to use the if not Python statement?." hrishikesh1990 | Sciencx [Online]. Available: https://www.scien.cx/2021/08/02/how-to-use-the-if-not-python-statement/. [Accessed: ]
rf:citation
» How to use the if not Python statement? | hrishikesh1990 | Sciencx | https://www.scien.cx/2021/08/02/how-to-use-the-if-not-python-statement/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.