introductory
In Python, but whenever the assignment operator is mentioned, it is actually about the equal sign =, the meaning of the equal sign in the programming language is no longer 1+1=2 in math, the real meaning is to assign the result on the right side of = to the variable on the left side of the equal sign.
As if the definition of a variable num = 1, the first calculation of the right side of the equals sign to the results of this calculation and then assigned to the left side of the equals sign of the variable, in fact, at this time the num variable is 1 of this data in the memory of a reference address, and later want to use the 1 of this data directly out of the num variable can be read num variable corresponding to the access to the data is also 1.
I. Meaning of assignment operator:
operator (computing) | descriptive | an actual example |
---|---|---|
= | assign a value to something | Assign the result to the right of = to the variable to the left of the equal sign |
II. assignment operator writing:
2.1 Individual variable assignment
num= 1 print(num)
2.2 Multiple variable assignment
Note: When assigning multiple variables, the number of variables on the left side of the equal sign should be the same as the number of data on the right side of the equal sign, the data should be separated by English commas, and the order of the variables and the order of the data should correspond to each other.
Multiple variable assignment parsing process: assign 3 to num1, then 0.8 to float1, and finally Python self-study.com to str1
num1,float1,str1 = 3,0.8,'Python Self-Learning Network' print(num1) print(float1) print(str1)
The return result is as follows:
2.3 Multiple variables assigned the same value
Assignment process: 100 is assigned to variable a and also to variable b.
a = b = 100 print(a) print(b)
The return result is as follows:
P.S. Extended assignment operator
= The assignment operator can also be combined with other operators (arithmetic operators, bitwise operators, etc.) to make more powerful assignment operators, as shown in Table 1.
Table 1 Common Python Assignment Operators
operator (computing) | Explanation | example | Forms of Expansion |
= | The most basic assignment operation | x = y | x = y |
+= | add value to | x += y | x = x + y |
-= | debit assignment | x -= y | x = x - y |
*= | multiply and assign | x *= y | x = x * y |
/= | division of a function by assignment (math.) | x /= y | x = x / y |
%= | take a remainder and assign it to a value | x %= y | x = x % y |
**= | power assignment (math.) | x **= y | x = x ** y |
//= | take an integer and assign it a value | x //= y | x = x // y |
&= | Bitwise and Assignment | x &= y | x = x & y |
|= | Bitwise or Assignment | x |= y | x = x | y |
^= | bitwisewise differentiation or assignment | x ^= y | x = x ^ y |
<<= | left shift assignment | x <<= y | x = x << y, where y refers to the number of bits shifted to the left |
>>= | right shift assignment (math.) | x >>= y | x = x >> y, where y refers to the number of bits shifted to the right |
Here are a few simple examples:
a = 1 b = 2 a += b print("a+b=",a)#1+2=3 a -= b print("a-b=",a)#3-2=1 a *= b print("a*b=",a)#1*2=2 a /= b print("a/b=",a)#2/2=1.0 a %= b print("a%b=",a)#1%2=1.0 c = 0 d = 2 c &= d print("c&d=",c)#0&2=0 c |= d print("c|d=",c)#0|2=2
The results of the run are:
a+b= 3
a-b= 1
a*b= 2
a/b= 1.0
a%b= 1.0
c&d= 0
c|d= 2
Note that the value of a in the program is implicitly changed to a floating-point number by the operations /= and %=, and floating-point numbers can't be operated on with &, |, ^, < < and > > or the Python interpreter will report an error!
summarize
This article on the meaning and use of Python assignment operator is introduced to this article, more related to the use of Python assignment operator content, please search for my previous posts or continue to browse the following related articles I hope that you will support me in the future more!