Lesson 9 of 4023%

(Progress persistence is disabled until Phase 9)

Python
Beginner

Operators

Perform mathematical and logical operations.

Operators

What You’ll Learn

Operators are used to perform operations on variables and values. You’ll learn the most common mathematical and assignment operators.

Arithmetic Operators

Used with numeric values to perform common mathematical operations:

x = 10
y = 3

print(x + y)  # Addition: 13
print(x - y)  # Subtraction: 7
print(x * y)  # Multiplication: 30
print(x / y)  # Division: 3.333... (always returns a float)

Special Arithmetic Operators

Python has some unique math operators:

# Floor Division (//) - divides and rounds down to nearest whole number
print(10 // 3)  # 3

# Modulus (%) - returns the remainder of the division
print(10 % 3)   # 1

# Exponentiation (**) - power
print(2 ** 3)   # 8 (2 * 2 * 2)

Assignment Operators

Used to assign values to variables:

x = 5    # Basic assignment
x += 3   # Same as: x = x + 3 (x is now 8)
x -= 2   # Same as: x = x - 2 (x is now 6)
x *= 2   # Same as: x = x * 2 (x is now 12)

Common Mistakes

  • Division differences: In Python 3, / always results in a float. If you need an integer division, you must use //.

Lesson Progress

Practice This TopicTake QuizBuild a Python Project

Ready to practise?

Apply what you learned with a hands-on challenge.

Practise This Topic