Amogh Jasthi
PRO
last year
Amoghcountry asked

How does if not work in Python?

Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hi Amogh,

In Python, the not keyword is a logical operator that flips the value of a condition:

  • not True becomes False

  • not False becomes True

So when you use if not, you're checking if a condition is not true.

Here’s a simple example:

is_raining = True

if not is_raining:
    print("You don't need to bring an umbrella")
else:
    print("Please bring an umbrella")

In this case, is_raining is True, so not is_raining becomes False, and the code inside the else block runs.

In short, if not is used when you want to run code only when a condition is false.

If you have more questions, I am here to help.

Python
This question was asked as part of the Getting started with Python course.