Factors that are used to give values to variables (Assignment Operators) in Python
| the name of the employee | his code | Example | Explanation of the code |
|---|---|---|---|
| Basic Assignment | = | a = b | Put a value b in a. |
| Add AND Assignment | += | a += b | Add a value a to a value b and store the result in a |
| Susbtract AND Assignment | -= | a -= b | Subtract a value a from a value b and store the result in a |
| Multiply AND Assignment | *= | a *= b | Multiply a value by its a value b and store the result in a |
| Exponent AND Assignment | **= | a **= b | Multiply a value by a a value b and store the result in a |
| Divide AND Assignment | /= | a /= b | Divide a value a by a value b and store the result in a |
| Floor Divide AND Assignment | //= | a //= b | Divide a value a by a value b and return the nearest integer |
| Modulo AND Assignment | %= | a %= b | Divide a value a by a value b and store the last remaining number in the division operation a |
| Left shift AND Assignment | <<= | a <<= 2 | remove the last two bits And put them in the first and then store the output in a |
| Right shift AND Assignment | >>= | a >>= 2 | Slide the first two bits And put them in the other and then store the output in a |
| Bitwise AND Assignment | &= | a &= b | Calculate the sum of bits common to a and b and store the result in a |
| Bitwise exclusive OR and Assignment | ^= | a ^= b | Calculate the sum of bits unshared between a and b and store the result in a |
| Bitwise inexclusive OR and Assignment | |= | a |= b | Calculate the sum of bits Shared and unshared between a and b and store the output in a |
Here we put an example for each factor in the table
Examples of operators used to give values to variables in Python
working python = (Basic Assign)
The operator = is used to assign a value to a variable.
Example
a = 10
print('a =', a)
• We will get the following result when running.
working python += (Add and Assign)
The operator += is used to add a value to the value of a variable with less code.
Example
a = 10
b = 20
a += b # a = a + b
print('a =', a)
• We will get the following result when running.
working python -= (Susbtract and Assign)
The operator -= is used to subtract a value from the value of a variable with a lower code.
Example
a = 10
b = 20
a -= b # a = a - b
print('a =', a)
• We will get the following result when running.
working python *= (Multiply and Assign)
The operator *= is used to multiply the value of a variable by a value with a lower code.
Example
a = 10
b = 20
a *= b # a = a * b
print('a =', a)
• We will get the following result when running.
working python ** (Exponent and Assign)
The operator ** is used to multiply a value by a value, that is, to multiply the value of a variable by itself several times with less code.
Example
a = 2
a **= 5 # a = a ** 5
print('a =', a)
• We will get the following result when running.
working python /= (Divide and Assign)
The operator /= is used to divide the value of a variable by a value with a lower code.
Example
a = 100
b = 20
a / = b # a = a / b
print('a =', a)
• We will get the following result when running.
working python //= (Floor Divide and Assign)
The operator //= is used to divide the value of a variable by a value with the result rounded to the nearest integer so that no number appears after the comma with less code.
Example
a = 8
b = 5
a //= b # a = a // b
print('a =', a)
• We will get the following result when running.
working python %= (Modulo and Assign)
The operator %= is used to store the remainder of a variable's value divided by a value with less code.
Example
a = 10
b = 7
a %= b # a = a % b
print('a =', a)
• We will get the following result when running.
worker <<= (Left shift and Assign)
In the following example, operator <<= means delete bits from the left side, then switch each bit It was erased from them with a zero and put them on the right and then put the result in the variable again.
Example
a = 10
a <<= 2 # a = a < < 2 = 00000000000000000000000000001010 << 2 = 00000000000000000000000000101000 = 40
print('a =', a)
• We will get the following result when running.
working python >>= (Right shift and Assign)
In the following example, operator >>= means delete bits on the right, then switch each bit It was erased from them with zero and put them on the left side, then put the result in the variable again.
Example
a = 10
a >>= 2 # a = a >> 2 = 00000000000000000000000000001010 >> 2 = 00000000000000000000000000000010 = 2
print('a =', a)
• We will get the following result when running.
working python &= (Bitwise and Assign)
The factor &= means Calculate the product of the sum of bits shared between the variable a and the variable and b then store the result in the variable a.
Example
a = 10
b = 75
a &= b# a = a & b = 00000000000000000000000000001010 & 00000000000000000000000001001011 = 00000000000000000000000000001010 = 10
print('a =', a)
• We will get the following result when running.
working python |= (Bitwise exclusive OR and Assign)
The factor |= means Calculate the product of the sum of bits Shared and unshared between the variable a and the variable and b then store the result in the variable a.
Example
a = 10
b = 75
a |= b # a = a | b = 0000000000000000000000000000001010 | 00000000000000000000000001001011 = 00000000000000000000000001001011 = 75
print('a =', a)
• We will get the following result when running.