Python Operators

Learn Python operators with simple explanations, including arithmetic, comparison, assignment, compound assignment, logical, bitwise, membership, identity, and operator precedence.

Published

Updated

Read time 7 min read

Reviewed byDeepak Prasad

Python Operators

Operators are symbols and keywords that combine values and variables: arithmetic for math, comparisons for ordering, logical keywords for conditions, assignment and compound assignment for updating names, bitwise ops for integer bits, and in / is for membership and identity. Precedence decides evaluation order when you do not add parentheses. This guide groups them in tables you can skim, then calls out //, precedence, and common mistakes. For string concatenation with +, see concatenate strings in Python; for combining tests in control flow, see if / else.

Tested on: Python 3.13.3; kernel 6.14.0-37-generic; Ubuntu 25.04.


Python operators list

Operator type Used for
Arithmetic Math calculations
Assignment Bind names to values
Compound assignment Update a variable and rebind in one step
Comparison Compare two values (True / False)
Logical Combine conditions (and, or, not)
Bitwise Integer operations on bit patterns
Membership Test presence in a sequence or mapping
Identity Test whether two references are the same object

Arithmetic operators in Python

Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division (true division; result is float in Python 3)
// Floor division
% Modulus (remainder)
** Exponentiation (see pow() examples)
python
a, b = 5, 2
print(a + b, a - b, a * b, a / b, a // b, a % b, a**b)
Output

This prints 7 3 10 2.5 2 1 25. Beyond numbers, + can concatenate sequences and * can repeat them—same idea as in the string tutorial linked above. Operand types and promotion rules are covered in Python numbers.


What does // mean in Python?

// is floor division: it divides and rounds the result toward negative infinity. It differs from /, which always performs true division and returns a floating-point value in Python 3.

Operator Meaning Typical result shape
/ True division float
// Floor division int if both operands are int, otherwise float
python
print(7 / 2, 7 // 2)
print(-7 // 2)
Output

You should see 3.5 and 3, then -4 (floor toward negative infinity, not “truncate toward zero” for negatives).


Comparison operators in Python

Comparisons evaluate to True or False. They appear in if, elif, while, comprehensions, and validation.

Operator Meaning
== Equal value
!= Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
python
print(5 == 5, 5 != 3, 9 > 4, 3 < 7)
Output

Assignment operators in Python

Operator Meaning
= Bind a name to a value
:= Walrus: assign inside an expression

Use = for normal binding. The walrus := assigns while producing a value (for example if (n := len(items)) > 10:). Learn = well first; add := only where it clearly removes duplication.


Compound assignment operators in Python

Compound operators update the left-hand side using the current value and an operand on the right.

Operator Same idea as
+= x = x + y
-= x = x - y
*= x = x * y
/= x = x / y
//= x = x // y
%= x = x % y
**= x = x ** y
&= x = x & y
^= x = x ^ y
<<= x = x << y
>>= x = x >> y

Bitwise OR uses the same pattern with a pipe character before the equals sign (OR-then-assign); write it in code as you would in a script.

For mutable targets such as lists, += can mutate in place rather than rebind; treat compound assignment as “update the object” when the target is mutable.


Assignment vs compound assignment

Feature = assignment Compound assignment
Purpose Store a value Update using current value
Typical use First binding Counters, accumulators, flags
Readability Clearest for new values Compact for repeated updates

Logical operators in Python

Operator Meaning
and True if both sides are true
or True if at least one side is true
not Inverts truthiness

and / or short-circuit: the right side may not run. They return one of the operands, not always a strict boolean—for example 0 or 42 yields 42. For “all / any” over iterables, see Python any() and all().


Bitwise operators in Python

Bitwise operators act on integer bits (two’s complement). Common uses: permission flags, masks, packed binary fields, and protocol work.

Operator Meaning
& Bitwise AND
&#124; Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
<< Left shift
>> Right shift

They are not interchangeable with and / or, which work on truthiness, not bit patterns.


Membership operators in Python

Operator Meaning
in Value is present
not in Value is absent

Works with strings (substrings), lists, tuples, sets, dict keys, and other collections that define containment.

python
print(3 in [1, 2, 3])
print("py" in "python")
print("x" in {"x": 1})
Output

Identity operators in Python

Operator Meaning
is Same object
is not Different objects

== compares values; is compares identity. For None, always write x is None (see Python None).

python
a = [1]
b = [1]
print(a == b, a is b)
Output

This prints True False: equal contents, different list objects.


Python operators by use case

Use case Operator type
Total or math Arithmetic
Compare scores or limits Comparison
Combine several tests Logical
Bump a counter Compound assignment
Item in a collection Membership
Sentinel None Identity (is None)
Flag bits Bitwise
Clarify order Parentheses

Python operator precedence

Precedence decides which sub-expressions run first. Parentheses override everything. Rough order: calls, indexing, attributes; then **; unary + - ~; then * / // %; then + -; then shifts; then bitwise & ^ |; then comparisons and membership/identity; then not, and, or; assignment forms are last.

Priority (high → low) Operators / forms
Highest Parentheses (), indexing [], calls (), attributes .
High **
High Unary +, -, ~
Medium *, /, //, %
Medium +, - (binary)
Medium <<, >>
Medium &, ^, &#124; (bitwise)
Low Comparisons, in, is
Low not
Low and
Low or
Lowest Conditional expression, =, walrus, compound assigns

For the authoritative ordering, see the Python language reference on operator precedence.


Common operator mistakes in Python

  • Mixing up / (true division) and // (floor division), especially with negative operands.
  • Using is for value equality; use == unless you mean identity (except None, where is None is correct).
  • Relying on precedence in long expressions—add parentheses when readers would pause.
  • Chaining and and or without parentheses; intent becomes ambiguous.
  • Assuming += on a list always creates a new list—it often mutates the existing list in place.
  • Using & or | where you meant and or or.

Best practices for using Python operators

  • Prefer parentheses for non-obvious mixes of arithmetic, comparison, and logical operators.
  • Use is None / is not None for None checks; use in for membership.
  • Use compound assignment for simple counters and accumulators; split complex logic into named intermediates.
  • Avoid cramming many different operators into one line—readability beats cleverness.

Python operators quick reference

Task Operator
Add +
Subtract -
Multiply *
Divide /
Floor divide //
Remainder %
Power **
Assign =
Add and assign +=
Equal value ==
Same object is
Membership in
Combine conditions and, or, not
Bitwise &, ^, ~, <<, >>, plus bitwise OR

Summary

  • Python operators cover arithmetic, comparison, logical, assignment, compound assignment, bitwise, membership, and identity.
  • / is true division; // is floor division—watch negatives.
  • Comparisons return booleans; logical operators short-circuit and may return operands.
  • Compound assignment updates in one step; on mutables it can mutate in place.
  • in / not in test containment; is tests identity—use is None, not == None.
  • Precedence controls evaluation order; use parentheses and the official table when in doubt.

References


Frequently Asked Questions

1. What is the difference between / and // in Python?

The / operator does true division and returns a float in Python 3; // is floor division and returns the floor of the quotient, which may be int or float depending on the operands.

2. When should I use is instead of == in Python?

Use is or is not for identity checks such as x is None; use == for value equality—custom classes can override == but not identity.

3. What is the walrus operator := in Python?

The walrus operator assigns a name inside an expression, for example while (line := file.readline()); learn normal = first, then use := where it clearly reduces repetition.

4. Do & and | mean the same as and and or?

No—& and | are bitwise operators on integers; and and or are logical operators on truthiness and short-circuit differently.
Bashir Alam

Data Analyst and Machine Learning Engineer

Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in OCR, text extraction, data preprocessing, and …