Click for All Topics
Python uses assignment operators to assign values to variables. They enable you to store values in variables for later use or modification. An assignment operator’s fundamental syntax is as follows:
variable = value
Here, the value on the right side of the assignment operator is assigned to the variable on the left side. We have a wide variety of assignment operators; let’s look into some of them below:
The assignment operator is denoted by ‘=’ and is used to assign values. Here are some examples of assignment operators:
# Assigning a value to a variable
x = 5
y = 10
name = "edslash"
# Assigning multiple values to multiple variables
a, b, c = 1, 2, 3
A value can be added to a variable’s current value, and then the result is assigned back to the original variable using the addition assignment operator (+=). It offers a concise way of adding a new value to a variable in order to update its value. The fundamental syntax is:
variable += value
x = 5
x += 2 # Equivalent to: x = x + 2
print(x)
# Output of the above Python code:
# ---> Output: 7
# Addition assignment with strings
greeting = "Welcome to"
name = "edslash"
greeting += " " + name # Equivalent to: greeting = greeting + " " + name
print(greeting)
# Output of the above Python code:
# ---> Output: Welcome to edslash
When a value is subtracted from the current value of a variable, the result is then assigned back to the original variable using the subtraction assignment operator (-=). By deducting one value from another, it offers a convenient shorthand notation for changing a variable’s value. The syntax at its core is:
variable -= value
x = 10
x -= 3 # Equivalent to: x = x - 3
print(x)
# Output of the above Python code:
# Output: 7
Note: Remember that the subtraction assignment operator won’t work on lists in Python; it will give you an error as these are not supported for lists.
The multiplication assignment operator (*=) is used to multiply the current value of a variable by a given value and then assign the result back to the original variable. By multiplying a variable’s value by another, it offers a shortcut notation for changing the value of a variable. The fundamental syntax is as follows:
variable *= value
x = 3
x *= 4 # Equivalent to: x = x * 4
print(x)
# Output of the above Python code:
# Output: 12
# Multiplication assignment with strings
message = "edslash"
message *= 3 # Equivalent to: message = message * 3
print(message)
# Output of the above Python code:
# ---> Output: edslashedslashedslash
By dividing a variable’s current value by a given value, the division assignment operator (/=) returns the result to the original variable. It offers a convenient shorthand notation for dividing the value of a variable by another value. The fundamental syntax is:
variable /= value
x = 10
x /= 2 # Equivalent to: x = x / 2
print(x)
# Output of the above Python code:
# ---> Output: 5.0
Note:-
When a variable’s current value is divided by a given value, the remainder can be calculated and assigned back to the original variable using the modulus assignment operator (%=). For updating a variable’s value with the modulus operation, it offers a shorthand notation. The fundamental syntax is:
variable %= value
x = 10
x %= 3 # Equivalent to: x = x % 3
print(x)
# Output of the above Python code:
# ---> Output: 1
# Modulus assignment with floats
y = 8.5
y %= 2.5 # Equivalent to: y = y % 2.5
print(y)
# Output of the above Python code:
# ---> Output: 0.5
Tips and Tricks:- In situations where you must deal with remainders or cyclic patterns, such as determining whether a number is odd or even, managing repetitive sequences, or computing periodic events, the modulus assignment operator is especially helpful.
The exponentiation assignment operator (**=) in Python can be used to multiply a variable by a given value and then assign the result back to the original variable. By raising the value of a variable to power, it offers a convenient shorthand notation for doing so. The fundamental syntax is:
variable **= value
x = 2
x **= 3 # Equivalent to: x = x ** 3
print(x)
# Output of the above Python code:
# ---> Output: 8
# Exponentiation assignment with floats
y = 2.5
y **= 2 # Equivalent to: y = y ** 2
print(y)
# Output of the above Python code:
# Output: 6.25
Article written by Aakarsh Pandey, Team edSlash
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India