Arithmetic Operators in Python

Learn every Python arithmetic operator, including addition, subtraction, multiplication, division, floor division, modulo, and exponentiation, with clear examples for each one.

5 min read

Arithmetic operators in Python handle the mathematical operations you need in nearly every program. You use them to add scores in a game, calculate a discount on a shopping cart total, determine how many items fit on each page of search results, or figure out whether a number is even by checking its remainder. Python provides seven arithmetic operators, and each one behaves predictably across integers and floats. Two of them extend their behavior to strings and other sequences, which keeps the language consistent across different data types.

This article is part of the operators section that started with a broad overview of all seven operator categories. If you have not read the overview yet, start with Python operators explained to see the full map. After you finish this article, the next logical step is to learn how assignment operators let you store and update the results of these calculations in variables. The arithmetic operators work closely with the numeric types covered earlier in Python data types explained, and the same operators behave differently depending on whether you use integers or floats.

Addition, subtraction, and multiplication

The three most familiar operators work exactly as you expect from basic mathematics. Addition returns the sum of two numbers. Subtraction returns the difference. Multiplication returns the product. All three work with integers and floats, and Python automatically converts the result to a float when either operand is a float.

pythonpython
total = 10 + 5       # 15
change = 7.5 - 2.3   # 5.2
product = 4 * 3.0    # 12.0 (float because one operand is float)

Python also extends addition and multiplication to work with sequences. When both operands are strings, addition concatenates them into a single string. When the left operand is a string and the right operand is an integer, multiplication repeats the string that many times. The same pattern holds for lists and tuples. This consistency across types is one of the reasons Python code stays readable even as programs grow.

The subtraction operator does not work on strings or lists because there is no unambiguous meaning for subtracting one sequence from another. Attempting to subtract a character from a string raises a TypeError. If you need to remove characters from a string or items from a list, you will use string methods or list methods instead, which are covered in earlier sections on strings and lists.

True division and floor division

Python provides two division operators, and the difference between them is one of the most common points of confusion for beginners coming from other languages. True division, written as a single slash, always returns a float, even when both operands are integers and the result is a whole number. Floor division, written as a double slash, divides and then rounds down to the nearest integer, returning an integer when both operands are integers.

pythonpython
print(7 / 2)    # 3.5 (always a float)
print(7 // 2)   # 3   (integer, rounded down)
print(8 / 2)    # 4.0 (float, even though it divides evenly)
print(8 // 2)   # 4   (integer)

Floor division rounds toward negative infinity, not toward zero. This matters when one operand is negative. The expression negative seven floor-divided by two returns negative four because negative 3.5 rounded down is negative 4, not negative 3. This behavior is consistent with the mathematical floor function and with how the modulo operator works in Python. If you need truncation toward zero instead, convert to an integer after true division using the int function.

Modulo for remainders

The modulo operator, written as a percent sign, returns the remainder after dividing the left operand by the right operand. For positive numbers, this is straightforward arithmetic: ten modulo three returns one because three goes into ten three times with one left over. Modulo is commonly used to check whether a number is even or odd by testing the remainder after division by two, to wrap values into a fixed range like clock arithmetic, or to determine whether one number is a multiple of another.

In Python, the result of modulo always has the same sign as the right operand. This design ensures that a mathematical identity holds for all integers and makes modulo useful for cyclic operations like rotating through a list of values. The result of negative ten modulo three is two, not negative one, which matches the mathematical convention that the remainder should be non-negative when the divisor is positive.

Exponentiation

The exponentiation operator, written as two asterisks, raises the left operand to the power of the right operand. Two raised to the third power equals eight. This operator works with both integers and floats, and it even handles fractional exponents through the same rules that the built-in pow function uses. Raising a number to 0.5 computes its square root, and raising to negative one computes its reciprocal.

Exponentiation has the highest precedence of any Python operator and groups from right to left. When you write multiple exponentiations in a row without parentheses, Python evaluates from the right side first. This means two to the three to the two evaluates as two to the ninth, which is 512, not as the quantity two-cubed squared which would be 64. Use parentheses whenever the intent is not obvious from a quick glance.

Operator precedence for arithmetic

Python follows the standard mathematical order of operations. Parentheses first, then exponentiation, then multiplication and division (including floor division and modulo) from left to right, and finally addition and subtraction from left to right. This means an expression with mixed operators like 2 + 3 * 4 evaluates to 14, not 20, because multiplication runs before addition.

Multiplication, true division, floor division, and modulo all share the same precedence level and evaluate left to right when they appear consecutively. Addition and subtraction share a lower precedence level and also evaluate left to right. The unary minus, which negates a single operand, has higher precedence than the binary arithmetic operators but lower than exponentiation. This is why negative three squared equals negative nine: exponentiation runs first on three, and then the unary minus negates the result. Write parentheses around negative three if you intend to square negative three.

Rune AI

Rune AI

Key Insights

  • Python has seven arithmetic operators: addition, subtraction, multiplication, true division, floor division, modulo, and exponentiation.
  • True division always returns a float, while floor division returns an integer when both operands are integers.
  • The modulo operator returns the remainder after division and is commonly used to check divisibility.
  • The addition and multiplication operators also work on strings and lists for concatenation and repetition.
  • Python follows standard mathematical precedence: parentheses override everything, then exponentiation, then multiplication and division, then addition and subtraction.
RunePowered by Rune AI

Frequently Asked Questions

What is the difference between `/` and `//` in Python?

The `/` operator performs true division and always returns a float, even when both operands are integers. The `//` operator performs floor division, which divides and then rounds down to the nearest integer. For example, `7 / 2` returns `3.5` while `7 // 2` returns `3`.

Does Python follow the standard order of operations?

Yes. Python follows PEMDAS: Parentheses first, then Exponentiation, then Multiplication and Division from left to right, and finally Addition and Subtraction from left to right. You can use parentheses to override the default order and make your expressions clearer.

Can I use arithmetic operators on strings?

The `+` operator concatenates two strings, and the `*` operator repeats a string a given number of times. Other arithmetic operators like `-` and `/` do not work on strings and will raise a TypeError.

Conclusion

Arithmetic operators are the foundation of computation in Python. Addition, subtraction, multiplication, and division handle everyday math, while floor division, modulo, and exponentiation solve specialized problems that come up in loops, data processing, and algorithm design.