Command Palette

Search for a command to run...

AI Programming

Arithmetic Operators

Introduction to Arithmetic Operators

Arithmetic operators are symbols that allow you to perform mathematical operations in Python. Think of operators like tools in a toolbox. Each tool has a specific function, and you need to know when and how to use them to get the right results.

Python provides seven main arithmetic operators that you can use to perform mathematical calculations. Each operator has a different order of precedence, so it's important to understand which one will be executed first.

Operator Precedence Order

Python has seven arithmetic operators grouped into three precedence levels. This order determines which operator will be executed first in an expression.

Highest Precedence

Exponentiation Operator **

The ** operator is used for exponentiation and has the highest precedence. This operator also has right associativity, unlike other operators.

Pythonexponentiation.py
>>> 2**38>>> 2**-10.5>>> 2**0.51.4142135623730951

The exponentiation operator is very useful for various calculations. You can use it to calculate squares, cubes, square roots, or even negative powers to get fractions.

Middle Precedence

The group of operators with middle precedence consists of four operators that you often use in daily calculations.

Multiplication Operator

The * operator is used to multiply two numbers.

Division Operator

The / operator performs division and always produces a float result, even when both operands are integers.

Pythondivision_examples.py
>>> 7.0 / 2.03.5>>> 7.0 / 23.5>>> 7 / 2.03.5>>> 7 / 23.5>>> 4 / 22.0

Note that even though 4 / 2 produces a whole number mathematically, Python still returns 2.02.0 as a float.

Integer Division Operator

The // operator performs integer division which rounds the result to the nearest smaller integer.

Pythoninteger_division.py
>>> 4 // 22>>> 7 // 23>>> -7 // 2-4>>> 7.0 // 23.0

Integer division works by rounding the result to the nearest smaller integer. For negative numbers like -7 // 2, the result is -4, not -3.

Modulo Operator %

The % operator returns the remainder of division. This operator follows the mathematical formula a%b=a(a//b)×ba \% b = a - (a // b) \times b.

Pythonmodulo_examples.py
>>> 5 % 41>>> -5 % 43>>> 5 % -4-3>>> -5 % -4-1

To understand modulo with negative numbers, note that 5 % 4 produces 1, while -5 % 4 produces 3. The modulo operator is very useful for determining whether a number is divisible or for getting the last digit of a number.

Lowest Precedence

Addition and Subtraction Operators

The + and - operators have the lowest precedence among all arithmetic operators.

Precedence Rules and Associativity

Expression Evaluation Order

Python evaluates expressions based on three main rules.

  1. Precedence order determines which operator is executed first
  2. Left associativity applies to operators with the same precedence, except exponentiation
  3. Parentheses can change the evaluation order
Pythonprecedence_examples.py
>>> 2 + 4 * 314>>> (2 + 4) * 318>>> 2 * 2**316>>> 6 / 2 * 412>>> 6 / (2 * 4)0.75

Associativity Rules

Associativity determines the evaluation order when there are multiple operators with the same precedence in one expression.

Left Associativity

Evaluation starts from left to right. Most operators use this rule.

Pythonleft_associative.py
>>> 6 / 2 * 412>>> 10 - 3 - 25

The expression 6 / 2 * 4 is evaluated as (6 / 2) * 4 = 3 * 4 = 12, not 6 / (2 * 4) = 6 / 8 = 0.75.

Right Associativity

Evaluation starts from right to left. Only the exponentiation operator ** uses this rule.

Pythonright_associative.py
>>> 2 ** 3 ** 2512>>> 3 ** 2 ** 281

The expression 2 ** 3 ** 2 is evaluated as 2 ** (3 ** 2) = 2 ** 9 = 512, not (2 ** 3) ** 2 = 8 ** 2 = 64.

Python Shell as Calculator

Python shell is very effective as a calculator for various mathematical calculations. You can type mathematical expressions directly and Python will evaluate them in real-time.

Important things to remember are that the / operator always produces a float even when the result is a whole number. Conversely, the // operator produces an integer unless one of the operands is a float. For the modulo operator, the result will follow the sign of the divisor and use a consistent mathematical formula.

Arithmetic Operators Summary Table

PrecedenceOperatorFunctionAssociativity
Highest**ExponentiationRight
Middle*MultiplicationLeft
Middle/DivisionLeft
Middle//Integer divisionLeft
Middle%ModuloLeft
Lowest+AdditionLeft
Lowest-SubtractionLeft

By understanding arithmetic operators and their precedence order, you can write accurate and efficient mathematical expressions in Python programs. Use parentheses when you want to change the standard evaluation order to get the desired results.