python not keyword instance usage

  • 2021-10-27 08:01:41
  • OfStack

1. The not keyword can be inverted by one Boolean value.


>>> not True
False
>>>
>>> not 1 > 0
False

2. When using if and while statements, the result of the condition is reversed.

The not keyword can be used with if statements:


if not  Condition :
 Code block 

When conditions are not met, the above if statements execute code blocks, but when conditions are met, they are not executed because the not key reverses the result.

Extension of knowledge points:

What is the specific representation of not in python:

In python, not is a logical judgment word, which is used for Boolean True and False, not True is False, not False is True, and the following are several commonly used usages of not:

(1) not is used with the logical judgment sentence if, which means that when the expression after not is False, the statement after colon is executed.

For example:


a = False
if not a: ( Here because a Yes False , so not a Is True)
print  " hello " 

The result hello can be output here

(2) Determine whether the element is in the list or dictionary, if a not in b, a is the element, b is the list or dictionary, this sentence means that if a is not in the list b, then execute the statement after the colon, such as:


a = 5
b = [1, 2, 3]
if a not in b:
print  " hello " 

The result hello can also be output here

not x means if x is false, then True, else False

There are often judgments about whether variables are None in code. There are three main ways to write them:

The first is if x is None;

The second is if not x:;

The third is if not x is None (this sentence is more clearly understood in this way if not (x is None))

The above is the python not keyword example usage details, more information about python not keyword use please pay attention to other related articles on this site!


Related articles: