Editor settings

General settings

Save settings

Learn to program with Python

Reverse a boolean with not keyword #

The not keyword in Python is a logical operator that reverses the boolean value of a condition.

For example, if we have a boolean expression that evaluates to True, we can use the not keyword to reverse it to False, and vice versa.

Here are some examples on how to use not keyword:

print(not True) # Output: False

print(not 50 == 50) # Output: False

my_list = ["A", "B", "C", "D"]
print("A" not in my_list) # Output: False
print(not "A" in my_list) # Output: False

In the example above, not True is evaluated to False. And not 50 == 50 is also False, as it is equivalent to not True.

In the list examples, we can use the keywords not in to get the opposite result of in. We can also add the keyword not before the entire expression as well.


Exercise #

Add the keyword not in the code to pass the tests

Tests #

  • true equals to True
  • false equals to False
  • not_has_a equals to False
  • not_has_z equals to True
  • not_has_name equals to False
  • not_has_salary equals to True
Output will be displayed here