“if false” in Python explained with examples

In this article, let us see how to write the logic “if false” in Python.

If you are a visual learner, here is a video we made on that topic.

There is only so much you can cover in a video, read on to learn the topic more thoroughly!

The “if false” logic is a technique used in if statements to execute something if a condition is NOT satisfied.

This is especially useful in 2 cases, let’s have a look at both of them and learn how to write the if-false logic for each.

Case#1: Checking if a condition has failed

Suppose you want to execute the if block if a particular condition has failed, you can do it as shown below.

is_shop_open = False

if is_shop_open == False:
    print("Uh-oh! The shop isn't open yet!")
    print("Welcome! The shop has opened up!")
Uh-oh! The shop isn't open yet!

All we did was, we simply checked if our condition is equal to False to make sure our condition has indeed failed.

From the above example we can see that the basic syntax is as follows:

if <condition> == False:
    # do your cool stuff here

If you are wondering what this double equal sign ( “==” ) is doing here, it is checking the equality of the objects on either side of it. If this is the first time you are coming across this “==” sign then I strongly suggest reading the article we made on that topic to get up to speed!

Python’s “==” Explained Using Examples!

Another way of writing this is

authentication = False

if not authentication:
    print("You are not authorized to proceed.")
    print("Authorization granted. Please proceed!")
You are not authorized to proceed.

Here the syntax used is

if not <condition>:
    # do your cool stuff here

Case#2: Checking if 2 objects are not equal

If your situation requires you to compare 2 objects and make sure they are not equal before proceeding, then you can implement the “if not” logic with the help of != operator (stands for “not equal to”)

So for instance, let’s say we compare 2 variables and we want to execute something when they are not equal:

a = 1
b = 2

if a != b:
    print("a is not equal to b!")
    print("a is equal to b!")

In this program, since the variables are not equal, we get the following output:

a is not equal to b!

We check if 2 objects are not equal by using the ‘!=’ operator.  It returns True if 2 objects are not equal and it returns False otherwise. It simply functions opposite to how the ‘==’ operator functions.

Let’s see another example:

num_tables = 40
num_students = int(input("How many students are there?: "))

if num_tables != num_students:
    print("The number of tables in the class is not equal to the number of students!")
    print("The number of tables and students in the class are equal!")

Depending on the answer, we get different outputs:

How many students are there?: 40
The number of tables and students in the class are equal!

How many students are there?: 35
The number of tables in the class is not equal to the number of students!

How does the ‘!=’ operator function?

If you are wondering how the != function operates, we have an interesting article on that topic that you can read here.

Python’s “!=” Explained Using Examples

The short version of the answer is, in Python, the != operator is implemented with the help of __ne__() method. Here “ne” stands for “not equal“. Almost all classes in Python will come in-built with this method.

Whenever we use the ‘!=’ operator, the Python interpreter calls this __ne__() function to do the comparison.

So when we write

object1 != object2

Python will interpret and transform the code into


Python will then run the __ne__() method and will return us either True or False.

The actual logic for the comparison of the 2 objects is implemented inside the __ne__() method of a given class. This implementation will vary depending on the datatype we are dealing with. 

Let us see a simple example of how we can directly call the __ne__() function instead of using the ’!=’ operator and get the same result!

x = 'a' 
if x.__ne__('b'):
    print ('x is not equal to b')

If you run the code above, you will get the following as the result

x is not equal to b

And with that little look behind the scenes, I will end this article!

Congratulations on making it to the end of the article, not many have the perseverance to do so!

I hope you enjoyed reading this article and found it useful!

Feel free to share it with your friends and colleagues!

If your thirst for knowledge has not been quenched yet, here are some related articles that might spark your interest!

Related Articles

Python “if” Statement with Multiple Conditions: Explained with Examples!

Python: “if not true” How to write? Explained with Examples!

If with multiple “and” conditions in Python: Examples

If with multiple “or” conditions in Python: Examples

Thanks to Namazi Jamal for his contributions in writing this article!

Photo of author
Balaji Gunasekaran
Balaji Gunasekaran is a Senior Software Engineer with a Master of Science degree in Mechatronics and a bachelor’s degree in Electrical and Electronics Engineering. He loves to write about tech and has written more than 300 articles. He has also published the book “Cracking the Embedded Software Engineering Interview”. You can follow him on LinkedIn