0% found this document useful (0 votes)
14 views4 pages

Assignment Operators in Python_ Detailed Guide

The document provides a detailed guide on assignment operators in Python, explaining their functions and providing examples for each operator. It covers basic assignment, as well as combined operations like addition, subtraction, multiplication, and various bitwise operations. The guide emphasizes how these operators simplify code by reducing redundancy in variable assignments.

Uploaded by

imghost2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Assignment Operators in Python_ Detailed Guide

The document provides a detailed guide on assignment operators in Python, explaining their functions and providing examples for each operator. It covers basic assignment, as well as combined operations like addition, subtraction, multiplication, and various bitwise operations. The guide emphasizes how these operators simplify code by reducing redundancy in variable assignments.

Uploaded by

imghost2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment Operators in Python: Detailed Guide

Assignment operators in Python are used to assign values to variables. They also allow you to
combine assignment with other operations such as addition, subtraction, multiplication, etc.
Here’s a detailed look at each assignment operator with examples:

1. Basic Assignment (=)


Assigns the value on the right to the variable on the left.

x = 5 # x is now 5

2. Add AND Assign (+=)


Adds the right operand to the left operand and assigns the result to the left operand.

x = 5
x += 3 # equivalent to: x = x + 3; now x is 8

3. Subtract AND Assign (-=)


Subtracts the right operand from the left operand and assigns the result to the left operand.

x = 5
x -= 2 # equivalent to: x = x - 2; now x is 3

4. Multiply AND Assign (*=)


Multiplies the left operand by the right operand and assigns the result to the left operand.

x = 5
x *= 4 # equivalent to: x = x * 4; now x is 20

5. Divide AND Assign (/=)


Divides the left operand by the right operand and assigns the result to the left operand.

x = 20
x /= 5 # equivalent to: x = x / 5; now x is 4.0
6. Modulus AND Assign (%=)
Takes modulus using two operands and assigns the result to the left operand.

x = 10
x %= 3 # equivalent to: x = x % 3; now x is 1

7. Exponent AND Assign (**=)


Performs exponentiation and assigns the value back to the variable.

x = 2
x **= 3 # equivalent to: x = x ** 3; now x is 8

8. Floor Divide AND Assign (//=)


Performs floor division and assigns the value back to the variable.

x = 10
x //= 3 # equivalent to: x = x // 3; now x is 3

9. Bitwise AND Assign (&=)


Performs bitwise AND and assigns the value to the variable.

x = 6 # binary: 110
x &= 3 # x = x & 3 -> 110 & 011 = 010 (2 in decimal)

10. Bitwise OR Assign (|=)


Performs bitwise OR and assigns the value to the variable.

x = 4 # binary: 100
x |= 3 # x = x | 3 -> 100 | 011 = 111 (7 in decimal)

11. Bitwise XOR Assign (^=)


Performs bitwise XOR and assigns the value to the variable.

x = 8 # binary: 1000
x ^= 3 # x = x ^ 3 -> 1000 ^ 0011 = 1011 (11 in decimal)
12. Bitwise Right Shift Assign (>>=)
Performs right shift and assigns new value.

x = 4 # binary: 100
x >>= 2 # x = x >> 2 -> 100 >> 2 = 1

13. Bitwise Left Shift Assign (<<=)


Performs left shift and assigns new value.

x = 3 # binary: 11
x <<= 2 # x = x << 2 -> 11 << 2 = 1100 (12 in decimal)

Summary Table
Equivalent
Operator Example Description
Expression

= x=5 x=5 Simple assignment

+= x += 3 x=x+3 Addition + assignment

Subtraction +
-= x -= 2 x=x-2
assignment

Multiplication +
*= x *= 4 x=x*4
assignment

/= x /= 5 x=x/5 Division + assignment

%= x %= 3 x=x%3 Modulo + assignment

Exponentiation +
**= x **= 3 x = x ** 3
assignment

Floor division +
//= x //= 3 x = x // 3
assignment

Bitwise AND +
&= x &= 3 x=x&3
assignment

x=x| Bitwise OR +
= x =3
3 assignment

Bitwise XOR +
^= x ^= 3 x=x^3
assignment

>>= x >>= 2 x = x >> 2 Right shift + assignment

<<= x <<= 2 x = x << 2 Left shift + assignment

Assignment operators simplify your code by reducing the redundancy of writing the same
variable on both sides of the expression and allow for powerful, concise updates to variable
values.

You might also like