What does '+=' mean in python

青灯夜游
Release: 2023-01-05 17:53:49
Original
28188 people have browsed it

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.

What does '+=' mean in python

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
Copy after login

This is equivalent to:

x = x + y
Copy after login

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)
Copy after login

What does += mean in python

##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"
Copy after login

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.

Table 1 Python extended assignment operatorsOperatorInstructionsUsage examplesEquivalent form=The most basic assignment operationx = yx = y =Add assignment x = yx = x y-=subtract assignmentx -= y#x = x - y*=Multiplication assignmentx *= yx = x * y /=Division assignmentx /= yx = x / y%=Get remainder assignmentx %= yx = x % y**=Power assignmentx **= yx = x ** y//=Take integer assignmentx //= yx = x // y&=bitwise AND assignmentx & = yx = x & y|=Bitwise OR assignmentx |= yx = x | y^=Bitwise XOR assignmentx ^= yx = x ^ y<<=Left shift assignmentx <<= yx = x << y, where y refers to the number of digits to shift left>>=Right shift assignmentx >>= yx = x >> y, where y refers to the number of digits to shift right
here A simple example:

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)
Copy after login

What does += mean in python

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 += 10
Copy after login
This 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!