Assignment Operators in Python

In this section, we will discuss the assignment operators in the Python programming language. Before moving on to the topic, let's give a brief introduction to operators in Python. Operators are special symbols used in between operands to perform logical and mathematical operations in a programming language. The value on which the operator operates the computation is called the operand. There are different types of operators arithmetic, logical, relational, assignment, and bitwise, etc.

Assignment Operators in Python

Python has an assignment operator that helps to assign values or expressions to the left-hand-side variable. The assignment operator is represented as the "=" symbol used in assignment statements and assignment expressions. In the assignment operator, the right-hand side value or operand is assigned to the left-hand operand.

Following are the examples of the assignment operators:

Types of Assignment Operators

Following are the different types of assignment operators in Python:

  1. Simple assignment operator ( = )
  2. Add and equal operator ( += )
  3. Subtract and equal operator ( -= )
  4. Asterisk and equal operator ( *= )
  5. Divide and equal operator ( /= )
  6. Modulus and equal operator ( %= )
  7. Double divide and equal operator ( //= )
  8. Exponent assign operator ( **= )
  9. Bitwise And Operator ( &= )
  10. Bitwise OR Operator ( |= )
  11. Bitwise XOR Assignment Operator ( ^= )
  12. Bitwise right shift assignment operator ( >>= )
  13. Bitwise left shift assignment operator ( <<= )

Assignment Operator (=)

A simple assignment operator assigns the right side operand expression or value to the left side operand.

Syntax

Example:

Output:

Output = 5

Add and Assignment Operator (+=)

An operator adds the right side operand or value to the left operand before assigning the result to the left operand.

Syntax

Example:

Output:

Output = 9

Subtract and Assignment Operator (-=)

An operator subtracts the right side operand or value from the left operand and stores the value to the left operand.

Syntax

Example:

Output:

Output = 2

Multiply and Assignment Operator (*=)

An operator multiplies the right side operand or value to the left operand and stores the product to the left operand.

Syntax

Example:

Output:

Output = 60

Divide and Assignment Operator (/=)

An operator divides the left operand by the right operand before assigning the result to the left operand.

Syntax

Example:

Output:

Output = 20.0

Modulus and Assignment Operator (%=)

An operator divides the left operand by the right side operand or value and places the remainder to the left side operand.

Syntax

Example:

Output:

Output = 2

Floor division and Assignment Operator (//=)

A floor division operator divides the left operand by the right side operand or value and then assigns floor (value) to the left operand.

Syntax

Example:

Output:

Output = 21

Exponent and Assignment Operator (**=)

An exponent assign operator is used to get the exponent value using both operands and assign the result into the left operand.

Syntax

Example:

Output:

Output = 64

Bitwise And (&) and Assignment Operator (&=)

A Bitwise And (&) and assigns operator is used to operate on both (left and right) operands and assign results into the left operand.

Syntax

Example:

Output:

Output = 4

Bitwise OR and Assignment Operator (|=)

A Bitwise OR and Assignment operator is used to operate on both (left and right) operand and store results into the left operand.

Syntax

Example:

Output:

Output = 15

Bitwise XOR and Assignment Operator (^=)

A Bitwise XOR and Assignment operator operated on both (left and right) operand and assign the results into the left operand.

Syntax

Example:

Output:

Output = 11

Bitwise Right Shift and Assign Operator (>>=)

An operator shifts the specified amount of bits or operands to the right and assigns value to the left operand.

Syntax

Example:

Output:

Output = 1

Bitwise Left Shift and Assign operator (<<=)

An operator shifts the specified amount of operands to the left side and assigns the result to the left operand.

Syntax

Example:

Output:

Output = 24





Latest Courses