Python Tutorial

What is Python Assignment Operators? Full List With Types and Examples

What are Assignment Operators in Python?

The assignment operators are utilized to assign values to variables.

What are Assignment Operators in Python

Assignment Operators Python

Assignment Operators Python

Assignment Operators in Python

Assignment Operators in Python

Let's take a look at each Python Assignment Operator individually now.

  • Assign

The value of the right side of the expression is assigned to the left side operand using this operator.

Syntax

x = y + z

Example

a = 13

b = 15

c = a + b

print(c)

Output

28
  • Add and Assign

The right side operand is added to the left side operand, and the result is assigned to the left operand.

Syntax

x += y

Example

a = 3

b = 5

# a = a + b

a += b

print(a)

Output

8
  • Subtract and Assign

The result is assigned to the left operand after the right side operand is added to the left operand.

Syntax

x -= y

Example

a = 3

b = 5

# a = a - b

a -= b

print(a)

Output

-2
  • Multiply and Assign

This operator multiplies the right operand by the left operand and assigns the result to the left operand.

Syntax

x *= y

Example

a = 3

b = 5

# a = a * b

a *= b

print(a)

Output

15
  • Divide and Assign

The right operand is multiplied by the left operand, and the result is assigned to the left operand.

Syntax

x /= y

Example

a = 3

b = 5

# a = a / b

a /= b

print(a)

Output

0.6
  • Modulus and Assign

Using the left and right operands, this operator computes the modulus and assigns the result to the left operand.

Syntax

x %= y

Example

a = 3

b = 5

# a = a % b

a %= b

print(a)

Output

3
  • Divide (floor) and Assign

The left operand is divided by the right operand, and the result (floor) is assigned to the left operand.

Syntax

x //= y

Example

a = 3

b = 5 

# a = a // b

a //= b

print(a)

Output

0
  • Exponent and Assign

This operator calculates the exponent(raise power) value using operands and then assigns the result to the left operand.

Syntax

x **= y

Example

a = 3

b = 5

# a = a ** b

a **= b 

print(a)

Output

243
  • Bitwise AND and Assign

This operator applies Bitwise AND to both operands before assigning the result to the left operand.

Syntax

x &= y

Example

a = 3

b = 5

# a = a & b

a &= b

print(a)

Output:

1
  • Bitwise OR and Assign

This operator performs a Bitwise OR on the operands before assigning the results to the left operand.

Syntax

x |= y

Example

a = 3

b = 5

# a = a | b

a |= b

print(a)

Output:

7
  • Bitwise XOR and Assign

This operator applies Bitwise XOR to the operands before assigning the results to the left operand.

Syntax

x ^= y

Example

a = 3

b = 5 

# a = a ^ b

a ^= b

print(a)

Output:

6
  • Bitwise Right Shift and Assign

Before assigning the result to the left operand, this operator performs a Bitwise right shift on the operands.

Syntax

x >>= y

Example

a = 3

b = 5

# a = a >> b

a >>= b

print(a)

Output:

0
  • Bitwise Left Shift and Assign

This operator does a Bitwise left shift on the operands before assigning the results to the left operand.

Syntax

x <<= y

Example

a = 3

b = 5 

# a = a << b

a <<= b

print(a)

Output:

96

Did you find this article helpful?