Python算术运算符

栏目: Python · 发布时间: 8年前

内容简介:Python算术运算符

Python算术运算符

以下假设变量a为10,变量b为20:

运算符 描述 实例
+ 加 - 两个对象相加 a + b 输出结果 30
- 减 - 得到负数或是一个数减去另一个数 a - b 输出结果 -10
* 乘 - 两个数相乘或是返回一个被重复若干次的字符串 a * b 输出结果 200
/ 除 - x除以y b / a 输出结果 2
% 取模 - 返回除法的余数 b % a 输出结果 0
** 幂 - 返回x的y次幂 a**b 输出结果 20
// 取整除 - 返回商的整数部分 9//2 输出结果 4 , 9.0//2.0 输出结果 4.0

以下实例演示了 Python 所有算术运算符的操作:

#!/usr/bin/python
= 21
= 10
= 0
= + b
print "Line 1 - Value of c is " , c
= - b
print "Line 2 - Value of c is " , c
= * b
print "Line 3 - Value of c is " , c
= / b
print "Line 4 - Value of c is " , c
= % b
print "Line 5 - Value of c is " , c
= 2
= 3
= a * * b
print "Line 6 - Value of c is " , c
= 10
= 5
= a / / b
print "Line 7 - Value of c is " , c

以上实例输出结果:

Line  1 - Value of c  is 31

Line  2 - Value of c  is 11

Line  3 - Value of c  is 210

Line  4 - Value of c  is 2

Line  5 - Value of c  is 1

Line  6 - Value of c  is 8

Line  7 - Value of c  is 2

 


以上所述就是小编给大家介绍的《Python算术运算符》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Out of Control

Out of Control

Kevin Kelly / Basic Books / 1995-4-14 / USD 22.95

Out of Control is a summary of what we know about self-sustaining systems, both living ones such as a tropical wetland, or an artificial one, such as a computer simulation of our planet. The last chap......一起来看看 《Out of Control》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具