Nearby lessons

38 of 108

Python - Assignment Operators

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
30

Explanation

a += 20

is equivalent to

a = a + 20
        

Another Example - Add and Assign

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
8

2. Subtract and Assign Operator (-=)

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

Example - Subtract and Assign

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
8

Explanation

2 ** 3 = 8
        

Assignment Operator with Strings

The += operator can concatenate strings.

Example - String Concatenation

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Hello Python

Assignment Operator with Lists

Assignment operators also work with lists.

Example - List Concatenation

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
[10, 20, 30, 40]

Multiple Assignment

Python allows assigning multiple values in a single statement.

Example - Multiple Assignment

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10
20
30

Same Value to Multiple Variables

Python also allows assigning the same value to multiple variables.

Example - Same Value Assignment

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
100
100
100

Swapping Variables

Python allows swapping two variables without using a temporary variable.

Example - Swapping Variables

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
20
10

Real World Usage

Assignment operators are commonly used in:

  • Counters
  • Calculations
  • Updating values
  • Loops
  • Game scores
  • Banking systems

Important Notes

  • Assignment operators store values in variables.
  • Compound assignment operators reduce code length.
  • Assignment operators work with numbers, strings, and lists.
  • Python supports multiple assignment.
  • Variables can be swapped without using a temporary variable.

Quick Summary

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

🧠 Test Your Knowledge

8 Questions

Progress: 0 / 8
Keep Going!Python - Bitwise Operators