python下not用法小结

210 阅读1分钟

在python中not是逻辑判断词,用于布尔型True和False,not True为False,not False为True, 以下是几个常用的not的用法:

  • (1) not与逻辑判断句if连用,代表not后面的表达式为False的时候,执行冒号后面的语句。比如:
   a = False
   if not a:   (这里因为a是False,所以not a就是True)
       print "hello"
       # hello
  • (2) 判断元素是否在列表或者字典中,
# if a not in b,a是元素,b是列表或字典,这句话的意思是如果a不在列表b中,那么就执行冒号后面的语句,比如:
a = 5
b = [1, 2, 3]
if a not in b:
    print "hello"
# hello
  • (3) not x 意思相当于 if x is false, then True, else False