1 >>> a =[1,2,3]
 2 >>> if a:
 3     print('I found something')
 4 
 5     
 6 I found something
 7 >>> b = []
 8 >>> if not b:
 9     print('Empty')
10 
11     
12 Empty
13 >>> b = []
14 >>> if b:
15     print('Empty')
16 
17     
18 >>> a =[1,2,3]
19 >>> if not a:
20     print('I found something')
21 
22     

代码中a,b是用户自定义的2个列表,有元素和无元素。有元素列表a,if a输出结果为if函数定义的内容,表示True。if not a输出结果为空,表示False。同理列表b

空序列为假,被当作False处理。

相关文章:

  • 2021-10-11
  • 2022-01-16
  • 2021-07-03
  • 2021-11-16
  • 2022-12-23
  • 2021-07-26
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-27
  • 2022-12-23
  • 2022-12-23
  • 2021-08-27
  • 2021-08-09
  • 2021-08-23
相关资源
相似解决方案