flowchart TB
A["Python Operators"] --> B["Assignment<br/>= += -= *= /="]
A --> C["Arithmetic<br/>+ - * / % ** //"]
A --> D["Comparison<br/>== != > < >= <="]
A --> E["Logical<br/>and / or / not"]
A --> F["Identity<br/>is / is not"]
A --> G["Membership<br/>in / not in"]
46 Operators
What This Chapter Covers
Operators are the verbs of Python — they describe what to do with values. By the end of this chapter you will be able to:
- Assign, update and combine values using assignment and compound assignment operators.
- Perform calculations with arithmetic operators, including modulus, exponent and floor division.
- Compare values with comparison operators to produce Booleans.
- Build compound conditions with logical operators (
and,or,not). - Test object identity with identity operators (
is,is not) and membership with membership operators (in,not in). - Read expressions correctly using Python’s operator precedence rules.
46.1 A Map of Python Operators
The diagram below groups Python’s most common operators by purpose. Each category takes one or two operands and produces either a new value (arithmetic, assignment) or a Boolean (comparison, logical, identity, membership).
Operators are the building blocks of any programming language, and understanding how to use them effectively is crucial for writing efficient and readable code in Python.
Operators in Python are special symbols that perform arithmetic or logical computation. The value that the operator operates on is called the operand. A combination of operators, operands and variables is called an expression, and Python reduces every expression to a single value. Python operators can be classified into several categories, covered below.
46.2 Assignment Operators
Assignment operators are used to assign values to variables:
-
=Assigns a value to a variable. E.g.,x = 5 -
+=Adds right operand to the left operand and assign the result to left operand. E.g.,x += 5 -
-=Subtracts right operand from the left operand and assign the result to left operand. E.g.,x -= 5 -
*=and others similarly modify and assign.
46.3 Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.
-
+Addition: Adds two operands. E.g.,x + y -
-Subtraction: Subtracts the right operand from the left operand. E.g.,x - y -
*Multiplication: Multiplies two operands. E.g.,x * y -
/Division: Divides the left operand by the right operand. E.g.,x / y -
%Modulus: Returns the remainder when the left operand is divided by the right operand. E.g.,x % y -
**Exponent: Left operand raised to the power of right operand. E.g.,x ** y -
//Floor Division: Division that results into whole number adjusted to the left in the number line. E.g.,x // y
When Does / vs // Matter?
-
/is true division and always returns afloat—10 / 4gives2.5, and even10 / 2gives5.0. -
//is floor division and returns whatever type makes sense —10 // 4gives2(int), while10.0 // 4gives2.0(float).
In analytics, use // when you need whole buckets (e.g. “how many full weeks in 100 days?”) and % for the leftover (e.g. “and how many extra days?”).
46.4 Comparison Operators
Comparison operators are used to compare values. It either returns True or False according to the condition.
-
==Equal: True if both operands are equal. E.g.,x == y -
!=Not equal: True if operands are not equal. E.g.,x != y -
>Greater than: True if left operand is greater than the right operand. E.g.,x > y -
<Less than: True if left operand is less than the right operand. E.g.,x < y -
>=Greater than or equal to: True if left operand is greater than or equal to the right operand. E.g.,x >= y -
<=Less than or equal to: True if left operand is less than or equal to the right operand. E.g.,x <= y
Chained Comparisons
Python lets you chain comparisons the way mathematicians write them. Instead of x > 0 and x < 10, you can write 0 < x < 10 — Python evaluates each link and ANDs the results together. This is a readability win unique to Python.
46.5 Logical Operators
Logical operators are used to combine conditional statements:
-
andReturns True if both statements are true. E.g.,x < 5 and x < 10 -
orReturns True if one of the statements is true. E.g.,x < 5 or x < 4 -
notReverse the result, returns False if the result is true. E.g.,not(x < 5 and x < 10)
Python’s and and or short-circuit: they stop evaluating as soon as the result is known. This lets you write safe guards like x != 0 and y / x > 1 — the division is skipped when x is zero.
46.6 Identity Operators
Identity operators ask a subtly different question from equality: “is this the very same object in memory?” rather than “do these two things have the same value?”.
-
isReturns True if both operands refer to the same object. -
is notReturns True if they refer to different objects.
The most common use is testing against singletons like None, True and False — always use x is None, never x == None.
46.7 Membership Operators
Membership operators test whether a value appears inside a container like a list, tuple, set, string or dict (keys only).
-
inReturns True if the value is found in the sequence. -
not inReturns True if the value is not found in the sequence.
For large lookups, prefer sets or dict keys — membership tests on a set are much faster than on a list.
46.8 Operator Precedence
When an expression has multiple operators, Python evaluates them in a fixed order — highest precedence first. Knowing the order lets you read expressions without guessing; using parentheses lets you override it when in doubt.
| Precedence (high → low) | Operators |
|---|---|
| 1 |
** (exponent) |
| 2 |
*, /, //, %
|
| 3 |
+, -
|
| 4 | Comparisons: ==, !=, <, >, <=, >=
|
| 5 | not |
| 6 | and |
| 7 | or |
For example, 2 + 3 * 4 is 14, not 20, because * binds tighter than +. And True or False and False is True, because and binds tighter than or — it reads as True or (False and False).
Rule of thumb: when an expression mixes and with or, or arithmetic with comparisons, wrap each sub-expression in parentheses. Your future self (and your code reviewer) will thank you.
46.9 Common Pitfalls with Operators
-
Using
=instead of==→if x = 5:is a syntax error in Python. Use==to compare,=to assign. -
isvs==→ischecks identity,==checks value.x == Nonesometimes works butx is Noneis the only correct idiom. -
Integer vs float division →
/always returns a float, even for whole-number results. Use//if you need an integer. -
Mixing
andwithorwithout parentheses → Precedence rules are not obvious at a glance. Parenthesise. -
notbinds tightly →not x in lstis parsed as(not x) in lst, notnot (x in lst). Usex not in lstfor clarity. -
Short-circuit surprises →
a or breturnsaifais truthy, notTrue— useful, but easy to forget when chaining. -
Float equality →
0.1 + 0.2 == 0.3isFalse. Usemath.isclose()for real-number comparisons.
Summary
| Concept | Description |
|---|---|
| Foundations | |
| Operator | A symbol or keyword that performs a computation on one or more operands and produces a value |
| Operand | The value or variable an operator acts on, such as the 5 and 3 in 5 + 3 |
| Expression | A combination of values, variables and operators that Python evaluates to a single result |
| Assignment Operators | |
| Assignment = | The single equals sign binds a value on the right to a variable name on the left |
| Compound Assignment | Shortcuts like +=, -=, *=, /= combine an arithmetic operation with re-assignment in one step |
| Arithmetic Operators | |
| Arithmetic + - * / | Plus, minus, asterisk and slash perform addition, subtraction, multiplication and true division |
| Modulus % | x % y returns the remainder of dividing x by y, useful for detecting even or every-nth conditions |
| Exponent ** | x ** y raises x to the power y and works for both integer and floating-point exponents |
| Floor Division // | x // y divides and rounds the result down to the nearest whole number |
| Comparison Operators | |
| Equality == and != | Equal and not-equal return a Boolean by comparing values for identity of content |
| Ordering > < >= <= | Greater-than and less-than family return Booleans for ordered comparisons between numbers or strings |
| Chained Comparisons | Python lets you write 0 < x < 10 directly instead of 0 < x and x < 10 |
| Logical Operators | |
| Logical and | Returns True only when both combined conditions are True; short-circuits if the first is False |
| Logical or | Returns True when at least one combined condition is True; short-circuits if the first is True |
| Logical not | Inverts a Boolean, turning True into False and vice versa |
| Short-Circuit Evaluation | and / or stop evaluating as soon as the result is determined, enabling safe guards like x != 0 and y/x > 1 |
| Identity and Membership | |
| Identity is and is not | Tests whether two names refer to the exact same object in memory; the idiomatic way to check against None |
| Membership in and not in | Tests whether a value appears in a container such as a list, tuple, set, dict keys or a string |
| Writing Safe Expressions | |
| Operator Precedence | The defined order in which operators are evaluated; use parentheses whenever intent could be ambiguous |
| Common Pitfalls | Common traps include = vs ==, is vs ==, float equality, not binding tighter than in, and mixing and with or |