coding180 icon
Coding180
Assignment Operators: Equals, Add equals, Subtract equals, Multiply equals, Divide equals, Modulus e

Assignment Operators: Equals, Add equals, Subtract equals, Multiply equals, Divide equals, Modulus e

Home > Tutorials > Python for beginners > Operators


If you're new to programming, you might be wondering what "assignment operators" are. Simply put, they're symbols used in programming languages to assign values to variables. In this article, we'll take a closer look at some common assignment operators and provide examples and exercises to help you understand how they work.

Equals Operator (=)

The equals operator is the most basic assignment operator. It assigns the value on the right-hand side of the operator to the variable on the left-hand side.

x = 5

In this example, x is assigned the value of 5.

Add Equals Operator (+=)

The add equals operator adds the value on the right-hand side of the operator to the variable on the left-hand side, and then assigns the result to the variable on the left-hand side.

x += 3

In this example, 3 is added to the value of x, which was previously 5, resulting in 8. The new value, 8, is then assigned to x.

Subtract Equals Operator (-=)

The subtract equals operator subtracts the value on the right-hand side of the operator from the variable on the left-hand side, and then assigns the result to the variable on the left-hand side.

x -= 2

In this example, 2 is subtracted from the value of x, which was previously 8, resulting in 6. The new value, 6, is then assigned to x.

Multiply Equals Operator (*=)

The multiply equals operator multiplies the value on the right-hand side of the operator with the variable on the left-hand side, and then assigns the result to the variable on the left-hand side.

x *= 2

In this example, 2 is multiplied with the value of x, which was previously 6, resulting in 12. The new value, 12, is then assigned to x.

Divide Equals Operator (/=)

The divide equals operator divides the variable on the left-hand side by the value on the right-hand side of the operator, and then assigns the result to the variable on the left-hand side.

x /= 3

In this example, x is divided by 3, resulting in 4.0. The new value, 4.0, is then assigned to x.

Modulus Equals Operator (%=)

The modulus equals operator divides the variable on the left-hand side by the value on the right-hand side of the operator, and then assigns the remainder to the variable on the left-hand side.

x %= 3

In this example, x is divided by 3, resulting in a remainder of 1. The new value, 1, is then assigned to x.

Exponentiate Equals Operator (**=)

The exponentiate equals operator raises the variable on the left-hand side to the power of the value on the right-hand side of the operator, and then assigns the result to the variable on the left-hand side.

x **= 2

In this example, x is raised to the power of 2, resulting in 1. The new value, 1, is then assigned to x.

Exercises

Now that you've seen some examples of assignment operators, try these exercises to test your understanding:

  1. Assign the value 10 to a variable named a
  2. Add 5 to the variable a using the add equals operator
  3. Subtract 2 from the variable a using the subtract equals operator
  4. Multiply the variable a by 3 using the multiply equals operator
  5. Divide the variable a by 2 using the divide equals operator
  6. Assign the value 7 to a variable named b
  7. Find the remainder when b is divided by 3 using the modulus equals operator
  8. Raise b to the power of 2 using the exponentiate equals operator

Conclusion

Assignment operators are an essential part of programming languages and allow you to assign values to variables quickly and easily. By understanding how they work, you can write more efficient and concise code. With practice, you'll become familiar with these operators and be able to use them effortlessly in your own programs.


user

Robort Gabriel

Lagos, Nigeria

Freelance Web Developer, Native Android Developer, and Coding Tutor.

Skills
  • UI / UX
  • JavaScript
  • Python
  • PHP
  • Kotlin
  • Java
  • Bootrap
  • Android
  • Laravel