举例讲解Python中的算数运算符的用法

yipeiwu_com6年前Python基础

下表列出了所有Python语言支持的算术运算符。假设变量a持有10和变量b持有20,则:

2015513115413853.jpg (585×361)

 例子:

试试下面的例子就明白了所有的Python编程语言提供了算术运算符:

#!/usr/bin/python

a = 21
b = 10
c = 0

c = a + b
print "Line 1 - Value of c is ", c

c = a - b
print "Line 2 - Value of c is ", c

c = a * b
print "Line 3 - Value of c is ", c

c = a / b
print "Line 4 - Value of c is ", c

c = a % b
print "Line 5 - Value of c is ", c

a = 2
b = 3
c = a**b
print "Line 6 - Value of c is ", c

a = 10
b = 5
c = 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测试线程应用程序过程解析

这篇文章主要介绍了Python测试线程应用程序过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 在本章中,我们将学习线程应用程序...

Python实现配置文件备份的方法

本文实例讲述了Python实现配置文件备份的方法。分享给大家供大家参考。具体如下: 这里平台为Linux: #!/usr/bin/python #Author:gdlinjianyi...

pygame学习笔记(3):运动速率、时间、事件、文字

pygame学习笔记(3):运动速率、时间、事件、文字

1、运动速率 上节中,实现了一辆汽车在马路上由下到上行驶,并使用了pygame.time.delay(200)来进行时间延迟。看了很多参考材料,基本每个材料都会谈到不同配置机器下运动速率...

浅谈使用Python内置函数getattr实现分发模式

本文研究的主要是使用Python内置函数getattr实现分发模式的相关问题,具体介绍如下。 getattr 常见的使用模式是作为一个分发者。举个例子,如果你有一个程序可以以不同的格式输...

Python字符串拼接、截取及替换方法总结分析

本文实例讲述了Python字符串拼接、截取及替换方法。分享给大家供大家参考,具体如下: python字符串连接 python字符串连接有几种方法,我开始用的第一个方法效率是最低的,后来看...