Nearby lessons
33 of 159Python - Assignment Operators
- Define assignment operators and their purpose
- Use the basic = operator to assign values
- Use compound operators like +=, -=, *=, and /=
- Use the %, //=, and **= compound operators
- Use assignment operators with strings and lists
- Swap variables without a temporary variable
Introduction
Assignment operators are used to assign values to variables.
They can also update the value of a variable by combining an operation with assignment.
Basic Assignment Operator (=)
The basic assignment operator is =.
It assigns a value to a variable.
Example - Basic Assignment
Explanation
The = operator assigns the value 10 to the variable a.
Types of Assignment Operators
| Operator | Meaning |
|---|---|
= | Assign |
+= | Add and Assign |
-= | Subtract and Assign |
*= | Multiply and Assign |
/= | Divide and Assign |
%= | Modulus and Assign |
//= | Floor Divide and Assign |
**= | Exponent and Assign |
1. Add and Assign Operator (+=)
The += operator adds a value and stores the result in the same variable.
Example - Add and Assign
Explanation
a += 20
is equivalent to
a = a + 20
Another Example - Add and Assign
2. Subtract and Assign Operator (-=)
The -= operator subtracts a value and stores the result in the same variable.
Example - Subtract and Assign
Explanation
a -= 20
is equivalent to
a = a - 20
3. Multiply and Assign Operator (*=)
The *= operator multiplies a value and stores the result in the same variable.
Example - Multiply and Assign
Explanation
a *= 5
is equivalent to
a = a * 5
4. Divide and Assign Operator (/=)
The /= operator divides a value and stores the result in the same variable.
Example - Divide and Assign
Important Note
The division assignment operator always returns a float value.
5. Modulus and Assign Operator (%=)
The %= operator calculates the remainder and stores the result.
Example - Modulus and Assign
Explanation
10 % 3 = 1
6. Floor Divide and Assign Operator (//=)
The //= operator performs floor division and stores the result.
Example - Floor Divide and Assign
Explanation
10 // 3 = 3
7. Exponent and Assign Operator (**=)
The **= operator calculates the power of a number and stores the result.
Example - Exponent and Assign
Explanation
2 ** 3 = 8
Assignment Operator with Strings
The += operator can concatenate strings.
Example - String Concatenation
Assignment Operator with Lists
Assignment operators also work with lists.
Example - List Concatenation
Multiple Assignment
Python allows assigning multiple values in a single statement.
Example - Multiple Assignment
Same Value to Multiple Variables
Python also allows assigning the same value to multiple variables.
Example - Same Value Assignment
Swapping Variables
Python allows swapping two variables without using a temporary variable.
Example - Swapping Variables
Real World Usage
Assignment operators are commonly used in:
- Counters
- Calculations
- Updating values
- Loops
- Game scores
- Banking systems
- Assignment operators assign or update variable values
- a += 20 is equivalent to a = a + 20
- The /= operator always produces a float value
- The += operator can concatenate strings and lists
- Python supports multiple assignment and same-value assignment
- a, b = b, a swaps two variables without a temporary variable