文章目录

  • 1、内置函数all()、any()
  • 2、python中哪些元素是真True,哪些元素是假False
  • 3、python中的None对象
  • 4、python中的布尔(bool)值
  • 5、内置函数all()、any()代码示例


1、内置函数all()、any()

(1)、一言以蔽之:

  • all(iterable) 全部元素都要满足不为False,则返回True;即全部布尔真,才返回真;
  • any(iterable) 只要有一个元素不为False,就返回True;即有一个布尔真,就返回真;
  • 其中,0、’’、None、False都为False。 元素除了是 0、空、None、False 外都算 True。
  • iterable为可迭代对象,比如列表,元组,字符串…

(2)、 概括:

  • all(iterable)全部元素都要满足不为0、不为’’、不为False才返回True; 空对象没有元素,即没有为False的元素,所以返回True;
  • any(iterable)只要有一个元素满足不为0、不为’’、不为False就返回True; 空对象找不到任何一个元素为True,所以返回False。
  • 空对象包括[] 、() 、 ‘’ …

(3)、 注意:

- '0123'这个字符串,all函数在判断0的时候,实际上是在判断'0'字符串,
   而'0'在python中是True,所以all('0123')、all('0')都会返回True。
- 浮点型或整型的0会返回False。 
- 但all([''])返回的是False,因为['']中有一个''元素,
  空字符串''在python中是False;
- 如果还是不理解,读完下面这句话就秒懂了: 
  衡量是False还是True不是针对参数iterable,而是针对iterable中的元素。

Python中True、False概述 Python中的真假有着更加广阔的含义范围,Python会把所有的空数据结构视为假,比如、{}(空集合)、’’(空字符串)等,而与之相反的非空数据结构即为真。

2、python中哪些元素是真True,哪些元素是假False

for elenment in ['', [], (), {}, 'S', [1, 2], (1, 2), {3, 'SS'}, [''], 0, 0.0, '0', 1, 2, None]:
    if elenment:
        print(elenment, '%10s' % '---', True) 
    else:
        print(elenment, '%10s' % '---', False) 
>>>          
        --- False
[]        --- False
()        --- False
{}        --- False
S        --- True
[1, 2]        --- True
(1, 2)        --- True
{3, 'SS'}        --- True
['']        --- True
0        --- False
0.0        --- False
0        --- True
1        --- True
2        --- True
None        --- False

3、python中的None对象

在Python中None不仅仅代表False,它本身就是一个特殊的空对象,可以用来占位,比如我们可以利用None实现类似C中定义数组的方式,预定义列表的大小,实现对可能的索引进行赋值,而未赋值的索引都为None。

L = [None] * 10
print(L)
>>>  
[None, None, None, None, None, None, None, None, None, None]

4、python中的布尔(bool)值

在Python中,布尔值True和False,不仅仅可以表示真与假,甚至可以用于数学运算

print(True+1)
print(False+1)
print(True+False)
>>>  
2
1
1

即True为1,False为0,为何Python中布尔值可以进行数学运算? 我们可以利用isinstance验证其是否为整型:

print(isinstance(True, int))
print(isinstance(False, int))
>>>  
True
True

即实质上在Python中布尔值本身是整型(int),即bool类型就是int类型的子类

5、内置函数all()、any()代码示例

all(['a', 'b', 'c', 'd'])  #列表list,元素都不为空或0
>>>  True
all(['a', 'b', '', 'd'])  #列表list,存在一个为空的元素
>>>  False
all([0, 1,2, 3])  #列表list,存在一个为0的元素
>>>  False
all(('a', 'b', 'c', 'd'))  #元组tuple,元素都不为空或0
>>>  True
all(('a', 'b', '', 'd'))  #元组tuple,存在一个为空的元素
>>>  False
all((0, 1,2, 3))  #元组tuple,存在一个为0的元素
>>>  False
all([]) # 空列表
>>>  True
all(()) # 空元组
>>>  True
all('') # 空字符串
>>>  True
all([''])
>>>  False
all([None])
>>>  False
all('0123')           
>>>  True
all('0')
>>>  True
all('a b')
>>>  True


any(['a', 'b', 'c', 'd'])  #列表list,元素都不为空或0
>>>  True
any(['a', 'b', '', 'd'])  #列表list,存在一个为空的元素
>>>  True
any((0,1))  #元组tuple,存在一个为空的元素
>>>  True
any((0,''))  #元组tuple,元素都为空
>>>  False
any(()) # 空元组
>>>  False
any([]) # 空列表
>>>  False
any('')
>>>  False
any('a b')
>>>  True
any('0')
>>>  True

q = [0]
while any(q):
    q.pop()
print(q)
# after while loop, the q is [0] still.
>>>  [0]

q = [0,1,2]
while any(q):
    # print(q, sep='', end='')
    q.pop()
print(q)
# after while loop, the q is [0].
>>>  [0]

q = [None] * 8
while any(q):
    q.pop()
print(q)
# after while loop, the q is [None, None, None, None, None, None, None, None]
>>>  [None, None, None, None, None, None, None, None]

q = [0]
while q:
    q.pop()
print(q)
# after while loop, the q is []
>>>  []