欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

整理Python中的赋值运算符

程序员文章站 2022-08-02 14:51:38
下表列出了所有python语言支持的赋值运算符。假设变量a持有10和变量b持有20,则:  例如: 试试下面的例子就明白了所有在python编程语言可...

下表列出了所有python语言支持的赋值运算符。假设变量a持有10和变量b持有20,则:

整理Python中的赋值运算符

 例如:

试试下面的例子就明白了所有在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