python 分解模数_Python模数

python 分解模数

Good day, learners. In our previous tutorial, we learned about Python NumPy Module. In this tutorial we are going to see examples of Python Modulo.

美好的一天,学习者。 在上一教程中,我们了解了Python NumPy模块。 在本教程中,我们将看到Python Modulo的示例。

Python模数 (Python Modulo)

Basically Python modulo operation is used to get the reminder of a division. The basic syntax of Python Modulo is a % b. Here a is divided by b and the remainder of that division is returned.

基本上,Python模运算用于提醒除法。 Python Modulo的基本语法是a % b 。 在此, a除以b然后返回该除法的其余部分。

In many language, both operand of this modulo operator has to be integer. But Python Modulo is flexible in this case. The operands can be either integer or float.

在许多语言中,此模运算符的两个操作数都必须为整数。 但是Python Modulo在这种情况下非常灵活。 操作数可以是integerfloat

Python Modulo示例 (Python Modulo Example)

We have discussed a little bit about modulo operation in the python operators tutorial. Now we will see python modulo example here.

我们已经在python运算符教程中讨论了一些关于模运算的知识。 现在,我们将在此处看到python模数示例。

In the following program you will be asked if you want to continue the program. By pressing ‘y’ means, you want to continue. Otherwise, the program will terminate instantly.

在以下程序中,将询问您是否要继续该程序。 通过按“ y”表示您要继续。 否则,程序将立即终止。

When you continue with the program, you have to enter two numbers and by using modulo operation, the reminder will be printed.

当您继续该程序时,您必须输入两个数字,并且使用模运算,将打印提醒。

while True:
    a = input('Do you want to continue? ')
    if a.lower() != 'y':
        break

    a = float(input('Enter a number : '))
    b = float(input('Enter another number : '))
    print(a, ' % ', b, ' = ', a % b)
    print(b, ' % ', a, ' = ', b % a)

The output will vary for different input. For my input, the output was the following:

对于不同的输入,输出将有所不同。 对于我的输入,输出如下:

Do you want to continue? y
Enter a number : 12
Enter another number : 3
12.0  %  3.0  =  0.0
3.0  %  12.0  =  3.0
Do you want to continue? y
Enter a number : 2
Enter another number : 3
2.0  %  3.0  =  2.0
3.0  %  2.0  =  1.0
Do you want to continue? y
Enter a number : 1.2
Enter another number : 2.1
1.2  %  2.1  =  1.2
2.1  %  1.2  =  0.9000000000000001
Do you want to continue? n

Python Modulo运算符异常 (Python Modulo Operator Exception)

The only Exception you get with python modulo operation is ZeroDivisionError error. This happens if the divider operand of the modulo operator become zero. That means the right operand can’t be zero. Let’s see the following code to know about this python exception.

使用python模运算获得的唯一异常是ZeroDivisionError错误。 如果取模运算符的除法操作数变为零,则会发生这种情况。 这意味着正确的操作数不能为零。 让我们看下面的代码来了解这个python exception

a = 12
b = 0
try:
    print(a, ' % ', b, ' = ', a % b)
except ZeroDivisionError as z:
    print('Cannot divide by zero! :) ')

The output of the following code will be like this

以下代码的输出将像这样

That’s all about python modulo example. If you have any query feel free to use the comment box.

这就是关于python模数示例的全部内容。 如果您有任何疑问,请随时使用注释框。

Reference: Official Documentation, ZeroDivisionError

参考: 官方文档ZeroDivisionError

翻译自: https://www.journaldev.com/15651/python-modulo

python 分解模数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值