Python If with NOT Operator

Python if not

In python, we use conditional statements to implement logic in our program. For this, we use boolean expressions with if statements. In this article, we will see how we can use the if statement with the not operator in our python program to implement logical decisions. 

NOT Operator and IF Statement in Python

The syntax for the if statement in python is as follows.

Whenever the boolean_expression evaluates to True, the statements inside the if block is executed.

The not operator in python is used to negate the output of a boolean expression. In other words, if a boolean expression evaluates to True, we can use the not operator with it to make the output False as follows.

Output:

Here you can see that the number 10 is not greater than 20. So, the condition number>20 evaluates to False. Therefore, we have used the not operator to negate the output of the condition. Hence the expression not number > 20 becomes True.

We can use the not operator inside the if statement to conditionally execute a block of statements only if the boolean expression inside the if statement evaluates to False or is a None or empty object.

There are various operations of the if statement with the not operator in python. We will discuss them in the following section.

Applications of the if statement with not operator in python

We can use the if statement with the not operator to check if a list is empty or not. In python, an empty list, when used as a boolean expression, evaluates to False. So, using the not operator helps us to check if the list is None as the entire expression will become True if the list is empty.

For example, if we have to pop out an element from a list and the list happens to be empty, the program will run into an error as shown below.

Output:

To avoid the error, we can use the if statement in python as shown below to check if the list is empty before using the pop() method. Here, the empty list will evaluate to False and the pop() method will not be executed.

Alternatively, we can use the if not statement to notify the user that the list is empty and the element cannot be popped out as follows.

Output:

Similar to lists, we can also check for a dictionary, tuple, or set if they are empty or not using the if not statement in python.

Conclusion

In this article, we have discussed the if not statement in python. We have also discussed the application of the if not statement in python and saw how they are used to negate the output of the if statement in python.


import_contacts

You may also like:

Related Posts

  • Python copy file to another directory
    17 January

    How to copy file to another directory in Python

    Table of ContentsWays to copy file to another directoy in PythonUsing file handlingUsing the shutil libraryUsing the pathlib libraryUsing the os moduleUsing the subprocess moduleConclusion In this article, we will see different ways to copy file to another directory in Python. We can read and write files in Python. We can also work with paths […]

  • Check if variable is String in Python
    13 January

    Check if variable is String in python

    Table of ContentsHow to check if a given variable is of the string type in Python?Using the isinstance() function.Using the type() function.Check if function parameter is String In this post, we will see what is a string in Python and how to check whether a given variable is a string or not. There are many […]

  • Count the number of characters in a String in Python
    13 January

    Count number of characters in a string in python

    Table of ContentsWays to count the number of characters in a string in PythonUsing the len() functionUsing the for loopUsing the collections.Counter classConclusion In this post, we will see how to count number of characters in a String in Python. We can think of strings as a collection of characters, with every character at a […]

  • Pandas apply function to column
    12 January

    Pandas apply function to column

    Table of ContentsHow do I apply function to column in pandas?Using dataframe.apply() functionUsing lambda function along with the apply() functionUsing dataframe.transform() functionUsing map() functionUsing NumPy.square() function We make use of the Pandas dataframe to store data in an organized and tabular manner. Sometimes there, is a need to apply a function over a specific column […]

  • Copy file Python
    12 January

    How to Copy File in Python?

    Table of ContentsCopy file using the shutil module in PythonCopy file using the copyfileobj() function in PythonCopy file using the copyfile() function in PythonCopy file using the copy() function in PythonCopy file using the copy2() function in PythonCopy file using the operating system command in PythonCopy file using the os module in PythonCopy file using […]

  • What is percent in python
    11 January

    What is % in Python?

    Table of ContentsWhat is % Operator in Python?The % Operator with strings in pythonThe % Operator as a placeholder for variables  in pythonThe % Operator as format specifiers in pythonConclusion In this article, we will cover what is % in Python and what are different ways to use % in Python. In python, there are […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.