在python中检查值是否为零

时间:2022-11-15 17:07:26

Often I am checking if a number variable number has a value with if number but sometimes the number could be zero. So I solve this by if number or number == 0.

我经常检查一个数字变量是否有值,但有时数字可能为零。我用if number或number = 0来解它。

Can I do this in a smarter way? I think it's a bit ugly to check if value is zero separately.

我可以用更聪明的方式来做吗?我认为单独检查值是否为0有点难看。

Edit

I think I could just check if the value is a number with

我想我可以检查一下这个值是不是一个数字。

def is_number(s):
    try:
        int(s)
        return True
    except ValueError:
        return False

but then I will still need to check with if number and is_number(number).

但是我仍然需要检查if number和is_number(number)。

4 个解决方案

#1


26  

If number could be None or a number, and you wanted to include 0, filter on None instead:

如果数字可以是None或number,而您想要包含0,那么就对None进行过滤:

if number is not None:

If number can be any number of types, test for the type; you can test for just int or a combination of types with a tuple:

如果数字可以是任意数量的类型,则对类型进行测试;您可以只测试int或类型与tuple的组合:

if isinstance(number, int):  # it is an integer
if isinstance(number, (int, float)):  # it is an integer or a float

or perhaps:

或者:

from numbers import Number

if isinstance(number, Number):

to allow for integers, floats, complex numbers, Decimal and Fraction objects.

允许整数、浮点数、复数、小数和分数对象。

#2


6  

Zero and None both treated as same for if block, below code should work fine.

如果代码块,下面的代码应该可以正常工作。

if number or number==0:
    return True

#3


0  

You can check if it can be converted to decimal. If yes, then its a number

你可以检查它是否可以转换成十进制。如果是,那就是一个数字

from decimal import Decimal

def is_number(value):
    try:
        value = Decimal(value)
        return True
    except:
        return False

print is_number(None)   // False
print is_number(0)      // True
print is_number(2.3)    // True
print is_number('2.3')  // True (caveat!)

#4


0  

The simpler way:

简单的方法:

h = ''
i = None
j = 0
k = 1
print h or i or j or k

Will print 1

将打印1

print k or j or i or h

Will print 1

将打印1

#1


26  

If number could be None or a number, and you wanted to include 0, filter on None instead:

如果数字可以是None或number,而您想要包含0,那么就对None进行过滤:

if number is not None:

If number can be any number of types, test for the type; you can test for just int or a combination of types with a tuple:

如果数字可以是任意数量的类型,则对类型进行测试;您可以只测试int或类型与tuple的组合:

if isinstance(number, int):  # it is an integer
if isinstance(number, (int, float)):  # it is an integer or a float

or perhaps:

或者:

from numbers import Number

if isinstance(number, Number):

to allow for integers, floats, complex numbers, Decimal and Fraction objects.

允许整数、浮点数、复数、小数和分数对象。

#2


6  

Zero and None both treated as same for if block, below code should work fine.

如果代码块,下面的代码应该可以正常工作。

if number or number==0:
    return True

#3


0  

You can check if it can be converted to decimal. If yes, then its a number

你可以检查它是否可以转换成十进制。如果是,那就是一个数字

from decimal import Decimal

def is_number(value):
    try:
        value = Decimal(value)
        return True
    except:
        return False

print is_number(None)   // False
print is_number(0)      // True
print is_number(2.3)    // True
print is_number('2.3')  // True (caveat!)

#4


0  

The simpler way:

简单的方法:

h = ''
i = None
j = 0
k = 1
print h or i or j or k

Will print 1

将打印1

print k or j or i or h

Will print 1

将打印1