What does “variable or 0” mean in python?

后端 未结 4 843
执笔经年
执笔经年 2020-12-09 16:04

What is the meaning of the following statement in python:

x = variable_1 or 0

variable_1 is an object. What value does x

4条回答
  •  一个人的身影
    2020-12-09 16:30

    x will be 0 if variable_1 evaluates as false, otherwise it will be variable_1

    >>> 'abc' or 0
    'abc'
    >>> '' or 0
    0
    >>> ['a', 'b', 'c'] or 0
    ['a', 'b', 'c']
    >>> [] or 0
    0
    

提交回复
热议问题