22.Python assignment operator (entry must-read)

Assignment operator is mainly used as a variable (or constant) assignment, in use, either directly with the basic assignment operator '=' the value assigned to the variable to the right of the left, right, some calculation may be performed and then assigned to the variable on the left side.

The basic assignment operator =

Python  "=" is an assignment operator, the value of the expression commonly used to another variable. For example the following code:

  1. # Is assigned to the variable st Python
  2. st = "Python"
  3. # Is assigned to the variable pi 3.14
  4. pi = 3.14
  5. # Assign a variable to True visited
  6. visited = True

In addition, the assignment operator can be used to facilitate the value of a variable to another variable. For example, the following code is correct:

  1. # The value assigned to the variable st st2
  2. st2 = st
  3. print(st2)

It is worth noting, Python's assignment expression is the value of the assignment expression is the value to be assigned a value, Python supports continuous assignment. For example, the following code is correct:

a = b = c = 20

The above procedure c = 20. The expression value to the variable b, since the assignment expression itself has value, is to be assigned values, and therefore the value C = 20 is the expression 20, it has also been assigned to b 20; and so on, the variable a It has also been assigned to 20.

Assignment operator can be used for the value of the expression to the variable. For example, the following code is correct:

  1. D1 = 12.34
  2. # The value of the expression is assigned to d2
  3. d2 = d1 + 5
  4. D2 is the output value of the #
  5. Print ( "D2 value:% G" % D2 ) # 17.34


Beginners need special attention, and = == completely different meanings, the former is an assignment, which is the equal sign, do not be confused.

The extended assignment operator

= Assignment operator also be combined with other operators (arithmetic operators, bitwise operators, etc.), the more powerful the assignment operator, as shown in Table 1.

Table 1 Python common assignment operator
Operators Explanation For example Expanded form
= The most basic assignment operator x = y x = y
+= Plus assignment x + = y x = x + y
-= Less assignment x - = y x = x - and
*= Take assignment x * = y x = x * y
/= In addition to the assignment x / = y x = x / y
%= Take the remainder assignment x% = y x = x% y
**= Power assignment x ** = y x = x ** and
//= Rounded assignment // x = y x = x // and
&= Bitwise AND assignment x &= y x = x & y
|= Bitwise OR assignment x | = y x = x | and
^= Bitwise XOR assignment x ^ = y x = x ^ y
<<= Left assignment x <<= y x = x << y, y herein refers to the number of bits left
>>= Right Assignment x >>= y x = x >> y, y is right here refers to the number of bits

Here are some simple examples:

  1. a = 1
  2. b = 2
  3. a += b
  4. print("a+b=",a)#1+2=3
  5. a -= b
  6. print("a-b=",a)#3-2=1
  7. a *= b
  8. print("a*b=",a)#1*2=2
  9. a /= b
  10. print("a/b=",a)#2/2=1.0
  11. a %= b
  12. print("a%b=",a)#1%2=1.0
  13. c = 0
  14. d = 2
  15. c &= d
  16. print("c&d=",c)#0&2=0
  17. c |= d
  18. print("c|d=",c)#0|2=2

Operating results as follows:

a+b= 3
a-b= 1
a*b= 2
a/b= 1.0
a%b= 1.0
c&d= 0
c|d= 2

It should be noted that the value of a program by / = and =% after the operation, the type of change has become an implicit float, and floating-point numbers can not be &, |, ^, << and >> operators, otherwise Python interpreter error (these are bitwise operators, specific usage will be detailed subsequent article).

Typically, the extension can be used as long as the assignment operator, it is recommended to use this assignment operator.

Guess you like

Origin www.cnblogs.com/youqc/p/12067854.html