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':breaka = 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 分解模数

python 分解模数_Python模数相关推荐

  1. 视频教程-快速入门Python基础教程_Python基础知识大全-Python

    快速入门Python基础教程_Python基础知识大全 十余年计算机技术领域从业经验,在中国电信.盛大游戏等多家五百强企业任职技术开发指导顾问,国内IT技术发展奠基人之一. 杨千锋 ¥99.00 立即 ...

  2. Python学习教程(Python学习视频_Python学些路线):Day05 总结和练习

    Python学习教程(Python学习视频_Python学些路线):总结和练习 练习清单 寻找"水仙花数". 寻找"完美数". "百钱百鸡" ...

  3. python分解五位数

    python分解五位数 1. 题目 2. 解法一 2. 解法二 3. 解法三 1. 题目 题目:给一个不多于5位的正整数 要求:1. 求它是几位数 2. 逆序打印出各位数字. 程序分析:学会分解出每一 ...

  4. Python学习教程(Python学习视频_Python学习路线):Day04循环结构

    Python学习教程(Python学习视频_Python学习路线):循环结构 循环结构的应用场景 如果在程序中我们需要重复的执行某条或某些指令,例如用程序控制机器人踢足球,如果机器人持球而且还没有进入 ...

  5. 视频教程-快速入门Python基础教程_Python基础进阶视频-Python

    快速入门Python基础教程_Python基础进阶视频 十余年计算机技术领域从业经验,在中国电信.盛大游戏等多家五百强企业任职技术开发指导顾问,国内IT技术发展奠基人之一. 杨千锋 ¥199.00 立 ...

  6. Python学习教程(Python学习路线_Python基础学习教程_Python视频教程):初学者新手怎样快速入门Python

    Python学习教程(Python学习路线_Python基础学习教程_Python视频教程):初学者新手怎样快速入门Python? 人生苦短,我用Python!!!短短几个字,现在在各大学习类平台随处 ...

  7. python分解质因数递归_Python 正整数分解质因数具体实现附代码

    时间:2019-02-02 概述:分解质因数 Python将一个正整数分解质因数.例如:输入90,打印出90=2*3*3*5.解题思路分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤 ...

  8. python分解完数_Python练习题 014:完数

    [Python练习题 014] 一个数如果恰好等于它的因子之和,这个数就称为"完数".例如6=1+2+3.编程找出1000以内的所有完数. -------------------- ...

  9. python差分方程求解_Python数值计算----------二维波动方程有限差分解

    波动现象在生活中非常常见,比如你随便扔一颗石子到平静的湖面上,一圈圈的波纹图案就会出现.波动现象的控制方程为波动方程,下面不要眨眼,请欣赏美丽的波纹:正方形域内波反射图案矩形区域波反射图案三角形区域( ...

最新文章

  1. bootstrap的两种在input框后面增加一个图标的方式
  2. MySql入门使用:登录及简单创建查询表
  3. 全球首届“AI球球大作战:Go-Bigger多智能体决策智能挑战赛”开启
  4. 大数据容器化-基于Kubernetes(k8s)构建spark运行环境
  5. 2-SAT适定性(Satisfiability)问题知识点详解
  6. centos安装 node.js
  7. 通用汽车CES官宣电动皮卡 追赶福特、Rivian
  8. python distance matrix_Python 矩阵转置的几种方法小结
  9. CakePHP 2.x CookBook 中文版 第三章 入门(三)
  10. centos安装python3.7和yum报错解决方法
  11. asp.net mvc在Model中控制日期格式
  12. 社区团购微信小程序开发
  13. java高校贫困生助学贷款系统ssm框架毕业设计
  14. 作为项目经理如何开展BI项目
  15. 结构化数据、半结构化数据和非结构化数据分析
  16. PAT考试经验总结(甲乙级均适用)~~想满分的请看这里!~~
  17. 批处理文件(bat)装逼 之全彩滚动我爱你 绘制五彩爱心 绘制3D球体
  18. 软件实施前后准备工作(软件实施工程师)
  19. 从零实现一个RPC框架系列文章(二):11个类实现简单RPC
  20. ●UVA 11021 tunnello

热门文章

  1. android 之反编译
  2. ADO.NET2.0 Querying Large Result Sets Asynchronously(ADO.NET 异步操作)
  3. [转载] python自带sqlite库_Python内置库SQlite3使用指南
  4. Entity Framework 与 面向对象
  5. Python:Django 项目中可用的各种装备和辅助
  6. Python递归、反射、2分查找、冒泡排序
  7. OTL翻译(4) -- otl_stream类
  8. sql索引的填充因子多少最好,填充因子的作用?
  9. [Leetcode][JAVA] Populating Next Right Pointers in Each Node II
  10. Separate texture from black background