Python Operators

The `+` in `8 + 3` adds, while the same symbol in `”py” + “thon”` joins text. I like that one familiar symbol can do both jobs, depending on what it acts on.

Once you can name the operation, choosing a symbol becomes less of a guess. Start with what an operator does in a Python expression.

What a Python operator does

An operator performs an operation on its operands, so the plus sign in 8 + 3 acts on the values 8 and 3.

ExpressionOperatorResult
8 + 3+11
8 > 3>True
“py” in “python”inTrue

Python operators can compare objects, test membership, assign values, combine conditions, and calculate numbers, so choose one by the result you need.

Choose the operator by the result you need

Operands can be literals, variables, or the result of another expression. Their types matter because the same symbol can perform different operations on different types, such as adding numbers or joining strings.

  • Use arithmetic operators when you need a numeric result.
  • Use comparison operators when you need a truth value about two values.
  • Use and, or, and not to combine or invert truth tests.
  • Use in for containment and is for object identity.

The equals sign assigns a value, while the double equals sign compares values. For example, count = 4 binds a name and count == 4 asks whether its current value equals four.

Python operator families in working expressions

The examples use Python 3.14.7 and show the value each expression produces. I ran the arithmetic, comparison, Boolean, precedence, and identity examples in Python 3.14.7. An expression can return a value other than True or False.

Arithmetic operators calculate with numbers

Use +, -, *, /, //, %, and ** for addition, subtraction, multiplication, true division, floor division, remainder, and exponentiation. Division with / returns a float, while // rounds the quotient down toward negative infinity.

print(7 / 2)
print(7 // 2)
print(-7 // 2)
print(7 % -3)
print(2 ** 3)

Python floors division toward negative infinity, and modulo’s remainder follows the divisor’s sign, so test negative operands when a boundary matters.

Comparison operators test relationships

The operators ==, !=, <, <=, >, and >= compare values and return True or False, while 0 < score < 10 checks both bounds and evaluates score only once.

score = 7
print(0 < score < 10)
print(score == 7)

Logical operators can return an operand

and and or short-circuit, which means Python may skip the right operand once the result is determined. They return one of their operands, not a normalized Boolean, so 0 and an expression returns 0 without evaluating the expression.

value = 0
print(value and 10 / value)
print(value or 42)
print(not value)

Short-circuiting can guard an operation that would fail, but use an explicit condition when zero is valid data rather than a fallback signal.

Python operator expression output showing short-circuiting and floor division

Bitwise operators act on integer bits

Bitwise &, |, ^, ~, <<, and >> combine or shift integer bits. They are useful for masks and packed flags, not as substitutes for and and or, which test truth values.

permissions = 0b1101
read_flag = 0b0100
print(permissions & read_flag)
print(permissions | 0b0010)
print(permissions >> 1)

The first expression keeps only the bits present in both integers, while the second sets any bit present in either value. Python integers have unlimited precision, so bitwise operations follow integer values rather than a fixed-width machine word.

Assignment, identity, and membership answer different questions

Augmented assignment such as total += amount updates a target using an operation. The is operator checks whether two references point to the same object, and in checks whether a value belongs to a container.

first = [1, 2]
second = [1, 2]
alias = first
print(first == second)
print(first is second)
print(first is alias)
print("name" in {"name": "Ada"})

The output is True, False, True, and True.

Equal lists can be separate objects, so use == for value comparison and reserve is for identity checks. Use is None to test for None, and remember that dictionary membership checks keys, not values.

Python’s operator module exposes callable forms such as add, eq, and itemgetter. Use one when an API expects a function, such as a sort key. An ordinary expression is clearer when you are writing the operation directly.

Where Python expressions surprise you

Precedence determines how Python groups operators when parentheses are absent. Operators at a higher level bind more tightly, and operators on the same level generally group left to right, with exponentiation grouping right to left.

ExpressionGrouping or result
2 + 3 * 414, multiplication first
(2 + 3) * 420, parentheses first
2 ** 3 ** 2512, grouped as 2 ** (3 ** 2)
not a == bnot (a == b)

For assignment versus comparison, keep = and == distinct. For object comparison, use is only when identity is the question.

Floor division and modulo satisfy a == (a // b) * b + (a % b). The remainder has the divisor’s sign.

If an expression needs a comment to explain its grouping, add parentheses instead. Python’s precedence table is the reference for less familiar combinations.

Parentheses make intent visible

Parentheses document the grouping you intend, even when Python’s precedence rules already determine the same result. In a condition, they make it clear whether a comparison belongs with an and or an or.

age = 20
has_ticket = True
allowed = (age >= 18) and has_ticket
print(allowed)

The result is True because both comparisons produce truthy values. Change either value and run it again. Parentheses show how the condition is grouped, which makes the code clearer to review than relying on memorized precedence.

FAQ

These answers cover the operator questions that need a short clarification beyond the examples above.

What does the // operator do in Python?

The // operator performs floor division. It rounds the quotient down toward negative infinity, so -7 // 2 evaluates to -4.

What does the modulo operator do in Python?

The % operator returns the remainder from floor division. The remainder has the sign of the divisor, so 7 % -3 evaluates to -2.

What is the difference between == and is?

== compares values for equality. is checks whether both references point to the same object. Use is None to test for None, and use == for value comparisons.

What does the and operator do in Python?

and evaluates operands from left to right and stops when the result is determined. It returns one of its operands, so a falsy left operand such as 0 is returned without evaluating the right side.

What does operator.add do?

operator.add(a, b) is the callable form of a + b. Use it when a function is needed as an argument, such as a key function or callback.

Pankaj Kumar
Pankaj Kumar

I have been working on Python programming for more than 12 years. At AskPython, I share my learning on Python with other fellow developers.

Articles: 256