下表列出了所有python语言支持的赋值运算符。假设变量a持有10和变量b持有20,则:
例如:
试试下面的例子就明白了所有在python编程语言可供选择的赋值运算符:
#!/usr/bin/python
a = 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 31
line 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