我们在数学里也学过,0是不能做除数的。在Python中也是一样,如果你把0当成除数的话,Python就要报错。
例如:
print(1/0)
运行结果是:
Traceback (most recent call last):
File "/tmp/compilebox.pwZSOR/usercode/file.py",
line 1, in <module>
print(1/0)
ZeroDivisionError: division by zero
又一例:
print(2%0)
其运行结果是:
Traceback (most recent call last):
File "/tmp/compilebox.3YIHom/usercode/file.py",
line 1, in <module>
print(2%0)ZeroDivisionError: integer division or modulo by zero
0不能做除数,但可以做被除数。
print(0/2)
其运行结果是:
0.0
又一例:
print(0%2)
其运行结果:
0