Start learning free

LESSON 15 OF 410 · CALCULATIONS WITH NUMBERS

Arithmetic Operators in Python

Lesson Read the full explanation

TL ; DR:

  • We can tell computers to perform calculations for us using +, -, *, /:

    print(2 + 6) # Prints 8
    print(10 - 7) # Prints 3
    print(3 * 3) # Prints 9
    print(6 / 2) # Prints 3
    


  • We can also use variables. In these examples, Python will replace variable num with its value 6 to do the calculations:

    num = 6
    print(num + 5) # Prints 11
    print(10 - num) # Prints 4
    print(3 * num) # Prints 18
    print(num / 2) # Prints 3
    



Full lesson:

Computers absolutely excel at performing calculations. The "compute" in their name comes from their historical association with providing answers to mathematical questions.

We've learned that in Python there are two types of numbers: integers, which are whole numbers like 10, and floating point numbers or floats, which are numbers with a decimal point like 3.145.


Arithmetic operators

Arithmetic operators are used with numeric values to perform common mathematical operations:

Operator Name Examples
+ Addition 3 + 5,  x + 5,  3 + y,  x + y
- Subtraction 8 - 3,  8 - y,  x - 3,  x - y
* Multiplication 3 * 5,  3 * y,  x * 5,  x * y
/ Division 8 / 4,  8 / y,  x / 8,  x / y
% Modulus 8 % 4,   8 % y,  x % 4,  x % y
** Exponentiation 3 ** 2,  3 ** y,   x ** 3,  x ** y
// Floor division 8 // 4,  8 // y,  x // 8,  x // y


Note that performing arithmetic on variables does not change the variable - you can only update a variable using the = sign.

Let's go through some examples together to better understand how each operator works:


Addition

# Declare 2 variables and initialize them:
a = 2
b = 3

print(a + b) # Output: 5

print(a + 10) # Output: 12

Subtraction

# Declare 2 variables and initialize them:
a = 2
b = 3

print(a - b) # Output: -1

print(10 - b) # Output: 7

Multiplication

# Declare 2 variables and initialize them:
a = 2
b = 3

print(a * 5) # Output: 10

print(-2 * a * b) # Output: -12

Division

# Declare 2 variables and initialize them:
a = 2
b = 3

print(b / a) # Output: 1.5

print(a / 10) # Output: 0.2

Exponentiation

The ** operator performs the exponential calculation (2**3 translates to 2 * 2 * 2):

# Declare 2 variables and initialize them:
a = 2
b = 3

print(2 ** 3) # Output: 8

print(b ** a) # Output: 9

We left the modulo (%) and floor division (//) operators for the next lesson.


Assignment
Follow the Coding Tutorial and let's practice with arithmetic operators!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    PYTHON · CALCULATIONS WITH NUMBERS

    Arithmetic Operators in Python, explained

    Python Arithmetic Operators: Making Your Code Do the Math

    Computers exist to compute — and Python makes math effortless. Whether you're calculating a discount, finding how many items fit in a box, or converting temperatures, arithmetic operators are the tools you reach for. Learning them puts you in control of numerical data, which shows up in virtually every program you'll write.

    The 7 Arithmetic Operators in Python

    OperatorNameExampleResult
    +Addition7 + 310
    -Subtraction7 - 34
    *Multiplication7 * 321
    /Division7 / 23.5 (always float)
    %Modulus (remainder)7 % 31
    **Exponentiation2 ** 8256
    //Floor division7 // 23 (rounds down)

    Basic Usage: Numbers and Variables

    All operators work with both raw numbers and variables. Python substitutes the variable's value before computing:

    price = 49.99
    quantity = 3
    
    total = price * quantity
    print(total)          # 149.97
    
    discount = total * 0.10
    final = total - discount
    print(round(final, 2))  # 134.97
    

    Division vs. Floor Division

    This is the most important distinction to memorize:

    # Regular division — always returns a float:
    print(10 / 3)    # 3.3333333333333335
    print(6 / 2)     # 3.0  (still a float, even when exact!)
    
    # Floor division — returns an integer (rounds toward -infinity):
    print(10 // 3)   # 3
    print(7 // 2)    # 3
    print(-7 // 2)   # -4  (rounds down, not toward zero)
    

    Use // when you need a whole number — for example, figuring out how many full rows fit in a grid.

    The Modulus Operator: Finding Remainders

    The % operator returns what's left over after division. It's used far more often than you'd expect:

    # Is a number even or odd?
    print(10 % 2)   # 0  → even (no remainder)
    print(7 % 2)    # 1  → odd
    
    # How many candies left after splitting equally?
    candies = 29
    kids = 4
    leftover = candies % kids
    print(leftover)   # 1 candy left over
    
    # What minute is it? (convert seconds to mm:ss)
    total_seconds = 137
    minutes = total_seconds // 60
    seconds = total_seconds % 60
    print(f"{minutes}m {seconds}s")   # 2m 17s
    

    Exponentiation: Powers Made Easy

    print(2 ** 10)   # 1024  (2 to the power of 10)
    print(3 ** 3)    # 27    (3 cubed)
    print(9 ** 0.5)  # 3.0   (square root)
    
    # Compound interest formula: A = P(1 + r)^t
    principal = 1000
    rate = 0.05
    years = 10
    amount = principal * (1 + rate) ** years
    print(round(amount, 2))   # 1628.89
    

    Order of Operations (PEMDAS)

    Python follows standard math order of operations. Use parentheses to be explicit:

    print(2 + 3 * 4)      # 14  (multiplication first)
    print((2 + 3) * 4)    # 20  (parentheses first)
    
    # Calculate average — parentheses matter!
    a, b, c = 10, 20, 30
    wrong   = a + b + c / 3    # 40.0  (divides only c by 3)
    correct = (a + b + c) / 3  # 20.0
    

    Common Mistakes to Avoid

    • Division by zero: x / 0 raises a ZeroDivisionError. Always validate denominators before dividing.
    • Integer vs. float confusion: 7 / 2 gives 3.5, not 3. Use // if you need a whole number.
    • Forgetting operator precedence: When in doubt, add parentheses to make your intent clear.
    # Division by zero — guard against it:
    divisor = 0
    if divisor != 0:
        print(10 / divisor)
    else:
        print("Cannot divide by zero")
    

    Key Takeaways

    • +, -, *, / work as expected; / always returns a float.
    • // performs floor division (integer result); % returns the remainder.
    • ** raises a number to a power (also works for square roots with 0.5).
    • Use parentheses to control order of operations and make code readable.

    What to Learn Next

    Now that you can do math in Python, learn augmented assignment operators (+=, -=, *=) to update variables more concisely. Then explore comparison operators to start making decisions in your programs with if statements.

    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free