Skip to content

Module 4 · Operators — topic 1 of 16

Arithmetic operators

An overview of Python’s seven arithmetic operators (+, -, *, /, //, %, **), the types of results they produce, and how they work with integers, floats and other types.

Beginner 12 min read·Lesson 44 of 306 in Python Programming

Learning objectives

  • List the seven arithmetic operators and what each does
  • Predict whether a result is an int or a float
  • Use unary minus and plus

Definition

An overview of Python’s seven arithmetic operators (+, -, *, /, //, %, **), the types of results they produce, and how they work with integers, floats and other types.

Introduction

An operator is a symbol that performs an operation on one or more values, called operands. In 7 + 3, the + is the operator and 7 and 3 are the operands. Arithmetic operators do maths.

Python has seven arithmetic operators. Each of the following lessons looks at one in detail; this lesson gives you the overview and the rules they share.

Why it matters

Calculations are at the heart of billing, grading, statistics, games and science. Knowing exactly what each operator returns, especially the difference between / and //, prevents some of the most common numeric bugs.

Detailed explanation

OperatorNameExampleResult
+Addition7 + 310
-Subtraction7 - 34
*Multiplication7 * 321
/Division7 / 23.5
//Floor division7 // 23
%Modulus (remainder)7 % 21
**Exponentiation7 ** 249

Result types: if both operands are ints, +, -, *, // and % give an int. / always gives a float. If either operand is a float, the result is a float.

Unary operators take one operand: -x negates a value, +x leaves it unchanged. -(-5) is 5.

Some operators also work on other types: + joins strings and lists, * repeats them. Operators mean whatever the operand types define, which is called operator overloading.

Arithmetic follows the familiar order from school maths: ** first, then *, /, // and %, then + and -. Parentheses override the order. The precedence lessons at the end of this module cover the full rules.

Syntax

Text
result = operand1 operator operand2
negative = -operand
  • Binary operators sit between two operands.
  • Spaces around operators are optional but improve readability.
  • Unary minus is written directly before its operand.

Code examples

All seven operators

Python
a, b = 17, 5
print(a + b, a - b, a * b)
print(a / b, a // b, a % b)
print(a ** 2)
Output
22 12 85
3.4 3 2
289

Result types

Python
print(type(8 / 2).__name__)
print(type(8 // 2).__name__)
print(type(8 + 2.0).__name__)
Output
float
int
float

More examples

Splitting a bill

Python
bill = 2_350
friends = 4
tip_percent = 10
total = bill + bill * tip_percent / 100
share = total / friends
whole_rupees = total // friends
left_over = total % friends
print("Total with tip:", total)
print("Exact share:", share)
print("Rounded down share:", whole_rupees)
print("Left over:", left_over)
Output
Total with tip: 2585.0
Exact share: 646.25
Rounded down share: 646.0
Left over: 1.0

Step-by-step explanation

  • bill * tip_percent / 100 is 235.0, a float because / is used, so total becomes the float 2585.0.
  • total / friends gives the exact share, 646.25.
  • total // friends rounds down to 646.0; it is a float because total is a float.
  • total % friends is the remainder after four equal whole shares: 1.0 rupee.

Expected output

Running “Splitting a bill” prints exactly:

TextOutput
Total with tip: 2585.0
Exact share: 646.25
Rounded down share: 646.0
Left over: 1.0

Practical applications

  • Payroll systems combine +, -, * and / to calculate gross and net pay.
  • Inventory software uses // and % to pack items into full boxes and a remainder.
  • Scientific code uses ** for powers, square roots (x ** 0.5) and growth formulas.

Best practices

  • Put spaces around binary operators: a + b, not a+b.
  • Use parentheses to make the order of a long calculation obvious.
  • Pick // or / deliberately depending on whether you need a whole number.

Common mistakes

Expecting / to give a whole number

Avoid

pages = 10 / 2 # 5.0

Prefer

pages = 10 // 2 # 5

/ always returns a float in Python 3. Use // when you need an integer result.

Writing ^ for powers

Avoid

area = side ^ 2

Prefer

area = side ** 2

^ is the bitwise XOR operator in Python, not exponentiation.

Key takeaways

  • Python has seven arithmetic operators: + - * / // % **.
  • / always returns a float; the others return ints for int operands.
  • + and * also join and repeat strings and lists.

Try it yourself

  1. 1
    Compute how many full weeks and remaining days are in 100 days using // and %.
  2. 2
    Predict the type and value of 9 // 2.0, then check.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

14 questions cover the “Operators” module. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
  • BeginnerWhat is printed?
  • BeginnerWhat is printed?
Practise this module