Assignment operators in python

Python Assignment Operators

Assignment operators are used to assign values to variables:

Operator Example Same As Try it
= x = 5 x = 5 Try it »
+= x += 3 x = x + 3 Try it »
-= x -= 3 x = x — 3 Try it »
*= x *= 3 x = x * 3 Try it »
/= x /= 3 x = x / 3 Try it »
%= x %= 3 x = x % 3 Try it »
//= x //= 3 x = x // 3 Try it »
**= x **= 3 x = x ** 3 Try it »
&= x &= 3 x = x & 3 Try it »
|= x |= 3 x = x | 3 Try it »
^= x ^= 3 x = x ^ 3 Try it »
>>= x >>= 3 x = x >> 3 Try it »
x x = x Try it »

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

What is Assignment Operator in Python?

Assignment Operators

Before deep-diving into the assignment operators in Python, first, understand what operators are. So operators are used in between the operands to perform various operations, like mathematical operations , bitwise operations , logical operations , and so on. Here, operands are the values on which operators perform the actions. So, assignment operators are used to assigning values to the operands on the left-hand side. Assignment operators assign the right-hand side values to the operand that is present on the left-hand side. The assignment operator in Python is used as the «=» symbol.

Let’s see a very basic example of the assignment operator.

Table Of Assignment Operators

Here we will see different assignment operators in Python with their names, descriptions, and syntax. Let’s take them one by one.

Operator Name Description Syntax
= Assignment Operator It assigns the right-hand side expression value to the operand present on the left-hand side. a=b+c
+= Addition Assignment Operator This operator adds left and right operands, and after that, it assigns the calculated value to the left-hand operand. a+=b or a=a+b
-= Subtraction Assignment Operator This operator subtracts the right operand from the left operand and assigns the result to the left operand. a-=b or a=a-b
*= Multiplication Assignment Operator This operator will multiply the left-hand side operand by the right-hand side operand and, after that, assign the result to the left-hand operand. a*=b or a=a*b
/= Division Assignment Operator This operator will divide the left-hand side operand by the right-hand side operand and, after that, assign the result to the left-hand operand. a/=b or a=a/b
%= Modulus Assignment Operator This operator will divide the left-hand side operand by the right-hand side operand and, after that, assign the reminder to the left-hand operand. a%=b or a=a%b
**= Exponentiation Assignment Operator This operator raises the left-side operand to the power of the right-side operand and assigns the result to the left-side value. a**=b or a=a**b
//= Floor Division Assignment Operator This operator will divide the left operand by the right operand and assign the quotient value (which would be in the form of an integer) to the left operand. a//=b or a=a//b
&= Bitwise AND Assignment Operator This operator will perform the bitwise operation on both the left and right operands and, after that, it will assign the resultant value to the left operand. a&=b or a=a&b
|= Bitwise OR Assignment Operator This operator will perform the bitwise or operation on both the left and right operands and, after that, it will assign the resultant value to the left operand. a|=b or a=a|b
^= Bitwise XOR Assignment Operator This operator will perform the bitwise XOR operation on the left and right operands and, after that, it will assign the resultant value to the left operand. a^=b or a=a^b
>>= Bitwise Right Shift Assignment Operator This operator will right-shift the left operand by the specified position, i.e., b, and after that, it will assign the resultant value to the left operand. a>>=b or a=a>>b
Bitwise Left Shift Assignment Operator This operator will left shift the left operand by the specified position, i.e., b, and after that, it will assign the resultant value to the left operand. a

Examples

Assignment Operator :

This is an assignment operator in Python which assigns the right-hand side expression value to the operand present on the left-hand side.

Sample Code :

Addition Assignment Operator :

This type of assignment operator in Python adds left and right operands, and after that, it assigns the calculated value to the left-hand operand.

Sample Code :

Subtraction Assignment Operator :

This operator subtracts the right operand from the left operand and assigns the result to the left operand.

Sample Code :

Multiplication Assignment Operator:

This operator will multiply the left-hand side operand by the right-hand side operand and, after that, assign the result to the left-hand operand.

Sample Code :

Division Assignment Operator :

This type of assignment operator in Python will divide the left-hand side operand from the right-hand side operand and, after that, assign the result to the left-hand operand.

Sample Code :

Modulus Assignment Operator :

This operator will divide the left-hand side operand to the right-hand side operand and, after that, assign the reminder to the left-hand operand.

Sample Code :

Click here, to learn more about modulus in python.

Exponentiation Assignment Operator :

This operator raises the left-side operand to the power of the right-side operand and assigns the result to the left-side value.

Sample Code :

Floor Division Assignment Operator :

This operator will divide the left operand by the right operand and assign the quotient value (which would be in the form of an integer) to the left operand.

Sample Code :

Bitwise AND Assignment Operator :

This operator will perform the bitwise operation on both the left and right operands and, after that, it will assign the resultant value to the left operand.

Sample Code :

Bitwise OR Assignment Operator:

This operator will perform the bitwise or operation on both the left and right operands and, after that, it will assign the resultant value to the left operand.

Sample Code :

Bitwise XOR Assignment Operator:

This operator will perform the bitwise XOR operation on the left and right operands and, after that, it will assign the resultant value to the left operand.

Sample Code :

Bitwise Right Shift Assignment Operator :

This operator will right shift the left operand by the specified position, i.e., b , and after that, it will assign the resultant value to the left operand.

Sample Code :

Bitwise Left Shift Assignment Operator:

This operator will left shift the left operand by the specified position, i.e., b , and after that, it will assign the resultant value to the left operand.

Sample Code :

Conclusion

  • Assignment operators in Python assign the right-hand side values to the operand that is present on the left-hand side.
  • The assignment operators in Python is used as the «=» symbol.
  • We have different types of assignment operators like +=, -=, \*=, %=, /=, //=, \*\*=, &=, |=, ^=, >>=,
  • All the operators have the same precedence, and hence we prioritise operations based on their associativiy . The associativity is from right to left.

Источник

Читайте также:  Java programming code help
Оцените статью