Assignment Operators in Python - GeeksforGeeks (2024)

Operators are used to perform operations on values and variables. These are the special symbols that carry out arithmetic, logical, bitwise computations. The value the operator operates on is known as Operand.

Here, we will cover Assignment Operators in Python. So, Assignment Operators are used to assigning values to variables.

Operator

Description

Syntax

=

Assign value of right side of expression to left side operandx = y + z

+=

Add and Assign: Add right side operand with left side operand and then assign to left operanda += b

-=

Subtract AND: Subtract right operand from left operand and then assign to left operand: True if both operands are equala -= b

*=

Multiply AND: Multiply right operand with left operand and then assign to left operanda *= b

/=

Divide AND: Divide left operand with right operand and then assign to left operanda /= b

%=

Modulus AND: Takes modulus using left and right operands and assign result to left operanda %= b

//=

Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to left operanda //= b

**=

Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operanda **= b

&=

Performs Bitwise AND on operands and assign value to left operanda &= b

|=

Performs Bitwise OR on operands and assign value to left operanda |= b

^=

Performs Bitwise xOR on operands and assign value to left operanda ^= b

>>=

Performs Bitwise right shift on operands and assign value to left operanda >>= b

<<=

Performs Bitwise left shift on operands and assign value to left operanda <<= b

Now Let’s see each Assignment Operator one by one.

1) Assign: This operator is used to assign the value of the right side of the expression to the left side operand.

Syntax:

x = y + z

Example:

Python3

# Assigning values using

# Assignment Operator

a = 3

b = 5

c = a + b

# Output

print(c)

Output:

8

2) Add and Assign: This operator is used to add the right side operand with the left side operand and then assigning the result to the left operand.

Syntax:

x += y

Example:

Python3

a = 3

b = 5

# a = a + b

a += b

# Output

print(a)

Output:

8

3) Subtract and Assign: This operator is used to subtract the right operand from the left operand and then assigning the result to the left operand.

Syntax:

x -= y

Example –

Python3

a = 3

b = 5

# a = a - b

a -= b

# Output

print(a)

Output:

-2

4) Multiply and Assign: This operator is used to multiply the right operand with the left operand and then assigning the result to the left operand.

Syntax:

x *= y

Example:

Output:

15

5) Divide and Assign: This operator is used to divide the left operand with the right operand and then assigning the result to the left operand.

Syntax:

x /= y

Example:

Python3

a = 3

b = 5

# a = a / b

a /= b

# Output

print(a)

Output:

0.6

6) Modulus and Assign: This operator is used to take the modulus using the left and the right operands and then assigning the result to the left operand.

Syntax:

x %= y

Example:

Python3

a = 3

b = 5

# a = a % b

a %= b

# Output

print(a)

Output:

3

7) Divide (floor) and Assign: This operator is used to divide the left operand with the right operand and then assigning the result(floor) to the left operand.

Syntax:

x //= y

Example:

Python

a = 3

b = 5

# a = a // b

a //= b

# Output

print(a)

Output:

0

8) Exponent and Assign: This operator is used to calculate the exponent(raise power) value using operands and then assigning the result to the left operand.

Syntax:

x **= y

Example:

Python

a = 3

b = 5

# a = a ** b

a **= b

# Output

print(a)

Output:

243

9) Bitwise AND and Assign: This operator is used to perform Bitwise AND on both operands and then assigning the result to the left operand.

Syntax:

x &= y

Example:

Python3

a = 3

b = 5

# a = a & b

a &= b

# Output

print(a)

Output:

1

10) Bitwise OR and Assign: This operator is used to perform Bitwise OR on the operands and then assigning result to the left operand.

Syntax:

x |= y

Example:

Python3

a = 3

b = 5

# a = a | b

a |= b

# Output

print(a)

Output:

7

11) Bitwise XOR and Assign:This operator is used to perform Bitwise XOR on the operands and then assigning result to the left operand.

Syntax:

x ^= y

Example:

Python3

a = 3

b = 5

# a = a ^ b

a ^= b

# Output

print(a)

Output:

6

12) Bitwise Right Shift and Assign: This operator is used to perform Bitwise right shift on the operands and then assigning result to the left operand.

Syntax:

x >>= y

Example:

Python3

a = 3

b = 5

# a = a >> b

a >>= b

# Output

print(a)

Output:

0

13) Bitwise Left Shift and Assign:This operator is used to perform Bitwise left shift on the operands and then assigning result to the left operand.

Syntax:

x <<= y

Example:

Python3

a = 3

b = 5

# a = a << b

a <<= b

# Output

print(a)

Output:

96

Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.


Last Updated : 29 Aug, 2020

Like Article

Save Article

Previous

A += B Assignment Riddle in Python

Next

Relational Operators in Python

I'm an enthusiast well-versed in Python programming, particularly in the realm of operators and their applications. My knowledge extends to practical usage and implementation of various operators, as evidenced by my understanding of assignment operators in Python.

In the provided article, the focus is on Assignment Operators in Python, which play a crucial role in assigning values to variables. Let's delve into each concept covered in the article:

1. Assignment Operator =

  • Description: Assigns the value of the right side of the expression to the left side operand.
  • Syntax: x = y + z
  • Example:
     a = 3
     b = 5
     c = a + b
     print(c)  # Output: 8

2. Add and Assign Operator +=

  • Description: Adds the right side operand with the left side operand and assigns the result to the left operand.
  • Syntax: x += y
  • Example:
     a = 3
     b = 5
     a += b
     print(a)  # Output: 8

3. Subtract and Assign Operator -=

  • Description: Subtracts the right operand from the left operand and assigns the result to the left operand.
  • Syntax: x -= y
  • Example:
     a = 3
     b = 5
     a -= b
     print(a)  # Output: -2

4. Multiply and Assign Operator *=

  • Description: Multiplies the right operand with the left operand and assigns the result to the left operand.
  • Syntax: x *= y
  • Example:
     a = 3
     b = 5
     a *= b
     print(a)  # Output: 15

5. Divide and Assign Operator /=

  • Description: Divides the left operand by the right operand and assigns the result to the left operand.
  • Syntax: x /= y
  • Example:
     a = 3
     b = 5
     a /= b
     print(a)  # Output: 0.6

6. Modulus and Assign Operator %=

  • Description: Takes the modulus using left and right operands and assigns the result to the left operand.
  • Syntax: x %= y
  • Example:
     a = 3
     b = 5
     a %= b
     print(a)  # Output: 3

7. Divide (floor) and Assign Operator //=

  • Description: Divides the left operand by the right operand and assigns the floor value to the left operand.
  • Syntax: x //= y
  • Example:
     a = 3
     b = 5
     a //= b
     print(a)  # Output: 0

8. Exponent and Assign Operator **=

  • Description: Calculates the exponent (raise power) value using operands and assigns the result to the left operand.
  • Syntax: x **= y
  • Example:
     a = 3
     b = 5
     a **= b
     print(a)  # Output: 243

9. Bitwise AND and Assign Operator &=

  • Description: Performs Bitwise AND on operands and assigns the result to the left operand.
  • Syntax: x &= y
  • Example:
     a = 3
     b = 5
     a &= b
     print(a)  # Output: 1

10. Bitwise OR and Assign Operator |=

  • Description: Performs Bitwise OR on operands and assigns the result to the left operand.
  • Syntax: x |= y
  • Example:
     a = 3
     b = 5
     a |= b
     print(a)  # Output: 7

11. Bitwise XOR and Assign Operator ^=

  • Description: Performs Bitwise XOR on operands and assigns the result to the left operand.
  • Syntax: x ^= y
  • Example:
     a = 3
     b = 5
     a ^= b
     print(a)  # Output: 6

12. Bitwise Right Shift and Assign Operator >>=

  • Description: Performs Bitwise right shift on operands and assigns the result to the left operand.
  • Syntax: x >>= y
  • Example:
     a = 3
     b = 5
     a >>= b
     print(a)  # Output: 0

13. Bitwise Left Shift and Assign Operator <<=

  • Description: Performs Bitwise left shift on operands and assigns the result to the left operand.
  • Syntax: x <<= y
  • Example:
     a = 3
     b = 5
     a <<= b
     print(a)  # Output: 96

By understanding and applying these assignment operators, one can manipulate variables efficiently in Python, contributing to the effective use of the language in various programming scenarios.

Assignment Operators in Python - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 6736

Rating: 5 / 5 (50 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.