Python赋值运算符
假设变量a持有10,变量b持有20,则-
序号 | 运算符和说明 | 例 |
---|---|---|
1 | =将 值从右侧操作数分配给左侧操作数 | c = a + b assigns value of a + b into c |
2 | + =添加AND 将右操作数添加到左操作数,并将结果分配给左操作数 | c += a is equivalent to c = c + a |
3 | -=减去AND 如果两个操作数的值不相等,则条件为true。它将从左侧操作数中减去右侧操作数并将结果分配给左侧操作数 | c -= a is equivalent to c = c - a |
4 | * =乘以AND 将右操作数与左操作数相乘并将结果分配给左操作数 | c *= a is equivalent to c = c * a |
5 | / =除以AND 将左操作数除以右操作数,并将结果分配给左操作数 | c /= a is equivalent to c = c / a |
6 | %=模数AND 使用两个操作数取模并将结果分配给左操作数 | c %= a is equivalent to c = c % a |
7 | ** =指数AND 对运算符执行指数(幂)计算并将值分配给左操作数 | c **= a is equivalent to c = c ** a |
8 | // =地板除法 它对运算符执行地板除法并将值赋给左操作数 | c // = a等效于c = c // a |
示例
假设变量a持有10,变量b持有20,则-
#!/usr/bin/pythona = 21
b = 10
c = 0
c = a + b
print "Line 1 - Value of c is ", c
c += a
print "Line 2 - Value of c is ", c
c *= a
print "Line 3 - Value of c is ", c
c /= a
print "Line 4 - Value of c is ", c
c = 2
c %= a
print "Line 5 - Value of c is ", c
c **= a
print "Line 6 - Value of c is ", c
c //= a
print "Line 7 - Value of c is ", c
输出结果
当您执行上述程序时,它将产生以下结果-
Line 1 - Value of c is 31Line 2 - Value of c is 52
Line 3 - Value of c is 1092
Line 4 - Value of c is 52
Line 5 - Value of c is 2
Line 6 - Value of c is 2097152
Line 7 - Value of c is 99864
以上是 Python赋值运算符 的全部内容, 来源链接: utcz.com/z/347411.html