Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 149 ยท Operators

Arithmetic Operators

Master Python's arithmetic operators: + - * / // % and **. Learn floor division with negatives, modulo tricks, divmod(), and the operator precedence rules.

An operator is a symbol that combines values into a new value. You've already used the arithmetic ones to do quick calculations. This lesson looks at them closely: what each one returns, how they behave with negative numbers, and the order Python evaluates them in when several appear in one expression.

These details matter more than you might expect. Off-by-one errors in pagination, wrong results from negative remainders and surprising answers to -2 ** 2 all come from the rules covered here.

The seven arithmetic operators

Operator Name Example Result
+ Addition 7 + 3 10
- Subtraction 7 - 3 4
* Multiplication 7 * 3 21
/ Division 7 / 2 3.5
// Floor division 7 // 2 3
% Modulo (remainder) 7 % 2 1
** Exponent (power) 7 ** 2 49

There are also two unary operators that take a single value: -x flips the sign and +x leaves it unchanged.

Python All seven operators

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Result types

The type of the result depends on the operands:

  • With two ints, +, -, *, //, % and ** (with a non-negative exponent) give an int.
  • If either operand is a float, the result is a float.
  • / always returns a float, even when the division is exact: 10 / 2 is 5.0.

Python's integers have no size limit, so 2 ** 200 gives you the exact 61-digit answer. Floats, on the other hand, are approximations, as you saw in Numbers.

Python What type comes out?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Dividing by zero with /, // or % raises ZeroDivisionError. If a divisor might be zero, check it first.

Floor division and modulo

// and % are a matched pair. Floor division gives how many whole times one number fits into another; modulo gives what's left over. They always satisfy this rule:

Python
(a // b) * b + (a % b) == a

Floor means "round down toward negative infinity", not "toward zero". With positive numbers you won't notice the difference. With negative numbers you will:

Python Negative numbers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

That last line shows why Python's choice is handy: (2 - 5) % 24 is 21, a valid hour. In some other languages the same expression gives -3.

Note: If you're porting code from C, Java or JavaScript, watch out: those languages truncate toward zero, so their -7 / 2 (integer) is -3 and their remainder of -7 % 2 is -1.

Everyday uses of // and %

These two operators show up constantly in practical code:

Python Practical // and %

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The page count trick, -(-a // b), divides and rounds up using only integers. math.ceil(a / b) gives the same answer but goes through a float.

divmod(): both at once

When you need the quotient and the remainder together, divmod(a, b) returns them as a pair. You can unpack the pair into two variables in one line:

Python divmod()

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Unpacking like hours, minutes = ... is covered fully in Unpacking and Star Expressions.

Operator precedence

When an expression has several operators, Python follows precedence rules to decide which to evaluate first, just like in math class. From highest to lowest:

  1. ( ) parentheses
  2. ** exponent
  3. -x, +x unary sign
  4. *, /, //, %
  5. +, -

Operators on the same level run left to right, except **, which runs right to left.

Python Precedence in action

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

-2 ** 2 catches many people: the exponent binds tighter than the minus sign, so Python computes -(2 ** 2). Comparison and logical operators sit below all of these; you'll see them in Comparison and Logical Operators.

Tip: When in doubt, add parentheses. (price * quantity) + shipping is no slower than the version without them, and a reader doesn't need to recall precedence rules.

Operators on other types

Some arithmetic operators also work on non-numbers. + joins strings and lists, and * repeats them:

Python
"ab" + "cd"      # 'abcd'
"-" * 20         # '--------------------'
[0] * 3          # [0, 0, 0]
[1, 2] + [3]     # [1, 2, 3]

% on a string is an old formatting style ("%d items" % 5); prefer f-strings in new code. Your own classes can support operators too, through Special (Dunder) Methods.

Built-in helpers for arithmetic

Alongside the operators, a few built-in functions handle common numeric jobs without any imports:

  • abs(-7) gives the distance from zero, 7.
  • pow(2, 10) is the same as 2 ** 10, and pow(base, exp, mod) computes a power modulo a number efficiently, which is handy in cryptography puzzles.
  • round(3.14159, 2) rounds to two decimal places.
  • min(), max() and sum() work on several values or a whole list.

For anything more advanced, such as square roots, logarithms, trigonometry or rounding up with ceil, reach for the math module, covered in math and random.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does 20 / 4 evaluate to?

    The / operator always returns a float, even when the division is exact.
  2. 2. What is -9 // 4?

    Floor division rounds toward negative infinity. -9 / 4 is -2.25, and its floor is -3.
  3. 3. What does -3 ** 2 evaluate to?

    ** has higher precedence than unary minus, so Python computes -(3 ** 2).
  4. 4. What does divmod(50, 7) return?

    7 goes into 50 seven times (49) with 1 left over, so the pair is (50 // 7, 50 % 7) = (7, 1).
  5. 5. How does 2 ** 3 ** 2 group?

    Exponentiation groups right to left, so 3 ** 2 runs first.

Summary

  • Python's arithmetic operators are + - * / // % **, plus unary - and +.
  • / always returns a float; the others keep int results when both operands are ints.
  • // floors toward negative infinity, and % takes the sign of the divisor, so (a // b) * b + a % b == a.
  • divmod(a, b) returns (a // b, a % b) in one step.
  • Precedence: parentheses, **, unary sign, * / // %, then + -. ** groups right to left, so -2 ** 2 is -4.
  • Dividing by zero raises ZeroDivisionError.

Next: Comparison and Logical Operators, where you'll ask questions about values and combine the answers.