Lesson 02
Arithmetic Operators
Perform addition, subtraction, multiplication, true division, floor division, modulo, and exponentiation in Python.
Arithmetic operators perform mathematical operations such as addition, subtraction, multiplication, and division.
When values, variables, or objects are connected using arithmetic operators, the result is an arithmetic expression.
a = 10
b = 3
print(a + b)13Visual reference
10 + 3 becomes the arithmetic expression 13.
What you'll learn
- The seven arithmetic operators
- Why
/always returns a float quotient - How
//behaves with integers and floats - Finding square roots using
** 0.5
The seven arithmetic operators
| Symbol | Meaning | Example | Result |
|---|---|---|---|
+ |
Addition | 10 + 3 |
13 |
- |
Subtraction | 10 - 3 |
7 |
* |
Multiplication | 10 * 3 |
30 |
/ |
True division | 10 / 3 |
3.333… |
// |
Floor division | 10 // 3 |
3 |
% |
Modulo | 10 % 3 |
1 |
** |
Exponentiation | 10 ** 3 |
1000 |
Interactive playground
Interactive playground
Adjust the values and see every arithmetic operator update instantly.
Addition
13
10 + 3
Subtraction
7
10 - 3
Multiplication
30
10 * 3
True division
3.333333
10 / 3
Floor division
3
10 // 3
Modulo
1
10 % 3
Exponentiation
1000
10 ** 3
Equivalent Python code
a, b = 10, 3
print("sum", a + b)
print("sub", a - b)
print("mul", a * b)
print("div", a / b)
print("floor", a // b)
print("mod", a % b)
print("pow", a ** b)Square root using `** 0.5`
16 ** 0.5
4
Understanding / vs //
True division (/)
print(10 / 3)3.3333333333333335Floor division (//)
print(10 // 3)3The // operator removes the fractional part by taking the floor of the result.
What happens when floats are involved?
Even with //, if either operand is a float, the result is still a float type.
| Expression | Result | Type |
|---|---|---|
10 / 3 |
3.3333333333333335 | float |
10 // 3 |
3 | int |
10.0 // 3.0 |
3.0 | float |
10 // 3.0 |
3.0 | float |
print(10.0 / 3.0)
print(10.0 // 3.0)
print(10.0 / 3)
print(10.0 // 3)
print(10 / 3.0)
print(10 // 3.0)3.3333333333333335
3.0
3.3333333333333335
3.0
3.3333333333333335
3.0Exponentiation (**)
The ** operator raises a number to a power.
print(10 ** 3)
print(2 ** 5)1000
32Read 10 ** 3 as “10 to the power of 3.”
Square root using ** 0.5
n = 16
print(n ** 0.5)4.0Use the interactive block above to try other values, or run 81 ** 0.5 in the playground.
Interactive program (two numbers)
This version uses input() to read values from the keyboard.
a = int(input("Enter value of a: "))
b = int(input("Enter value of b: "))
print("sum({}, {}) = {}".format(a, b, a + b))
print("div({}, {}) = {}".format(a, b, a / b))
print("floor_div({}, {}) = {}".format(a, b, a // b))
print("pow({}, {}) = {}".format(a, b, a ** b))Example input: 10 and 3.
Practice exercises
- Predict the output of
10 // 3and10 / 3before running. - Predict the result of
10.0 // 3. - Print the square root of
81using81 ** 0.5.
Quick knowledge check
-
What does
/return?
A float quotient. -
What is
10 // 3?
3 -
What is
10.0 // 3?
3.0
Summary
- Python has seven arithmetic operators:
+,-,*,/,//,%,**. /always performs true division and returns a float.//performs floor division; with floats, the result remains a float.**is used for powers;** 0.5gives a compact square root.
Next lesson: Assignment Operator — =, multi-target assignment, and swapping variables.
Loading playground…