Codectionary / Developer documentation / Python

Arithmetic Operators

Python supports the standard arithmetic operators for numeric calculations: addition, subtraction, multiplication, and division, alongside two operators many languages lack directly - floor division (//), which discards the remainder, and the power operator (**), which raises a number to an exponent. Python also has the modulus operator (%) for finding a remainder.

Syntax

+  -  *  /  //  %  **

Examples

Basic Arithmetic

Addition, subtraction, multiplication, and true division.

print(10 + 3)   # 13
print(10 - 3)   # 7
print(10 * 3)   # 30
print(10 / 3)   # 3.3333333333333335  (always returns a float)

Floor Division and Modulus

Getting a whole-number quotient and the remainder of a division.

print(10 // 3)  # 3   (floor division - discards the remainder)
print(10 % 3)   # 1   (modulus - just the remainder)

# Common use: checking even/odd
number = 7
if number % 2 == 0:
    print("Even")
else:
    print("Odd")

Exponentiation

Raising a number to a power with **.

print(2 ** 10)   # 1024
print(9 ** 0.5)  # 3.0  (square root via a fractional exponent)
print(5 ** 2)    # 25

Best practices

  • Use // when you specifically need an integer result from division, and / when you want the precise float result
  • Remember that / always returns a float in Python 3, even when dividing two integers evenly (10 / 2 is 5.0, not 5)
  • Use ** rather than importing math.pow() for simple integer or float exponentiation
  • Watch for floating-point precision quirks (0.1 + 0.2 is not exactly 0.3) when comparing float results directly

At a glance

Purpose
Scripting and general-purpose applications
File extension
.py
Runs in
Python interpreter
Usually used with
Python standard library and packages

Specifications & further reading

Related Python documentation

True
In Python, True is a built-in constant that represents the boolean value of true. Alongside False, True enables logical and conditional operations. It's a fundamental keyword used throughout Python programming for decision-making, loops, comparisons, and boolean logic. True plays a key role in if statements, while loops, and logical expressions.
False
False is Python's built-in constant representing the boolean value false. It's the logical opposite of True and is essential for conditional logic, loop control, and boolean operations. False is used to indicate negative conditions, failed validations, or when something is not the case. Understanding False is crucial for writing effective control flow in Python programs.
None
None is Python's built-in singleton value that represents the absence of a value or a null result. It is its own type (NoneType) and is commonly used as a default parameter value, a placeholder before a variable is assigned real data, or a function's implicit return value when nothing is explicitly returned. Unlike 0, an empty string, or an empty list, None specifically means 'no value here', not 'an empty value'.
Variables & Assignment
Variables in Python are names bound to values, created the moment you first assign to them - there's no need to declare a type in advance, since Python is dynamically typed. A single variable can be reassigned to a completely different type later in the program. Python also supports several convenient assignment patterns, including assigning multiple variables at once and using augmented assignment operators.