假设变量a持有10,变量b持有20,则-
序号 | 运算符和说明 | 例 |
---|---|---|
1 | +加法 在运算符的任一侧添加值。 | a + b = 30 |
2 | -减法 从左手操作数中减去右手操作数。 | a – b = -10 |
3 | *乘法 将运算符两侧的值相乘 | a * b = 200 |
4 | /除法 将左操作数除以右操作数 | b / a = 2 |
5 | %模量 %模量 | b%a = 0 |
6 | **指数 对运算符执行指数(幂)计算 | a ** b =幂20 |
7 | // 底数除法-操作数的除法,其结果是除去小数点后的数字的商。但是,如果其中一个操作数是负数,则结果是下限的,即从零舍入(朝负无穷大)- | 9 // 2 = 4和9.0 // 2.0 = 4.0,-11 // 3 = -4,-11.0 // 3 = -4.0 |
假设变量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