Skip to main content

Arithmetic Operators — Doing Math with Python

You've been doing math your whole life. Five plus three equals eight. Ten minus four equals six. Now you're going to do the exact same math with Python—except Python calls them operators instead of just "math".

An operator is a symbol that tells Python to do something with values. In this lesson, you'll learn the seven arithmetic operators that let you perform calculations. You already know the concepts; Python just has its own symbols for them.

What It Is: Operators as Verbs for Numbers

Think of operators like verbs in English. Just like "run", "jump", and "walk" are actions, operators are actions you perform on numbers.

Python has seven arithmetic operators:

  • + Addition — combines values
  • - Subtraction — finds the difference
  • * Multiplication — scales values
  • / Division — splits into parts
  • // Floor division — counts whole groups (no decimals)
  • % Modulus — finds the remainder
  • ** Exponentiation — raises to a power

All of these do exactly what you learned in math class. The difference is that Python needs to know what to do with types—and that's where it gets interesting.

The Seven Arithmetic Operators

Let's see all seven operators in action. Each one takes two numbers (called operands) and produces a result.

Addition and Subtraction: The Basics

Loading Python environment...

Both addition and subtraction keep the type consistent. When you add two integers, you get an integer back. Same with subtraction.

💬 AI Colearning Prompt

"Python is designed to be helpful with numbers. But why does Python have both / and // for division when math just has one division symbol? Explain the difference and why this matters."

Notice something: we're asking AI to explain the design choice behind operators, not just what they do. That's more useful than memorization.

Multiplication, Division, and Floor Division

Loading Python environment...

Here's something important: division with / always returns a float, even when both numbers are integers. This surprises many beginners because they expect 10 / 5 to give 2 (an int), but it actually gives 2.0 (a float).

Why? Because division often produces decimals, so Python designed / to always return float. If you want an integer result (discarding the decimal), you use // (floor division).

🎓 Expert Insight

In AI-native development, you don't memorize why Python made this choice. You understand what's happening (division produces floats, floor division gives integers) and use the right operator for your intent. Then you verify with type() to be sure.

Let's verify this with type():

Loading Python environment...

Notice: when you floor divide two floats, the result is still a float. Python follows this rule: if either operand is a float, the result is a float.

Modulus: The Remainder Operator

Loading Python environment...

The modulus operator % is useful when you care about what's left over. For example:

  • Check if a number is even: 5 % 2 gives 1 (odd), 4 % 2 gives 0 (even)
  • Find the last digit: 234 % 10 gives 4
  • Cycle through values: 7 % 3 gives 1, 8 % 3 gives 2, 9 % 3 gives 0 (then it repeats)

Exponentiation: Raising to a Power

Loading Python environment...

Notice the symbol: ** (two asterisks), not ^. The caret symbol ^ does something different in Python (bitwise XOR), so don't use it for exponentiation.

🤝 Practice Exercise

Ask your AI: "I understand that arithmetic operators in Python are +, -, *, /, //, %, **. But why does / always return a float even when dividing two integers? And when would I actually use // vs. /? Give me a concrete example where using the wrong one would cause a bug."

Expected Outcome: You'll understand the design decisions behind Python's division operators and how they interact with types.

Type Behavior: Why Types Change

Here's where it gets interesting. When you combine operands of different types, Python follows rules about what type the result is.

Loading Python environment...

The pattern: When you mix int and float, the result is always float. Python considers this safe because a float can represent any integer value (though with potential precision loss for very large numbers).

Operator Precedence: Order Matters

Just like in math, operators have an order of operations. Multiplication happens before addition.

Loading Python environment...

Python follows PEMDAS (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction). But honestly, the best practice is to use parentheses. Make your intent clear.

Loading Python environment...

Parentheses aren't just for changing order—they're for clarifying your intent to anyone reading your code (including yourself a month from now).

💬 AI Colearning Prompt

"When I see 2 + 3 * 4, how does Python know to do multiplication first? Explain operator precedence. And when should I use parentheses even if they're not required?"

Putting It Together: A Complete Example

Let's see all seven operators working together:

Loading Python environment...

This example shows:

  • All seven operators working
  • Different types in the results (int vs. float)
  • Type verification with type()

Common Mistakes (And Why They Happen)

Mistake 1: Expecting 5 / 2 to give 2

Loading Python environment...

Mistake 2: Forgetting that mixing int and float gives float

Loading Python environment...

Mistake 3: Trying to use / for exponentiation

Loading Python environment...


Try With AI

Ready to apply all 7 arithmetic operators in real calculations?

🔍 Explore Operator Behaviors:

"Compare the 7 arithmetic operators (+, -, *, /, //, %, **). For 10 and 3, show me the result of each operation and explain: (1) what type the result is (int or float?), (2) why / returns float but // returns int, (3) when I'd use each operator in real applications. Include type() verification for each result."

🎯 Practice Calculator Building:

"Create a calculator program that takes num1 = 10 and num2 = 3, performs all 7 operations, and displays results with f-strings like '10 + 3 = 13'. Add type hints for all variables. Then review my code for: correct operator usage, proper type hints (especially division results), and clear output formatting. Point out any type inconsistencies."

🧪 Test Division Edge Cases:

"I'm calculating how many groups of 3 people I can make from 10 people. Compare 10 / 3 vs. 10 // 3. Which is correct for counting groups? Show me: (1) the result and type of each, (2) why one makes sense for counting and the other doesn't, (3) what happens with 10 / 0 vs. 10 // 0. Create code demonstrating the ZeroDivisionError."

🚀 Apply to Your Calculations:

"I need to do these calculations in my project: [describe calculations like splitting items, calculating prices, computing averages, etc.]. For each, recommend which arithmetic operator(s) to use. Add validation to prevent division by zero and explain why some operations need float while others need int results."