Home
Search
not: If Not TrueApply the not-operator to see if an expression is False. Invert the value of booleans.
Python
This page was last reviewed on Nov 16, 2022.
Not. Consider the "not" keyword in Python. With this keyword we change the meaning of expressions—it changes a true result to false.
Shows a list
Keyword info. With "not" we invert an expression in an elegant and clear way. We use not in if-statements. Sometimes, we want to flip or invert the value of a boolean variable.
Not in list. To start, we can use "not" as part of an in-expression. With the in-keyword we test for inclusion. We see if a value is in a list or a string or a dictionary.
Part 1 We test the list's contents with a "not in" operator. The string "red" is not an element of the list, so we print the message.
Part 2 The string "blue" is found in the list, so the "in" operator here returns true, and we print a message again.
In
Shows a list
colors = ["blue", "green"] # Part 1: use not in on a list. if "red" not in colors: print("NOT IN, red") # Part 2: use in operator without not. if "blue" in colors: print("IN, blue")
NOT IN, red IN, blue
If not example. In this example, we use an "if not" clause to test that a method returns False. So if equals() returns false, the if-block is entered.
Elif We can also use "not" inside an elif clause. The not-keyword can be added to the start of any expression.
Here The 3 "false" values are printed, as the 3 print statements are reached when this program executes in the Python interpreter.
def equals(first, second): # Return true if the two ints are equal. return first == second value = 10 value2 = 100 if value != value2: # This is reached. print(False) if not equals(value, value2): # This is reached. print(False) if value == 0: print(0) elif not equals(value, value2): # This is reached. print(False)
False False False
Invert booleans. With not, we can change True to False and False to True. This may help with creating a loop that alternates between True and False.
Thus We can invert our boolean value with not—we apply "not" to get the inverse of our boolean.
value = True print(value) # Change True to False with not. value = not value print(value) # Invert the value back to True. value = not value print(value)
True False True
Not, not. This is something that is not often going to be useful. But we can chain multiple "nots" at the start of an expression.
Syntax This example tells us about how the not-operator is parsed in the language. It modifies the following expression.
valid = False print(valid) # We can make something "not not." valid = not not valid print(valid)
False
Not with equality. Instead of the "!=" operator, we can use not with the "==" operator. It is probably clearer just to use the shorter form.
# Initial animal values. animal1 = "bird" animal2 = "bird" # Compare two strings. if animal1 == animal2: print(1) # Change our animals. animal1 = "lizard" animal2 = "fish" # We can use "not" in front of an equals expression. if not animal1 == animal2: print(2)
1 2
A summary. With "not" we write Python code that sounds almost like normal English. It is expressive. It is easy to read and parse for humans.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
Home
Changes
© 2007-2023 Sam Allen.