Definition
What is 2 + 3 × 4 to Python: 20 or 14?
An operator is a symbol that performs an operation on values. An expression combines values, variables and operators into something Python can evaluate to a single value. Python has arithmetic, assignment, comparison and logical operators, evaluated in a fixed order of precedence.
In simple words
Python follows the same order as school maths: brackets first, then powers, then multiply and divide, then add and subtract. So 2 + 3 * 4 is 14. Comparisons such as 5 > 3 give True or False, and 'and', 'or' and 'not' combine those answers.
Overview
- An operator is a symbol that performs an operation on values, called operands.
- An expression combines values, variables and operators into something Python can evaluate to one value.
- Precedence decides the order: brackets, then **, then * / // %, then + -, then comparisons, then not, and, or.
- Most operators are left-to-right, but ** is right-to-left: 2 ** 3 ** 2 is 2 ** 9 = 512.
- and and or short-circuit: Python stops as soon as the answer is known.
- Using brackets, even when not needed, makes expressions easier to read and check.
Why it matters
Every calculation in a program, from a shopping bill to a model's error, is an expression. Getting the order right avoids silent bugs where the program runs but gives the wrong number.
Key terms
- Operator
- A symbol that performs an operation, such as + or ==.
- Operand
- A value an operator works on; in 3 + 4, the operands are 3 and 4.
- Expression
- A combination of values and operators that evaluates to one value.
- Precedence
- The order in which operators are applied, such as * before +.
- Floor division
- // divides and rounds down to a whole number.
- Modulus
- % gives the remainder after division.
Types
Python groups its operators into seven families. Each family works on a particular kind of question: calculating, comparing, combining conditions, storing, checking membership, checking identity or working with bits.
1. Arithmetic operators
+, -, *, /, // (floor division), % (remainder) and ** (power).
- How it works: / always returns a float; // rounds down to a whole number; % gives what is left over; ** raises to a power.
- Example: 17 / 5 = 3.4, 17 // 5 = 3, 17 % 5 = 2, 2 ** 10 = 1024.
- When to use it: Use them for every calculation; % is also handy to test even numbers (n % 2 == 0).
2. Comparison (relational) operators
==, !=, <, >, <= and >=, which compare two values and give True or False.
- How it works: Numbers compare by size and strings compare alphabetically by character codes; comparisons can be chained like 0 <= x <= 100.
- Example: marks >= 33 is True for 45; "apple" < "banana" is True.
- When to use it: Use them in if-conditions and loop tests; never use = (assignment) where you mean ==.
3. Logical operators
and, or and not, which combine or reverse True/False values.
- How it works: and is True only if both sides are True; or is True if at least one is; not flips a value; evaluation short-circuits.
- Example: age >= 18 and has_id; is_holiday or is_sunday; not is_raining.
- When to use it: Use them to build conditions with several parts, such as eligibility rules.
4. Assignment operators
= and the shortcut forms +=, -=, *=, /=, //=, %= and **=.
- How it works: x += 5 means x = x + 5; the walrus operator := (Python 3.8+) assigns inside an expression.
- Example: total += mark inside a loop adds each mark to the total.
- When to use it: Use the shortcut forms for counters and accumulators; they are shorter and clearer.
5. Membership operators
in and not in, which test whether a value is inside a collection or string.
- How it works: Python searches the sequence (or uses a fast hash lookup for sets and dict keys) and returns True or False.
- Example: "a" in "Delhi" is False; 7 in [1, 7, 9] is True; "Pune" not in cities.
- When to use it: Use them to check menu choices, search lists and test whether a key exists in a dictionary.
6. Identity operators
is and is not, which test whether two names refer to the very same object in memory.
- How it works: Unlike ==, which compares values, is compares identity (the object's id).
- Example: a = [1, 2]; b = [1, 2]; a == b is True but a is b is False; x is None checks for None.
- When to use it: Use is mainly to test for None, True or False singletons; use == for comparing values.
7. Bitwise operators
&, |, ^, ~, << and >>, which work on the individual bits of integers.
- How it works: Each bit is combined separately: & is AND, | is OR, ^ is XOR, ~ flips bits, << and >> shift bits left or right.
- Example: 6 & 3 is 2 (110 & 011 = 010); 1 << 4 is 16.
- When to use it: Use them in low-level work such as flags, permissions, image pixels and fast multiplication by powers of two.
Explained in detail
Arithmetic beyond + and -
Python has seven arithmetic operators. Besides +, - and *, there are three kinds of division and a power operator. / is true division and always gives a float: 7 / 2 is 3.5. // is floor division, which rounds down: 7 // 2 is 3, and, surprisingly for many learners, -7 // 2 is -4, because rounding down moves towards minus infinity. % gives the remainder: 7 % 2 is 1. ** raises to a power: 2 ** 10 is 1024. Together, // and % split a quantity into whole units and what is left over, such as turning 135 minutes into 2 hours and 15 minutes with 135 // 60 and 135 % 60.
Useful tricks with %
- n % 2 == 0 tests whether n is even.
- year % 4 == 0 is part of the leap-year rule.
- i % 7 cycles through 0 to 6, useful for days of the week.
- n % 10 gives the last digit of a whole number.
Comparison and logic
Comparison operators, ==, !=, <, >, <= and >=, produce True or False. Python allows chained comparisons that read like maths: 0 <= mark <= 100 checks both limits at once. Logical operators combine conditions: and is True only if both sides are True, or is True if at least one is, and not reverses a value. Python evaluates and and or lazily, stopping as soon as the answer is known. So in x != 0 and 10 / x > 2, the division never runs when x is 0, which avoids a ZeroDivisionError.
Precedence
| Priority | Operators | Example |
|---|---|---|
| Highest | ( ) brackets | (2 + 3) * 4 is 20 |
| ** power (right to left) | 2 ** 3 ** 2 is 2 ** 9, which is 512 | |
| * / // % | 2 + 3 * 4 is 14 | |
| + - | 10 - 4 - 3 is 3 (left to right) | |
| == != < > <= >= | 1 + 1 == 2 is True | |
| Lowest | not, then and, then or | True or False and False is True |
You do not need to memorise every level. The safe habit is to add brackets whenever an expression could be misread. Brackets cost nothing and make intent obvious to readers, which is why style guides recommend them even when Python would get the order right anyway.
How it works
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + - * | Add, subtract, multiply | 6 * 7 | 42 |
| / | Divide (always a float) | 7 / 2 | 3.5 |
| // | Whole-number division | 7 // 2 | 3 |
| % | Remainder | 7 % 2 | 1 |
| ** | Power | 2 ** 3 | 8 |
| == != < > | Compare | 5 > 3 | True |
| and or not | Combine True/False | True and False | False |
In code
A bill with quantity, discount and a free-delivery check.
price = 120
quantity = 3
discount_percent = 10
subtotal = price * quantity
discount = subtotal * discount_percent / 100
total = subtotal - discount
print("Subtotal:", subtotal)
print("Discount:", discount)
print("Total:", total)
free_delivery = total >= 300 and quantity >= 2
print("Free delivery?", free_delivery)
print("2 + 3 * 4 =", 2 + 3 * 4)
print("(2 + 3) * 4 =", (2 + 3) * 4)
print("17 // 5 =", 17 // 5, "and 17 % 5 =", 17 % 5)Output
Subtotal: 360 Discount: 36.0 Total: 324.0 Free delivery? True 2 + 3 * 4 = 14 (2 + 3) * 4 = 20 17 // 5 = 3 and 17 % 5 = 2
Try it yourself
Predict the result
Work out each expression in Python's order, then match it to its value.
- 2 + 3 * 4
- (2 + 3) * 4
- 10 // 3
- 10 % 3
- 2 ** 4
- 5 > 3 and 2 > 4
Common mistakes
- Using = when you mean ==.
- Forgetting that * happens before +. Use brackets when in doubt.
- Confusing / and //: 7 / 2 is 3.5 but 7 // 2 is 3.
Check yourself
Summary
- Expressions combine values with operators into one value.
- Precedence: brackets, powers, * / // %, then + -.
- Comparisons give True or False; and, or, not combine them.
Finished reading?
Sources: Python docs: Operator precedence