Python 来开个会会:Python 里面的真真假假

余嬢嬢 · 2021年03月14日 · 最后由 Mango 回复于 2021年03月16日 · 2230 次阅读

首先声明,写在这里只是我个人的一个记录方式,可能有不完美的地方,只是为了自己加深记忆。
曾经在 JD 上班的时候,有天一个同事开玩笑的说,哎呀,我今天才知道原来代码不仅仅只有 if 和 else 啊,当然,这只是一句玩笑话,但是从这句话里面,是不是也得出一个结论,那就是 if else 真的在程序员日常工作中占据大片江山呢?新需求来了,不管三七二十一,先 if 判断,在 else,不行加个 else if ,想想有点搞笑哈。这么重要的 if else,里面肯定少不了判断条件,那么 Python 里面的真真假假怎么来判断呢?😍
Python 里面用 bool 类型,来表示真假,注意大小写。咱们先来看看源代码里面对 bool 类的定义

class bool(int):
    """
    bool(x) -> bool

    Returns True when the argument x is true, False otherwise.
    The builtins True and False are the only two instances of the class bool.
    The class bool is a subclass of the class int, and cannot be subclassed.
    """
。。。

发现没,居然是继承自 int 类型的,并且上面很重要的一个说明是 and cannot be subclassed,翻译出来就是不可子类化。因为 bool 是继承自 int 的,所以 bool 类型的对象实际是有值的,False 是 0,True 是 1,并且 bool 类型的对象也可以进行计算

>>>print(True==1)
>>>True
>>>print(False==0)
>>>True
>>>print(True==2)
>>>False #看到没,2就不是True了哈,这一块后面还有比较详细的举例

bool 类型参与计算

>>>print(True+True)
>>>2
>>>print(True+False)
>>>1
>>>print(pow(True+True,3))
>>>8

这一点和其他编程语言有一点不一样。

那么 Python 里面什么是 False,什么是 Ture 呢,我们来一个一个尝试
1.None 对象

>>>print(bool(None))
>>>False  #None是False

2.数字 0,0.0,上面说过了,Python 里面 0 就是表示 False

>>>print(bool(0))
>>>False
>>>print(bool(0.0))
>>>False

3.空字符串和非空字符串

>>>print(bool(''))
>>>False#空字符串也是False
>>>print(bool('hello'))
>>>True#非空字符串是Ture哈

4.空的序列和非空序列

>>>print(bool([]))
>>>False
>>>print(bool(set()))
>>>False
>>>print(bool(list()))
>>>False  #空的元祖,空的列表依然是False
>>>print(bool([1]))
>>>True#非空列表是Ture
>>>print(bool(list([1,2])))
>>>True#非空列表是Ture
>>>print(bool((1,)))
>>>True#非空元祖是Ture
>>>print(bool(set([2])))
>>>True#非空集合是Ture

5.空的字典和非空字典

>>>print(bool({}))
>>>False
>>>print(bool(dict()))
>>>False #空字典依然是False哈
>>>print(bool({'name':'heh'}))
>>>True#非空字典是Ture

6.比较重要的一点,上面一直说的是 1 是 True,但是 2 不是,但是看代码

>>>print(bool(2))
>>>True #excus me??这时候砸变成True了,下面我尝试说清楚这个事情

bool 类型只有两个对象,一个是 True,一个是 False,True 的值就是 1,就是 1,没有其他值,但是 bool(2) 的时候,其实是调用了 bool 类里面的 init方法的,通过这个方法,把 2 转换成了 bool 类型的值 True。
代码看一下:

>>>a=bool(2)
>>>print(a)
>>>True
>>>print(int(a))
>>>1  #看到喵,把a转换成int类型的时候变成1了,实际在处理bool(2)的时候,已经把2这个值转换成bool类型的值True了,这并不是说明True等于2,True永远等于#1,耶稣来了True也是等于1的

同理 bool() 类传空字符串,空列表的时候,也是相当于创建了一个 bool 对象,传入的参数是空字符串或者空列表,然后生成的是 bool 对象,bool 对象只有 True 和 False,而 True 的值是 1,False 的值是 0.

那么问题来了,有的人可能要问,我是吃多了吗,要把这些值转换成 bool 对象,有啥子用,ok。。。
开篇我就是说了,if。。。else。。。啊,终于点题了,其实 if 在判断的时候,就是把条件转换成 bool 类型来判断的啊,这么重要的 if else 啊,代码表示一下

# coding:utf-8
if None:
    print('None is True')
else:
    print('None is False')

if '':
    print('空字符串 is True')
else:
    print('空字符串 is False')

if 0:
    print('0 is True')
else:
    print('0 is False')

if []:
    print('空列表 is True')
else:
    print('空列表 is False')

if set():
    print('空集合 is True')
else:
    print('空集合 is False')

if dict():
    print('空字典 is True')
else:
    print('空字典 is False')

输出:

None is False
空字符串 is False
0 is False
空列表 is False
空集合 is False
空字典 is False

大家看输出就明白了吧,if 在进行判断的时候,会把条件语句转换成 bool 类型的数据在判断。下面在举一下 True 的例子

# coding:utf-8
if 'hello':
    print('非空字符串 is True')
else:
    print('非空字符串 is False')

if 2:
    print('2 is True')
else:
    print('2 is False')

if [1,2]:
    print('非空列表 is True')
else:
    print('非空列表 is False')

if set([1,2]):
    print('非空集合 is True')
else:
    print('非空集合 is False')

if {'name':'nihao'}:
    print('非空字典 is True')
else:
    print('非空字典 is False')

输出:

非空字符串 is True
2 is True
非空列表 is True
非空集合 is True
非空字典 is True

其实这个 bool 类型除了用在 if 这里判断的用处,还有一个用处,就是做测试的时候的 assert 语句也是一样的逻辑,
assert 2# 这个会返回 True,这个 case 会是通过

说了太多了,总结一个:
1.bool 类型继承自 int,只有两个值,True 和 False,True 的值是 1,False 是 0
2.在做 if 判断的时候,会把条件语句转换成 bool 类型的,然后根据 bool 的值判断怎么执行,判断的依据是,''、""、0、()、[]、{}、None 为 False,其他的为 True
3.asser 做测试的时候,判断逻辑和 if 一样

好了,该去接娃儿了,就写到这里

共收到 6 条回复 时间 点赞

Pyrhon 里面的真真假假 。。

赞,总结得浅显易懂了

突然想到 if 的判断条件里面,我还少总结了两个
1.判断条件是方法或者函数的时候,这个时候就根据方法函数的返回值转换成 bool 对象的值来作为判断,有人会问了,那没有返回值的函数呢?没有返回值的函数的值是 None,None 转换成 bool 对象后是 False
代码举例

# coding:utf-8
def test_bool_no_return():
    print('no return')
def test_bool_with_return():
    print('with return')
    return 5
if test_bool_no_return():
    print('没有返回值的函数是True')
else:
    print('没有返回值的函数是False')

if test_bool_with_return():
    print('有返回值5的函数是True')
else:
    print('有返回值5的函数是False')

输出:

no return
没有返回值的函数是False
with return
有返回值5的函数是True

2.判断条件里面是对象

class Student:
    def __init__(self):
        pass

if Student():
    print('True')
else:
    print('False')

输出:

True

说明类对象是 True

🍗 清晰易懂

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册