HomeSearch

Python not: If Not True

Apply the not-operator to see if an expression is False. Invert the value of booleans.

Not.

A shadow is the absence of light. Consider the "not" keyword in Python. With this keyword we change the meaning of expressions.

Essentially we invert

an expression, so if it is False it is now True. We use not in if-statements. Sometimes, we want to "flip" or invert the value of a boolean variable.

And: We can invert our boolean value with not—we apply "not" to get the inverse of our boolean.

In: Not can be used with the in-keyword. We often make "not in" tests with an if-statement in Python.

In

If not example.

In this first 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.

Python program that uses not method call 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) Output 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.
Python program that uses not to invert booleans 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) Output True False True

Not in.

With the in-keyword we test for inclusion. We see if a value is in a list or a string or a dictionary. We can use "not" as part of an in-expression.
Python program that uses not in colors = ["blue", "green"] # Use not in on a list. if "red" not in colors: print(True) Output 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.

Python program that uses not not valid = False print(valid) # We can make something "not not." valid = not not valid print(valid) Output 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.
Python program that uses not equals # 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) Output 1 2

Summary for not.

With "not" we write Python code that sounds almost like normal English. It is expressive. It is easy to read and parse for humans.
Home
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, info@dotnetperls.com.