加成
a, b = 1, 2
# Using the "+" operator:
a + b # = 3
# Using the "in-place" "+=" operator to add and assign:
a += b # a = 3 (equivalent to a = a + b)
import operator # contains 2 argument arithmetic functions for the examples
operator.add(a, b) # = 5 since a is set to 3 right before this line
# The "+=" operator is equivalent to:
a = operator.iadd(a, b) # a = 5 since a is set to 3 right before this line
可能的组合(内置类型):
int
和int
(给一个int
)int
和float
(给一个float
)int
和complex
(给一个complex
)float
和float
(给一个float
)float
和complex
(给一个complex
)complex
和complex
(给一个complex
)
注意:+
运算符也用于连接字符串,列表和元组:
"first string " + "second string" # = 'first string second string'
[1, 2, 3] + [4, 5, 6] # = [1, 2, 3, 4, 5, 6]