码迷,mamicode.com
首页 > 编程语言 > 详细

Python算术运算符

时间:2017-05-31 16:38:26      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:border   描述   运算   bsp   cell   cin   code   plain   function   

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算术运算符

标签:border   描述   运算   bsp   cell   cin   code   plain   function   

原文地址:http://www.cnblogs.com/0kuxia0/p/6924492.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!