Python Operators with Examples: Arithmetic, Comparison, Logical, Bitwise and More

Python Operators with Examples – All 7 operator types explained

Quick answer: Python has seven types of operators: arithmetic (+, −, *, /, //, %, **), comparison (==, !=, >, <, >=, <=), logical (and, or, not), assignment (=, +=, −=, *=, etc.), bitwise (&, |, ^, ~, <<, >>), membership (in, not in) and identity (is, is not). Python 3.8+ also has the walrus operator (:=) for assignment inside expressions.

All code examples were run on Python 3.13; the outputs shown are real.

Contents

Arithmetic Operators

OperatorMeaning
+Addition
−Subtraction
*Multiplication
/Division (always float)
//Floor division
%Modulus (remainder)
**Power
a, b = 17, 5
print(a + b, a - b, a * b, a / b, a // b, a % b, a ** 2)

Output:

22 12 85 3.4 3 2 289

Comparison (Relational) Operators

They compare values and return True or False.

x, y = 10, 20
print(x == y, x != y, x > y, x < y, x >= 10, y <= 15)

Output:

False True False True True False

Logical Operators

and is True only if both conditions are True; or is True if at least one is True; not reverses the result.

temp, pressure = 85, 2.5
print(temp > 80 and pressure < 3)
print(temp > 90 or pressure > 2)
print(not temp > 80)

Output:

True
True
False

Assignment Operators

Shorthand operators update a variable in place.

n = 10
n += 5
n *= 2
n -= 4
n //= 3
print(n)

Output:

8

Step by step: 10 + 5 = 15, × 2 = 30, − 4 = 26, // 3 = 8.

Bitwise Operators

They work on the binary form of integers: & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift) and >> (right shift).

a, b = 6, 3     # 110 and 011 in binary
print(a & b, a | b, a ^ b, ~a, a << 1, a >> 1)

Output:

2 7 5 -7 12 3

Membership and Identity Operators

in checks whether a value is in a sequence; is checks whether two names point to the same object.

parts = ["bolt", "nut", "gear"]
print("nut" in parts, "shaft" not in parts)
a = [1, 2]
b = a
c = [1, 2]
print(a is b, a is c, a == c)

Output:

True True
True False True

Operator Precedence

From highest to lowest: ** → unary −, +, ~ → *, /, //, % → +, − → shifts → & → ^ → | → comparisons, is, in → not → and → or. Use brackets to make intent clear.

print(2 + 3 * 4, (2 + 3) * 4, 2 ** 3 ** 2, -3 ** 2, 10 - 4 - 3)

Output:

14 20 512 -9 3

Note: ** is right-associative (2 ** 3 ** 2 = 2 ** 9), and -3 ** 2 is −(3 ** 2) = −9.

Walrus Operator (:=)

Assigns a value inside an expression (Python 3.8+):

readings = [12, 45, 7, 60]
if (n := len(readings)) > 3:
    print("Too many readings:", n)

Output:

Too many readings: 4

Frequently Asked Questions

How many types of operators are there in Python?

Seven: arithmetic, comparison, logical, assignment, bitwise, membership and identity operators. The walrus operator (:=) is an additional assignment expression.

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

/ performs true division and always returns a float (7 / 2 = 3.5); // performs floor division and rounds down (7 // 2 = 3).

What is the difference between == and is?

== compares values; is checks whether two variables refer to the same object in memory.

What does % do in Python?

% returns the remainder of a division, for example 17 % 5 = 2. It is used to check even/odd numbers and divisibility.

Which operator has the highest precedence in Python?

The exponent operator ** has the highest precedence among arithmetic operators, and it is evaluated right to left.

Related Python Guides

Learn Python With Guidance

Join our Python course or message us on WhatsApp for doubt-clearing and project support.

Scroll to Top