Operators
Learn how to perform calculations, compare values, and combine conditions using Python's operators.
Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.
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.
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.
↗ 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 is used constantly: pagination, cyclical patterns, divisibility checks, time conversion. Once you recognise it, you'll see it everywhere.
// and % are a natural pair — they give you the quotient and the remainder from the same division.
3 Comparison Operators
Comparison operators compare two values and return True or False . You'll use these constantly with if statements.
↗ 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.
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.
Most languages require you to write two separate conditions. Python lets you chain them like a maths expression:
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 .
Summary: and needs ALL conditions True. or needs ANY condition True. not flips the result.
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 .
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.
↗ 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.
Incrementing a number by 1 each time something happens is called a counter . It's one of the most common patterns in programming:
Adding values to a running total is called an accumulator . This is how shopping carts, bank balances, and totals work:
+= 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.
When you see a long expression, break it down by precedence:
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.
Check if two variables refer to the same object
↗ Membership Operators: Practical Patterns
in and not in are more versatile than they first appear. Here's how they're used in real code.
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.
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
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!
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.