Python算术运算符
假设变量a持有10,变量b持有20,则-
在运算符的任一侧添加值。
从左手操作数中减去右手操作数。
将运算符两侧的值相乘
将左操作数除以右操作数
%模量
对运算符执行指数(幂)计算
底数除法-操作数的除法,其结果是除去小数点后的数字的商。但是,如果其中一个操作数是负数,则结果是下限的,即从零舍入(朝负无穷大)-
示例
假设变量a持有21,变量b持有10,则-
#!/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