Home > Article > Backend Development > What does "+=" mean in python
In python, "=" refers to the "add assignment" operator, which is a type of assignment operator. Its function is to perform addition operation first, and then assign the result to the variable on the left side of the operator; syntax is "x = y", and the equivalent form is "x = x y". The "=" operator can only assign values to existing variables, because the variable itself needs to participate in the operation during the assignment process. If the variable is not defined in advance, its value is unknown and cannot participate in the operation.
The operating environment of this tutorial: windows7 system, python3 version, DELL G3 computer
In python, "=" means The "plus assignment" operator is a type of assignment operator.
=” operator can perform addition operation first, and then assign the result to the variable on the left side of the operator
Syntax:
x += y
This is equivalent to:
x = x + y
Example:
n1 = 100 f1 = 25.5 n1 += 80 #等价于 n1=n1+80 f1 += 0.5 #等价于 f1=f1+0.5 print("n1=%d" % n1) print("f1=%.2f" % f1)
##Extended knowledge: Python assignment operator
The assignment operator is used to transfer the value on the right side to the variable (or constant) on the left side; you can directly transfer the value on the right side to the variable on the left side, or you can perform some operations and then cross it. To the variables on the left, such as addition, subtraction, multiplication and division, function calls, logical operations, etc. The most basic assignment operator in Python is the equal sign=; combined with other operators,
= can also be extended to more powerful assignment operators.
The basic assignment operator
= is the most common in Python , the most basic assignment operator, used to assign the value of an expression to another variable, please see the following example:
#将字面量(直接量)赋值给变量 n1 = 100 f1 = 47.5 s1 = "http://c.biancheng.net/python/" #将一个变量的值赋给另一个变量 n2 = n1 f2 = f1 #将某些运算的值赋给变量 sum1 = 25 + 46 sum2 = n1 % 6 s2 = str(1234) #将数字转换成字符串 s3 = str(100) + "abc"
Expanded assignment operator
= can also be combined with other operators (including arithmetic operators, bitwise operators and logical operators) to expand into a more powerful assignment operator, as shown in Table 1. Extension The later assignment operators will make the writing of assignment expressions more elegant and convenient.
Instructions | Usage examples | Equivalent form | |
---|---|---|---|
The most basic assignment operation | x = y | x = y | |
Add assignment | x = y | x = x y | |
subtract assignment | x -= y | #x = x - y | |
Multiplication assignment | x *= y | x = x * y | |
Division assignment | x /= y | x = x / y | |
Get remainder assignment | x %= y | x = x % y | |
Power assignment | x **= y | x = x ** y | |
Take integer assignment | x //= y | x = x // y | |
bitwise AND assignment | x & = y | x = x & y | |
Bitwise OR assignment | x |= y | x = x | y | |
Bitwise XOR assignment | x ^= y | x = x ^ y | |
x x = x | |||
Right shift assignment | x >>= y | x = x >> y, where y refers to the number of digits to shift right |
n1 = 100 f1 = 25.5 n1 -= 80 #等价于 n1=n1-80 f1 *= n1 - 10 #等价于 f1=f1*( n1 - 10 ) print("n1=%d" % n1) print("f1=%.2f" % f1)Generally, as long as the extended assignment operator can be used, it is recommended to use this assignment operator. But please note that this assignment operator can only assign values to existing variables, because the variable itself needs to participate in the operation during the assignment process. If the variable is not defined in advance, its value is unknown and cannot participate in the operation. . For example, the following writing is wrong:
n += 10This expression is equivalent to n = n 10. n is not defined in advance, so it cannot participate in the addition operation. 【Related recommendations:
Python3 video tutorial】
The above is the detailed content of What does "+=" mean in python. For more information, please follow other related articles on the PHP Chinese website!