Python语言学习:各种为空的情况检测

类似于其它语言的 is_emtpy 的各种检测
更新于: 2022-02-20 02:11:44

数组是否为空

  1. 利用 if 的真值检测
  2. 利用 not False 的假值检测
l1 = ["Hire", "the", "top", "1%", "freelancers"]

l2 = []

if l2:
    print("list is not empty")
else:
    print("list is empty")

#Output: "list is empty"
sl1 = ["Hire", "the", "top", "1%", "freelancers"]

l2 = []

if not l2:
    print("list is  empty")
else:
    print("list is not empty")

# Output: "list is empty"

参考