Min menu

Pages

Operators that are used to assign values __to variables ( Assignment Operators ) in Python

 Factors that are used to give values ​​to variables (Assignment Operators) in Python 

the name of the employeehis codeExampleExplanation of the code
Basic Assignment=a = bPut a value  b in  a.
Add AND Assignment+=a += bAdd a value  a to a value  b and store the result in a
Susbtract AND Assignment-=a -= bSubtract a value  a from a value  b and store the result in a
Multiply AND Assignment*=a *= bMultiply a value  by its a value  b and store the result in a
Exponent AND Assignment**=a **= bMultiply a value  by a a value  b and store the result in a
Divide AND Assignment/=a /= bDivide a value  a by a value  b and store the result in a
Floor Divide AND Assignment//=a //= bDivide a value  a by a value  b and return the nearest integer
Modulo AND Assignment%=a %= bDivide a value  a by a value  b and store the last remaining number in the division operation a
Left shift AND Assignment<<=a <<= 2remove the last two bits And put them in the first and then store the output in a
Right shift AND Assignment>>=a >>= 2Slide the first two bits And put them in the other and then store the output in a
Bitwise AND Assignment&=a &= bCalculate the sum of bits common to  a and  b and store the result in a
Bitwise exclusive OR and Assignment^=a ^= bCalculate the sum of bits unshared between  a and  b and store the result in a
Bitwise inexclusive OR and Assignment|=a |= bCalculate 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

Here is a set of examples for each of the operators that are used to give values ​​to variables in Python.

working python  = (Basic Assign)

The operator  = is used to assign a value to a variable.


Example

Test.py
        a = 10
		
			print('a =', a)
      

We will get the following result when running.

a = 10


working python +=  (Add and Assign)

The operator  += is used to add a value to the value of a variable with less code.


Example

Test.py
        a = 10
			b = 20
		
			a += b # a = a + b
		
			print('a =', a)
      

We will get the following result when running.

a = 30


working python  -= (Susbtract and Assign)

The operator  -= is used to subtract a value from the value of a variable with a lower code.


Example

Test.py
        a = 10
			b = 20
		
			a -= b # a = a - b
		
			print('a =', a)
      

We will get the following result when running.

a = -10


working python  *= (Multiply and Assign)

The operator  *= is used to multiply the value of a variable by a value with a lower code.


Example

Test.py
        a = 10
			b = 20
		
			a *= b # a = a * b
		
			print('a =', a)
      

We will get the following result when running.

a = 200


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

Test.py
        a = 2
		
			a **= 5 # a = a ** 5
		
			print('a =', a)
      

We will get the following result when running.

a = 32


working python  /= (Divide and Assign)

The operator  /= is used to divide the value of a variable by a value with a lower code.

Example

Test.py
        a = 100
			b = 20
		
			a / = b # a = a / b
		
			print('a =', a)
      

We will get the following result when running.

a = 5


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

Test.py
        a = 8
			b = 5
		
			a //= b # a = a // b
		
			print('a =', a)
      

We will get the following result when running.

a = 2


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

Test.py
        a = 10
        b = 7
	
        a %= b # a = a % b
	
        print('a =', a)
  

We will get the following result when running.

a = 3


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

Test.py
        a = 10
	
        a <<= 2 # a = a < < 2 = 00000000000000000000000000001010 << 2 = 00000000000000000000000000101000 = 40
	
        print('a =', a)
  

We will get the following result when running.

a = 40


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

Test.py
        a = 10
	
        a >>= 2 # a = a >> 2 = 00000000000000000000000000001010 >> 2 = 00000000000000000000000000000010 = 2
	
        print('a =', a)
  

We will get the following result when running.

a = 2


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

Test.py
        a = 10
        b = 75
	
        a &= b# a = a & b = 00000000000000000000000000001010 & 00000000000000000000000001001011 = 00000000000000000000000000001010 = 10
	
        print('a =', a)
  

We will get the following result when running.

a = 10


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

Test.py
        a = 10
        b = 75
	
        a |= b # a = a | b = 0000000000000000000000000000001010 | 00000000000000000000000001001011 = 00000000000000000000000001001011 = 75
	
        print('a =', a)
  

We will get the following result when running.

a = 75