Operators
Learn how to perform calculations, compare values, and combine conditions using Python's operators.
Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn
- • Perform math with arithmetic operators
- • Compare values with comparison operators
- • Combine conditions with logical operators
- • Use assignment operator shortcuts
- • Understand operator precedence (order)
- • Check membership and identity
1 What Are Operators?
Operators are special symbols that tell Python to perform specific operations on values. Think of them as action words that do something with your data.
Here, + is the operator , and 10 and 5 are the operands (the values being operated on).
Python has several types of operators. We'll cover them in order of how often you'll use them.
2 Arithmetic Operators (Math)
These are the operators you'll use most often. They perform mathematical calculations.
Operator
Name
Example
Result
When to Use
+
Addition
10 + 3
13
Add numbers together
-
Subtraction
10 - 3
7
Find differences
*
Multiplication
10 * 3
30
Multiply values
/
Division
10 / 3
3.333...
Divide (returns decimal)
//
Floor Division
10 // 3
3
Divide (whole number only)
%
Modulus
10 % 3
1
Get remainder after division
**
Exponent
2 ** 3
8
Raise to a power (2³)
- / always gives a decimal: 10 / 3 = 3.333...
- // rounds down to whole number: 10 // 3 = 3
↗ Arithmetic in Real Programs
Arithmetic operators aren't just for textbook maths — they solve real problems constantly. Here are the patterns you'll reach for again and again.
Modulo (%) — the most underrated operator
Modulo is used constantly: pagination, cyclical patterns, divisibility checks, time conversion. Once you recognise it, you'll see it everywhere.
Floor Division (//) — whole-number results
// and % are a natural pair — they give you the quotient and the remainder from the same division.
Exponent (**) — real-world uses
3 Comparison Operators
Comparison operators compare two values and return True or False . You'll use these constantly with if statements.
Meaning
==
Equal to
5 == 5
True
!=
Not equal to
5 != 3
>
Greater than
5 > 3
<
Less than
5 < 3
False
>=
Greater than or equal
5 >= 5
<=
Less than or equal
5 <= 3
↗ Comparing Strings and Chaining Comparisons
Comparison operators aren't just for numbers. Understanding how they work with strings — and how to chain them — makes your code far more expressive.
Strings compare alphabetically (lexicographically)
Python compares strings character by character using their Unicode value. Capital letters (A–Z) have lower values than lowercase (a–z), so "Apple" < "apple" is True. This matters for sorting names or usernames.
Chained comparisons — Python's elegant shortcut
Most languages require you to write two separate conditions. Python lets you chain them like a maths expression:
⚠️ Never compare different types with < or >
Equality ( == ) across types is allowed and simply returns False. But ordering comparisons ( < , > ) between incompatible types crash your program. This usually happens when input() returns a string and you forget to convert it with int() .
4 Logical Operators
Logical operators let you combine multiple conditions. They're essential for complex decision-making.
A theme park ride might require: age >= 12 and height >= 140 Both conditions must be true to ride!
↗ Logical Operators: Truth Tables and Short-Circuit Evaluation
To use logical operators with confidence, you need to know every possible outcome — and understand a powerful performance trick Python uses called short-circuit evaluation .
Truth tables — every possible combination
A
B
A and B
A or B
not A
Summary: and needs ALL conditions True. or needs ANY condition True. not flips the result.
Short-circuit evaluation — Python stops early
Python is lazy in a clever way: it stops evaluating as soon as the result is certain.
This isn't just trivia — it's a common technique to prevent crashes. Check that a value is safe before using it, using and .
Combining and / or / not — operator priority
When mixing logical operators, priority order is: not → and → or . Use parentheses to be explicit:
5 Assignment Operators
Assignment operators are shortcuts for updating a variable's value. They combine an operation with assignment.
Shortcut
Same As
Example (x = 10)
+=
x = x + n
x += 5
15
-=
x = x - n
x -= 3
*=
x = x * n
x *= 2
20
/=
x = x / n
x /= 2
5.0
//=
x = x // n
x //= 3
%=
x = x % n
x %= 3
**=
x = x ** n
x **= 2
100
↗ Augmented Assignment: Real Patterns You'll Use Every Day
The shorthand operators aren't just about typing less — they represent three fundamental programming patterns that appear in virtually every program ever written.
Pattern 1: The Counter
Incrementing a number by 1 each time something happens is called a counter . It's one of the most common patterns in programming:
Pattern 2: The Accumulator
Adding values to a running total is called an accumulator . This is how shopping carts, bank balances, and totals work:
Pattern 3: String Building
+= works on strings too — it appends text to an existing string:
In large programs, f-strings are usually better for complex string construction — but += is handy when you're building a string incrementally over time.
6 Operator Precedence (Order of Operations)
Just like in math, Python follows a specific order when evaluating expressions with multiple operators. Remember PEMDAS !
PEMDAS - Order of Operations
Parentheses make your code clearer and ensure operations happen in the order you expect. (2 + 3) * 4 is clearer than 2 + 3 * 4
↗ Building Complex Expressions and the Ternary Operator
Operators become most powerful when combined. Here's how to read and write complex expressions confidently — plus a cleaner way to write simple if/else logic.
Reading complex expressions step by step
When you see a long expression, break it down by precedence:
The ternary operator — if/else in one line
Python has a compact syntax for simple if/else decisions called a conditional expression (or ternary operator):
7 Identity and Membership Operators
These operators check relationships between objects and values.
Identity Operators
Check if two variables refer to the same object
Membership Operators
↗ Membership Operators: Practical Patterns
in and not in are more versatile than they first appear. Here's how they're used in real code.
Searching inside strings
Validating against a list of allowed values
This pattern is everywhere: checking file extensions, validating user choices, filtering allowed commands, verifying roles and permissions.
! Common Operator Mistakes (And How to Fix Them)
These mistakes catch nearly every beginner at least once. Recognise them now and you'll debug much faster.
Python actually prevents this in conditions and raises a SyntaxError. In some other languages it silently does the wrong thing — Python protects you here.
Technically valid Python, but treat booleans as booleans and numbers as numbers. Don't mix them intentionally.
Always use round() or a tolerance when comparing floats for equality. This trips up developers at every level.
★ Mini-Project: Password Strength Checker
Let's combine every operator type from this lesson into one real program. This is exactly the kind of validation logic used in real login systems.
Operators used in this project
Can you add a check that the password doesn't contain the word "password"? Hint: use not in . What about checking it doesn't start with a digit? Hint: use password[0] in DIGITS .
8 Summary - Quick Reference
Arithmetic
Comparison
Logical
Assignment
Identity
Membership
Great Job!
You've learned all the essential Python operators. These are the building blocks for writing expressions and making decisions in your programs. In the next lesson, you'll learn about control flow and how to use these operators with if statements!
Lesson 3 done — you can now make Python calculate anything!
Arithmetic, comparison, logical, assignment, identity, and membership operators — you've got them all. These are the building blocks of every expression in every Python program.
🚀 Up next: Control Flow — use those operators with if/elif/else to make programs that make decisions.
Practice quiz
What does 10 % 3 evaluate to?
- 3
- 1
- 3.33
- 0
Answer: 1. % is modulus (remainder); 10 divided by 3 leaves remainder 1.
What is the result of 2 ** 3?
- 6
- 9
- 8
- 5
Answer: 8. ** is the exponent operator; 2 to the power of 3 is 8.
What does 2 + 3 * 4 evaluate to?
- 20
- 14
- 24
- 9
Answer: 14. By PEMDAS, multiplication happens first: 3 * 4 = 12, then 2 + 12 = 14.
Which operator checks if two values are EQUAL?
- =
- ==
- !=
- is
Answer: ==. == compares for equality; a single = assigns a value.
What does 'banana' in ['apple', 'banana', 'cherry'] return?
- True
- False
- 1
- Error
Answer: True. The membership operator 'in' returns True when the value is found.
Which logical operator requires BOTH conditions to be True?
- or
- not
- and
- in
Answer: and. 'and' is True only when both operands are True.
What does 2 * 3 ** 2 evaluate to?
- 36
- 18
- 12
- 64
Answer: 18. Exponents bind before multiplication: 3 ** 2 = 9, then 2 * 9 = 18.
What is the result of not (5 >= 10)?
- True
- False
- Error
- None
Answer: True. 5 >= 10 is False, and 'not False' is True.
Using Python's ternary: status = 'adult' if age >= 18 else 'minor'. If age is 20, status is?
- 'minor'
- 'adult'
- True
- Error
Answer: 'adult'. Since 20 >= 18 is True, the value before 'if' ('adult') is chosen.
What does the expression 1 < '1' do in Python?
- Returns True
- Returns False
- Raises a TypeError
- Returns 0
Answer: Raises a TypeError. Ordering comparisons (<, >) between an int and a str raise a TypeError.
Continue this course
- Previous: Variables and Data Types
- Next: Control Flow - If Statements