Menu

Python Operators: Arithmetic, Comparison, Logical, Membership, and Identity

A complete tour of Python operators — arithmetic, comparison, logical, assignment, membership, and identity — with examples of each.

Operators Are Just Symbols for Actions

An operator is a symbol (or short keyword) that tells Python to do something with one or two values — add them, compare them, check if one contains the other. You already know a few from basic math; Python has a handful more for logic, containment, and identity.

We'll walk the main categories one at a time.

Arithmetic Operators

The standard math operators, plus a couple of Python specialties:

main.py
Output
Click Run to see the output here.

Two pieces of this list are easy to forget:

  • / returns a float even when both sides are integers. 10 / 2 gives you 5.0. For integer division, use //.
  • % is modulo. n % 2 == 0 is the cleanest way to test whether n is even.

Python follows standard math precedence: ** binds tighter than * and /, which bind tighter than + and -. Parentheses override everything:

main.py
Output
Click Run to see the output here.

Use parentheses generously when there's any doubt. They cost nothing and make the intent obvious.

Comparison Operators

Six comparisons, each returning a boolean:

main.py
Output
Click Run to see the output here.

Python lets you chain comparisons, which is unusual and useful:

main.py
Output
Click Run to see the output here.

That reads "age is at least 18 and less than 65." Under the hood it's equivalent to 18 <= age and age < 65, but it reads more like the math.

Logical Operators

Written as English words, not symbols:

main.py
Output
Click Run to see the output here.

Two details worth knowing:

Short-circuit evaluation. and stops at the first falsy value; or stops at the first truthy value. The right side isn't even evaluated if the left side already decides the answer. This lets you guard against errors safely:

main.py
Output
Click Run to see the output here.

and/or return the value, not always True/False. They return whichever operand decided the result:

main.py
Output
Click Run to see the output here.

You'll see name = user_input or "anonymous" in real code — that's a default-value pattern built on this behavior.

Assignment Operators

= assigns. The compound forms combine an operation with assignment:

main.py
Output
Click Run to see the output here.

The compound forms also work for strings (s += "more") and lists (lst += [4, 5]). They're not faster than writing the full form; they're just shorter.

Membership: in and not in

Test whether a value appears inside a container:

main.py
Output
Click Run to see the output here.

in works on any iterable — lists, tuples, sets, strings, dicts, and more. For large lookups, in on a list is O(n); in on a set or dict is O(1). That matters once you're checking membership many times.

Identity: is and is not

is checks whether two variables point at the same object in memory, not just equal values:

main.py
Output
Click Run to see the output here.

Almost always you want ==. The one place is really shines is checking against None, True, and False:

main.py
Output
Click Run to see the output here.

The community convention is firm: is None, not == None. Same for is True (though you rarely need that).

Bitwise Operators (Skip This on First Read)

Python also has bitwise operators — &, |, ^, ~, <<, >> — for manipulating individual bits of an integer. They're useful in cryptography, low-level networking, and certain algorithms. You can safely ignore them on a first pass.

The Walrus Operator (:=)

Python 3.8 added :=, which assigns and returns a value in one expression. Useful in conditions where you want to both capture and test a result:

main.py
Output
Click Run to see the output here.

Without the walrus, you'd call len(numbers) twice or add an extra line. It's not essential; use it where it reduces repetition.

Operator Precedence Cheat Sheet

Tightest-to-loosest binding for the operators you'll actually use:

  1. ** — exponent
  2. *, /, //, % — multiplication and division
  3. +, - — addition and subtraction
  4. <, <=, >, >=, ==, != — comparisons
  5. not
  6. and
  7. or

When in doubt, parenthesise. Clarity beats cleverness.

Moving Into Control Flow

With operators under your belt, you can write conditions. The next page is if/elif/else — the construct that lets your program take one path or another based on what the operators tell it.

Frequently Asked Questions

What are the main types of operators in Python?

Arithmetic (+, -, *, /, //, %, **), comparison (==, !=, <, <=, >, >=), logical (and, or, not), assignment (=, +=, -=, …), membership (in, not in), and identity (is, is not).

What's the difference between == and is in Python?

== checks whether two values are equal. is checks whether two variables refer to the exact same object in memory. For None, True, and False, use is — those are singletons. For everything else, == is almost always what you want.

What does the % operator do in Python?

For numbers, % is the modulo operator — it returns the remainder of a division. 17 % 5 is 2. For strings it's an older-style formatting syntax (like "hello %s" % name), but f-strings have mostly replaced that.

Learn to code with Coddy

GET STARTED