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

Loading editor…

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

Loading editor…

Explanation

a += 20

is equivalent to

a = a + 20
        

Another Example - Add and Assign

Loading editor…

2. Subtract and Assign Operator (-=)

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

Example - Subtract and Assign

Loading editor…

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

Loading editor…

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

Loading editor…

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

Loading editor…

Explanation

10 % 3 = 1
        

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

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

Example - Floor Divide and Assign

Loading editor…

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

Loading editor…

Explanation

2 ** 3 = 8
        

Assignment Operator with Strings

The += operator can concatenate strings.

Example - String Concatenation

Loading editor…

Assignment Operator with Lists

Assignment operators also work with lists.

Example - List Concatenation

Loading editor…

Multiple Assignment

Python allows assigning multiple values in a single statement.

Example - Multiple Assignment

Loading editor…

Same Value to Multiple Variables

Python also allows assigning the same value to multiple variables.

Example - Same Value Assignment

Loading editor…

Swapping Variables

Python allows swapping two variables without using a temporary variable.

Example - Swapping Variables

Loading editor…

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

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.