DevHireLab
Tutorials
BootcampProblemsCode SimulatorAI InterviewSoonContact
DevHireLab
Tutorials
BootcampProblemsCode SimulatorAI InterviewSoonContact
Getting Started
01
What is Python and Why Learn It in 2026
02
Installing Python on Windows, Mac, and Linux
03
Setting up VS Code / PyCharm for Python
04
Understanding the Python Interpreter and REPL
05
Writing and Running Your First Python Program
06
Python Syntax and Indentation Rules
Variables and Data Types
01
Variables and Naming Conventions in Python
02
Numbers in Python (int, float, complex)
03
Strings and String Methods in Python
04
Booleans in Python
05
Type Conversion / Casting in Python
06
Understanding the None Type in Python
Operators
01
Arithmetic Operators in Python
02
Comparison Operators in Python
03
Logical Operators in Python
04
Assignment Operators in Python
05
Bitwise Operators in Python
06
Identity (is) and Membership (in) Operators
07
Walrus Operator (:=) in Python
Input, Output & Strings
01
input() and print() Functions in Python
02
f-strings and String Formatting in Python
03
String Slicing and Indexing in Python
04
Common String Methods (split, join, strip, replace)
05
Escape Characters in Python Strings
Control Flow
01
if, elif, else Statements in Python
02
Nested Conditionals in Python
03
for Loops in Python
04
while Loops in Python
05
break, continue, and pass Statements
06
Loop with else Clause in Python
07
match-case (Structural Pattern Matching)
Data Structures
01
Python Lists: Creation, Indexing, Slicing, Methods
02
Python Tuples and Immutability
03
Python Sets and Set Operations
04
Python Dictionaries: Keys, Values, and Methods
05
List Comprehensions in Python
06
Dictionary & Set Comprehensions in Python
07
Working with Nested Lists and Dictionaries
Functions
01
Defining and Calling Functions in Python
02
Function Arguments: Positional, Keyword, Default
03
*args and **kwargs in Python
04
Return Statements in Python Functions
05
Lambda Functions in Python
06
Recursion in Python
07
Variable Scope: Local, Global, Nonlocal
08
Docstrings and Function Annotations
Functional Programming Basics
01
map(), filter(), and reduce() in Python
02
Closures in Python Explained
03
Python Decorators: Basic to Advanced
04
Generators and yield in Python
Object-Oriented Programming (OOP)
01
Classes and Objects in Python
02
The init Constructor in Python
03
Instance vs Class Variables in Python
04
Inheritance in Python (Single, Multiple, Multilevel)
05
Polymorphism in Python
06
Encapsulation in Python
07
Abstraction in Python with the abc Module
08
Magic/Dunder Methods in Python
09
Static and Class Methods in Python
10
The @property Decorator in Python
11
Python Dataclasses Explained
Error Handling
01
try, except, finally in Python
02
Handling Multiple Exceptions in Python
03
Raising Custom Exceptions in Python
04
Understanding Python's Exception Hierarchy
05
Assertions in Python
File Handling
01
Reading and Writing Text Files in Python
02
Working with the with Statement in Python
03
Working with CSV Files in Python
04
Working with JSON Files in Python
05
Working with Directories (os, pathlib) in Python
Modules and Packages
01
Importing Built-in Modules in Python
02
Creating Your Own Modules in Python
03
Understanding Packages and init.py
04
Python Standard Library Overview
05
Installing Packages with pip
Advanced Core Concepts
01
Iterators and Iterables in Python
02
Context Managers in Python (with, contextlib)
03
Multithreading Basics in Python
04
Multiprocessing Basics in Python
05
Asyncio Basics: async/await in Python
06
Memory Management and Garbage Collection in Python
07
Type Hints in Python (typing module)

Arithmetic Operators in Python

Almost every calculation you write in Python comes down to a small handful of symbols: +, -, *, and a few others. These are Python's arithmetic operators, and while the basics are simple, a few of them behave in ways that catch beginners off guard — particularly division and how negative numbers interact with it. This article covers all seven, plus the precedence rules that determine how they combine.

What Are Arithmetic Operators?

An operator is a symbol that performs an operation on values, called operands. In 5 + 3, the + is the operator, and 5 and 3 are the operands.

Python has seven core arithmetic operators:

Operator

Operation

+

Addition

-

Subtraction

*

Multiplication

/

True division

//

Floor division

%

Modulo (remainder)

**

Exponentiation

Python fully supports mixed arithmetic — combining an int and a float in the same expression works without any special handling, as covered in the earlier article on type conversion:

result = 5 + 2.5
print(result)   # 7.5

The rest of this article walks through each operator, including a couple that behave a little differently than you might expect.

The Core Operators: Addition, Subtraction, Multiplication

Straightforward numeric use
print(5 + 3)    # 8  — addition
print(5 - 3)    # 2  — subtraction
print(5 * 3)    # 15 — multiplication
Bonus behavior: strings

Two of these python operators do something unexpected — but genuinely useful — when applied to strings instead of numbers.

+ concatenates strings together:

greeting = "Hello, " + "World!"
print(greeting)   # Hello, World!

* repeats a string a given number of times:

line = "-" * 20
print(line)   # --------------------

That second one is a handy shortcut for building separator lines or simple visual formatting without a loop.

Common pitfall: mixing strings and numbers

Trying to use + between a string and a number directly raises an error, since Python won't guess whether you meant to combine them as text or convert one of them first:

age = 30
print("Age: " + age)
# TypeError: can only concatenate str (not "int") to str

The fix is explicit conversion, using str():

print("Age: " + str(age))   # Age: 30

(Or, as covered in the strings article earlier in this series, an f-string sidesteps this entirely: f"Age: {age}".)

Division Operators: True Division vs. Floor Division

This is where Python's arithmetic gets a little more interesting — and where floor division vs division confusion trips up a lot of beginners.

True division (/)

The / operator always returns a float, even when the division happens to come out evenly:

print(10 / 2)   # 5.0 — note the .0, even though it divides evenly
print(10 / 3)   # 3.3333333333333335
Floor division (//)

The // operator, by contrast, discards any remainder and returns the whole number part — but specifically by rounding toward negative infinity, not simply "toward zero" or "down" in the way you might assume.

print(10 // 3)   # 3 — straightforward with positive numbers

Here's the surprise: with negative numbers, floor division doesn't behave the way most beginners expect.

print(-10 // 3)   # -4, not -3

Mathematically, -10 / 3 is approximately -3.33. Floor division rounds down to the nearest whole number — meaning toward negative infinity — so it lands on -4, not -3. If you're expecting truncation toward zero (the way int() behaves), this will catch you off guard the first time you hit it.

Modulo (%)

The % operator returns the remainder of a division:

print(10 % 3)    # 1  — 10 divided by 3 is 3 remainder 1
print(7 % 2)     # 1  — 7 is odd
print(8 % 2)     # 0  — 8 is even

That last example points to modulo's most common practical use: checking whether a number is even or odd.

number = 15
if number % 2 == 0:
    print("Even")
else:
    print("Odd")

It's also useful for wrapping values within a range — for example, cycling through a fixed set of options, or converting a total number of seconds into minutes and leftover seconds.

Exponentiation and Operator Precedence

The python exponent operator

** raises a number to a power:

print(2 ** 3)    # 8   — 2 to the power of 3
print(5 ** 2)    # 25  — 5 squared
print(9 ** 0.5)  # 3.0 — fractional exponents work too, this is a square root

A common syntax mistake: writing 2 * * 3 with a space between the asterisks. Python won't understand this as exponentiation — it needs to be written as a single, unspaced ** token.

Operator precedence (PEMDAS in Python)

When an expression combines multiple operators, Python evaluates them in a specific order, closely mirroring the PEMDAS rule taught in math class:

  1. Parentheses — evaluated first, always

  2. Exponents (**)

  3. Multiplication, division, floor division, modulo (*, /, //, %) — evaluated left to right

  4. Addition, subtraction (+, -) — evaluated left to right

Walking through an example
result = 2 + 3 * 4 ** 2 - 6 / 2

Step by step:

  1. 4 ** 2 → 16 (exponent first)

  2. 3 * 16 → 48 (multiplication)

  3. 6 / 2 → 3.0 (division)

  4. 2 + 48 - 3.0 → 47.0 (addition/subtraction, left to right)

print(result)   # 47.0

When precedence gets hard to track mentally, parentheses are your friend — grouping parts of an expression explicitly makes the intended order obvious to anyone reading the code, including future you.

Common Arithmetic Errors and How to Avoid Them

TypeError from incompatible types

Covered above, but worth restating as a general rule: combining a string and a number with an arithmetic operator (outside of the specific +/* string behaviors) raises a TypeError. The fix is always the same — convert explicitly with int(), float(), or str() as appropriate before the operation.

ZeroDivisionError

Dividing by zero — with either / or // — raises an error rather than returning something like infinity:

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

If there's any chance a divisor could legitimately be zero (a user-supplied value, a calculated denominator that might come out to zero), guard against it explicitly:

numerator = 10
denominator = 0

if denominator != 0:
    print(numerator / denominator)
else:
    print("Cannot divide by zero")
Real-world example: calculating a discounted price

Putting several of these operators together in a practical scenario:

original_price = 80.00
discount_percent = 25

discount_amount = original_price * (discount_percent / 100)
final_price = original_price - discount_amount

print(f"You save: ")
print(f"Final price: ")
# You save: $20.00
# Final price: $60.00

This small example touches division (converting a percentage to a decimal), multiplication (calculating the discount), and subtraction (applying it) — a fairly typical mix of what everyday arithmetic in Python actually looks like once you move past isolated examples.

PREVIOUSNEXT LESSON