Instance Usage of the python not Operator
- 2021-11-13 08:22:00
- OfStack
Description
1. not logic is NO, which can perform NO operation on the value to the right of symbol.
2. For Boolean values, the non-operation will reverse them, and True will change to False, and False will change to True.
For non-Boolean values, the non-operation converts them to Boolean values first, and then reverses them.
The nullity value is False, and the others are True.
Instances
a = True
# If the expression does not assign a value. Will not check the variable a Have any impact,
# Just created 1 A new object stores the results,
# Same as data type conversion
not a
# Pair variable a After assignment, the expression is the result that affects the variable a .
a = not a
print(f"a = {a}") # a = False
# 4 Logical operator plus the expression around () Does not affect the result of the operation.
# Why add (), Avoid ambiguity and increase readability.
a = 1
b = 2
c = 3
print((a < b) and (b < c)) # True
print((a > b) and (b < c)) # False
print((a > b) or (b < c)) # True
print(not (a > b)) # True
Instance extension:
# Defining variables num Value is a string 123num = "123"# Defining variables num2 For int Value 1
num2 = 1#while The loop condition is, if the variable num Include string 2 Is looped and printed hehe
while "2" in num:
print("hehe")# Every cycle, num2 Will increase by itself 1
num2 += 1# When num Value is equal to 2 Stop this time while Cycle (break To interrupt this cycle )
if num2 == 3:
break# Last print over Value
print("over")# Explanation: