Three ways to judge None in Python

        1.if x is None
        2.if not x
        3.if not x is None

        In Python, None, False, empty string'', empty list[], and empty tuple() are actually equivalent to False . If x is an empty list and y is None, if you make a judgment that x is None, you will get False. If you make a judgment that not x, it will be True, that is, the empty list is actually False. Therefore, the first and second methods cannot distinguish between the cases of x==[] and x==None .

        Then use the third method to distinguish whether x is an empty list or None. Similarly, if x is an empty list, then the result of not x is None is True. If x is None, then the result of not x is None is False, so Using the third method, you can distinguish whether x is an empty list [] or None.

# Three method of judging variable is None or Not

# 1.if x is None
# 2.if not x
# 3.if not x is None

x = []
y = None
print("not x is:", not x)
print("not y is:", not y)
print("not x is None is:", not x is None)
print("not y is None is:", not y is None)

Guess you like

Origin blog.csdn.net/qq_36171491/article/details/124728283