Python加成
示例
a, b = 1, 2 # Using the "+" operator: a + b #=3 # Using the "in-place" "+=" operator to add and assign: a += b #a=3(相当于a=a+b) import operator #包含2个参数算术函数的示例 operator.add(a, b) #=5,因为在此行之前将a设置为3 # The "+=" operator is equivalent to: a = operator.iadd(a, b) #a=5,因为在此行之前将a设置为3
可能的组合(内置类型):
int和int(给出int)
int和float(给afloat)
int和complex(给acomplex)
float和float(给afloat)
float和complex(给acomplex)
complex和complex(给acomplex)
注意:该+运算符还用于连接字符串,列表和元组:
"第一个字符串 " + "second string" # = '第一个字符串 second string' [1, 2, 3] + [4, 5, 6] #=[1、2、3、4、5、6]