Nearby lessons

33 of 159

Python - Assignment Operators

📌 What You Will Learn
  • 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

🐍Code Cell
1a = 10
2 
3print(a)
Output
10

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

🐍Code Cell
1a = 10
2 
3a += 20
4 
5print(a)
Output
30

Explanation

a += 20

is equivalent to

a = a + 20
        

Another Example - Add and Assign

🐍Code Cell
1x = 5
2 
3x += 3
4 
5print(x)
Output
8

2. Subtract and Assign Operator (-=)

The -= operator subtracts a value and stores the result in the same variable.

Example - Subtract and Assign

🐍Code Cell
1a = 50
2 
3a -= 20
4 
5print(a)
Output
30

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

🐍Code Cell
1a = 10
2 
3a *= 5
4 
5print(a)
Output
50

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

🐍Code Cell
1a = 20
2 
3a /= 2
4 
5print(a)
Output
10.0

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

🐍Code Cell
1a = 10
2 
3a %= 3
4 
5print(a)
Output
1

Explanation

10 % 3 = 1
        

6. Floor Divide and Assign Operator (//=)

The //= operator performs floor division and stores the result.

Example - Floor Divide and Assign

🐍Code Cell
1a = 10
2 
3a //= 3
4 
5print(a)
Output
3

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

🐍Code Cell
1a = 2
2 
3a **= 3
4 
5print(a)
Output
8

Explanation

2 ** 3 = 8
        

Assignment Operator with Strings

The += operator can concatenate strings.

Example - String Concatenation

🐍Code Cell
1s = "Hello"
2 
3s += " Python"
4 
5print(s)
Output
Hello Python

Assignment Operator with Lists

Assignment operators also work with lists.

Example - List Concatenation

🐍Code Cell
1l = [10, 20]
2 
3l += [30, 40]
4 
5print(l)
Output
[10, 20, 30, 40]

Multiple Assignment

Python allows assigning multiple values in a single statement.

Example - Multiple Assignment

🐍Code Cell
1a, b, c = 10, 20, 30
2 
3print(a)
4print(b)
5print(c)
Output
10
20
30

Same Value to Multiple Variables

Python also allows assigning the same value to multiple variables.

Example - Same Value Assignment

🐍Code Cell
1a = b = c = 100
2 
3print(a)
4print(b)
5print(c)
Output
100
100
100

Swapping Variables

Python allows swapping two variables without using a temporary variable.

Example - Swapping Variables

🐍Code Cell
1a = 10
2b = 20
3 
4a, b = b, a
5 
6print(a)
7print(b)
Output
20
10

Real World Usage

Assignment operators are commonly used in:

  • Counters
  • Calculations
  • Updating values
  • Loops
  • Game scores
  • Banking systems
📝 Key Takeaways
  • 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

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8