Arithmetic Operators in Python (+, -, *, /, //, %, **)

Modified: | Tags: Python

This article explains Python's arithmetic operators and their usage.

Python supports basic arithmetic operations—addition, subtraction, multiplication, division, and exponentiation—for numeric types (int and float). When used with sequences like lists and strings, some of these operators perform actions like concatenation and repetition.

Note that the asterisks * and ** also have special meanings in function definitions and calls, where they are used for argument unpacking.

Basic Arithmetic Operations

Addition (+)

The + operator performs addition between numbers.

print(10 + 3)
# 13

Subtraction (-)

The - operator performs subtraction between numbers.

print(10 - 3)
# 7

Multiplication (*)

The * operator performs multiplication between numbers.

print(10 * 3)
# 30

Division (/)

The / operator performs division, always returning a floating-point result.

print(10 / 3)
# 3.3333333333333335

print(0.1 / 0.03)
# 3.3333333333333335

Note: In Python 2, integer division returned an int. Python 3 changed this behavior to always return a float.

Integer Division (//)

The // operator performs integer division (floor division), returning the largest integer less than or equal to the division result.

print(10 // 3)
# 3

print(0.1 // 0.03)
# 3.0

If either operand is a floating-point number, the result is a float—even when the outcome is mathematically an integer.

Modulo (%)

The % operator returns the remainder of division.

print(10 % 3)
# 1

print(0.1 % 0.03)
# 0.010000000000000009

Note: Floating-point modulo operations may introduce small numerical errors. For precise comparisons, see:

For getting both quotient and remainder in one operation, use the divmod() function:

Exponentiation (**)

The ** operator performs exponentiation (raising a number to a power).

You can also raise floating-point numbers and negative values to a power. In Python, 0 raised to the power of 0 is defined as 1.

print(10**3)
# 1000

print(2**0.5)
# 1.4142135623730951

print(10**-2)
# 0.01

print(0**0)
# 1

Division by Zero (ZeroDivisionError)

Attempting to divide by zero raises a ZeroDivisionError:

# print(10 / 0)
# ZeroDivisionError: division by zero

# print(10 // 0)
# ZeroDivisionError: integer division or modulo by zero

# print(10 % 0)
# ZeroDivisionError: integer modulo by zero

# print(0**-1)
# ZeroDivisionError: 0.0 cannot be raised to a negative power

To handle these exceptions, see:

Compound Assignment Operators

Basic arithmetic operators create new objects. Here's an example using f-strings to display variable values:

a = 10
b = 3
c = a + b

print(f'{a = }')
print(f'{b = }')
print(f'{c = }')
# a = 10
# b = 3
# c = 13

Compound assignment operators (like +=) modify the left-hand operand in place:

a = 10
b = 3
a += b

print(f'{a = }')
print(f'{b = }')
# a = 13
# b = 3

Python provides compound assignment forms for all basic arithmetic operators: +=, -=, *=, /=, %=, and **=.

a = 10
b = 3
a %= b

print(f'{a = }')
print(f'{b = }')
# a = 1
# b = 3

a = 10
b = 3
a **= b

print(f'{a = }')
print(f'{b = }')
# a = 1000
# b = 3

Type Conversion in Arithmetic Operations

When mixing integers and floats in operations, Python follows these rules:

  • For +, -, and *: The result is a float if any operand is a float
  • For /: The result is always a float (in Python 3)
  • For **: The result is an int only if both operands are integers and the result is an integer
print(2 + 3.0)
print(type(2 + 3.0))
# 5.0
# <class 'float'>

print(10 / 2)
print(type(10 / 2))
# 5.0
# <class 'float'>

print(2**3)
print(type(2**3))
# 8
# <class 'int'>

print(2.0**3)
print(type(2.0**3))
# 8.0
# <class 'float'>

print(25**0.5)
print(type(25**0.5))
# 5.0
# <class 'float'>

print(0.01**-2)
print(type(0.01**-2))
# 10000.0
# <class 'float'>

Operator Precedence

Python follows standard mathematical operator precedence rules:

The following expressions are equivalent:

print(100 / 10**2 + 2 * 3 - 5)
# 2.0

print(100 / (10**2) + (2 * 3) - 5)
# 2.0

Use parentheses () to explicitly control the order of operations:

print((100 / 10) ** 2 + 2 * (3 - 5))
# 96.0

Operations on Sequences (list, tuple, str)

Some arithmetic operators have special behavior when used with sequences like lists, tuples, or strings.

Concatenation (+)

The + operator concatenates sequences of the same type:

l1 = [1, 2, 3]
l2 = [10, 20, 30]

t1 = (1, 2, 3)
t2 = (10, 20, 30)

s1 = 'abc'
s2 = 'xyz'

print(l1 + l2)
# [1, 2, 3, 10, 20, 30]

print(t1 + t2)
# (1, 2, 3, 10, 20, 30)

print(s1 + s2)
# abcxyz

Attempting to concatenate incompatible types raises a TypeError. To add a single element, convert it to the appropriate sequence type:

# print(l1 + 4)
# TypeError: can only concatenate list (not "int") to list

print(l1 + [4])
# [1, 2, 3, 4]

For tuples, remember that a single-element tuple requires a trailing comma:

# print(t1 + 4)
# TypeError: can only concatenate tuple (not "int") to tuple

print(t1 + (4,))
# (1, 2, 3, 4)

Compound assignment works with concatenation:

l1 += l2
print(l1)
# [1, 2, 3, 10, 20, 30]

t1 += t2
print(t1)
# (1, 2, 3, 10, 20, 30)

s1 += s2
print(s1)
# abcxyz

For alternative ways to concatenate sequences, see:

Repetition (*)

The * operator repeats sequences when multiplied by an integer:

l = [1, 10, 100]
t = (1, 10, 100)
s = 'Abc'

print(l * 3)
# [1, 10, 100, 1, 10, 100, 1, 10, 100]

print(t * 3)
# (1, 10, 100, 1, 10, 100, 1, 10, 100)

print(s * 3)
# AbcAbcAbc

The order of operands does not affect the result:

print(3 * l)
# [1, 10, 100, 1, 10, 100, 1, 10, 100]

Non-integer multipliers raise a TypeError. Using a negative integer as the multiplier returns an empty sequence:

# print(l * 0.5)
# TypeError: can't multiply sequence by non-int of type 'float'

print(l * -1)
# []

Compound assignment works with repetition:

l *= 3
print(l)
# [1, 10, 100, 1, 10, 100, 1, 10, 100]

t *= 3
print(t)
# (1, 10, 100, 1, 10, 100, 1, 10, 100)

s *= 3
print(s)
# AbcAbcAbc

Repetition (*) has higher precedence than concatenation (+). Use parentheses to control the order:

l1 = [1, 2, 3]
l2 = [10, 20, 30]
print(l1 + l2 * 2)
# [1, 2, 3, 10, 20, 30, 10, 20, 30]

print((l1 + l2) * 2)
# [1, 2, 3, 10, 20, 30, 1, 2, 3, 10, 20, 30]

Related Categories

Related Articles