Welcome to CodDesire’s step-by-step guide to Python operators with examples for beginners. If you’re just starting Python, understanding operators will make your code shorter, clearer, and more powerful. In this tutorial, you’ll learn what operators are, how they work, and how to avoid common mistakes students make in school and college programming courses.
What are operators in Python for beginners?
Operators are symbols or keywords that tell Python to perform operations on values and variables. For example, + adds numbers, == checks equality, and combines conditions, and in checks membership. Python groups operators into categories such as arithmetic, comparison, logical (Boolean), assignment, identity, membership, bitwise, and a few modern ones you’ll see in real projects.
Basic Python operator categories
- Arithmetic:
+-*///%** - Comparison:
==!=<<=>>=(including chained comparisons) - Logical (Boolean):
andornot - Assignment:
=and augmented assignment like+=, plus the “walrus” operator:= - Identity:
isis not - Membership:
innot in - Bitwise:
&|^~<<>>(operate on integer bits) - Modern/useful: dictionary merge
|and update|=, type-hint unionsX | Y, and matrix multiplication@(used in libraries like NumPy)
Arithmetic operators in Python
Arithmetic operators do math. In Python 3, / always produces a float (true division); use // for floor division (rounded down).
Syntax and quick examples
b = 3
print(a + b) # addition
print(a - b) # subtraction
print(a * b) # multiplication
print(a / b) # true division (float)
print(a // b) # floor division (int)
print(a % b) # remainder (modulo)
print(2 ** 3) # exponent (power)
print(3 * (2 + 1)) # parentheses change precedence
10
4
21
2.3333333333333335
2
1
8
9- Mixing ints and floats results in a float:
1 + 2.0 == 3.0. //floors toward negative infinity:-7 // 3 == -3.
Choose the right division operator (/ vs // vs %) in Python
/ (true division) e.g., 5 / 2 == 2.5% (modulo) e.g., 7 % 3 == 1// (floor division) e.g., 7 // 3 == 2// rounds down toward negative infinity. Example: -7 // 3 == -3.
Comparison operators in Python
Comparison operators compare values and return True or False. Python also supports comparison chaining, which reads like math.
Examples, including chained comparisons
print(x == 5) # equal to
print(x != 5) # not equal to
print(x < 10) # less than
print(0 < x < 10) # chained comparison: 0 < x and x < 10
True
False
True
True“==” vs “=” vs “is” (beginner-friendly)
| Symbol | Meaning | Example |
|---|---|---|
| = | Assignment (store a value in a variable) | a = 3 |
| == | Equality (compare values) | a == 3 → True |
| is | Identity (same object in memory) | x is None |
Tip: Use == to compare values. Use is to compare identity (especially for None checks). Writing if a = 3: is a syntax error—use == in conditions.
Logical operators in Python
Python’s logical operators are keywords: and, or, and not. They short-circuit and return one of the original operands (not necessarily a pure True or False), which is very handy for defaults.
and, or, not with practical examples
print(True or False) # either can be True
print(not True) # invert
# Short-circuit returns operands
print(0 and 5) # 0 is falsy, so returns 0
print(2 and 5) # 2 is truthy, so returns 5
print(0 or 5) # 0 is falsy, so returns 5
print(2 or 5) # 2 is truthy, so returns 2
# Idiomatic default value
user = ""
display = user or "guest"
print(display)
False
True
False
0
5
5
2
guestLogical vs Bitwise operators at a glance (Python operators with examples for beginners)
| Topic | Operators | Works on | Short-circuits? | Returns | Example | Output |
|---|---|---|---|---|---|---|
| Logical AND | and |
Any type (truthiness) | Yes | First falsy or last operand | 0 and 5 |
0 |
| Logical OR | or |
Any type (truthiness) | Yes | First truthy or last operand | "" or "guest" |
"guest" |
| Logical NOT | not |
Any type (truthiness) | N/A | Boolean | not 1 |
False |
| Bitwise AND | & |
Integers (bit patterns) | No | Integer | 6 & 3 |
2 |
| Bitwise OR | | |
Integers (bit patterns) | No | Integer | 6 | 3 |
7 |
| Bitwise XOR | ^ |
Integers (bit patterns) | No | Integer | 6 ^ 3 |
5 |
| Bitwise NOT | ~ |
Integers (two’s complement) | N/A | Integer | ~6 |
-7 |
Assignment operators in Python
Assignment operators set or update variable values. Augmented assignment updates in place where possible. Python also has the “walrus” operator to assign during an expression.
Augmented assignment and walrus operator
total = 0
total += 5 # same as total = total + 5
print(total)
# Walrus operator(Python 3.8+): assign and use in one expression
items = [1, 2, 3]
if (n := len(items)) > 0:
print("Count:", n)5
Count: 3Membership and identity operators with examples
Membership tests if a value is contained in a sequence or collection. Identity tests if two names refer to the same object in memory.
print(2 in nums) # membership
print(4 not in nums)
a = []
b = []
c = a
print(a is b) # different empty lists
print(a is c) # same object
x = None
if x is None:
print("x has no value")
True
True
False
True
x has no valueUse is and is not for None checks; use == for value comparisons like numbers and strings.
Bitwise operators explained simply
Bitwise operators work on the binary representation of integers—useful for flags and low-level tasks. Don’t confuse them with logical and/or.
b = 3 # 011 in binary
print(a & b) # AND: 110 & 011 = 010 (2)
print(a | b) # OR: 110 | 011 = 111 (7)
print(a ^ b) # XOR: 110 ^ 011 = 101 (5)
print(~a) # NOT: bitwise inversion (two's complement)
print(a << 1) # shift left: 1100 (12)
print(a >> 1) # shift right: 11 (3)
2
7
5
-7
12
3- Use logical
notfor booleans; avoid applying bitwise~to boolean values. &and|are not the same asandandor.
Modern operators students should know
Dictionary merge and update (Python 3.9+)
Merge two dicts with |. Right side wins on key conflicts. Update in-place with |=.
b = {"y": 99, "z": 3}
c = a | b
print(c)
a |= b
print(a)
{'x': 1, 'y': 99, 'z': 3}
{'x': 1, 'y': 99, 'z': 3}Union types in type hints (Python 3.10+)
You may see X | Y in annotations meaning “either X or Y”. This is useful for function signatures in typed code.
def parse_number(x: int | str) -> int:
return int(x)Matrix multiplication @
The @ operator is for matrix multiplication (per PEP 465). It’s commonly used with numeric libraries such as NumPy for linear algebra. Built-in Python containers like lists don’t implement @ by default.
Python operator precedence for beginners
Operator precedence decides which parts of an expression are evaluated first. When in doubt, use parentheses for clarity.
- Highest: Parentheses
(...) - Exponent
** - Unary
+-~ - Multiply/Divide
*///% - Add/Subtract
+- - Shifts
<<>> - Bitwise
&then^then| - Comparisons
<<=>>=!===innot inisis not - not, then and, then or
print((2 + 3) * 4) # parentheses first
print(not True or False) # not before or
print(True or 0 and 5) # and before or
14
20
False
TrueChained comparisons like 0 < x < 10 evaluate as 0 < x and x < 10 with x evaluated once.
Common beginner mistakes and quick fixes
- Using
=instead of==inif:if a = 3:is invalid. Writeif a == 3:. - Using
isfor numbers/strings:a is 5is not value comparison. Usea == 5. Reserveisfor identity, e.g.,is None. - Confusing
and/orwith&/|: Useand/orfor Boolean logic;&/|for bitwise on integers. - Expecting
/to truncate: In Python 3,/is true division; use//for floor division. - Thinking
inreturns an index: It returnsTrue/False. To get an index, use.index()on lists. - Forgetting short-circuit results:
and/orreturn operands, not always booleans. Wrap withbool(...)if you need a strictTrue/False. - Applying bitwise NOT to booleans: Prefer
not flaginstead of~flag.
Quick operator choices: a beginner-friendly checklist
- Compare values with ==; compare identity with is (use
x is NoneforNonechecks). - Need a float result? Use /. Need an integer quotient? Use //. Need the remainder? Use %.
- Use and/or/not for Boolean logic; don’t mix them up with & | ^ (bitwise).
- When unsure about order, add parentheses for clarity.
- Use in / not in to test membership; use
list.index()to find positions. - Update values with +=, -=, *=, etc.; use the waltus (
:=) to assign inside expressions thoughtfully. - Merge/override dictionaries with | or update in-place with |= (Python 3.9+).
- Use @ for matrix multiplication with arrays (e.g., NumPy), not with plain lists.
Practice: try these mini-exercises
- Write an expression that yields
20using**,*,+, and parentheses. - Check if a score
sis between 50 and 80 inclusive using a single chained comparison. - Given
name(string), print the name or"Anonymous"usingor. - Merge two dicts
aandbso that keys fromboverridea.
FAQ: Python operators for students
What are the basic operators in Python?
They include arithmetic (+ - * / // % **), comparison (== != < <= > >=), logical (and or not), assignment (= += -= ...), identity (is, is not), membership (in, not in), and bitwise (& | ^ ~ << >>).
How do arithmetic operators work in Python with examples?
Use + to add, - to subtract, * to multiply, / for true division, // for floor division, % for remainder, and ** for exponent. Example: 7 / 3 == 2.3333..., 7 // 3 == 2.
What is the difference between == and = in Python?
= assigns a value to a variable. == compares values and returns True or False. Use is to compare identity (same object), notably for None: if x is None:.
How do logical operators and, or, not work in Python?
They evaluate truthiness and short-circuit. x and y returns x if x is falsy else y. x or y returns x if x is truthy else y. not x flips the truth value. Example: name or "guest".
What is operator precedence in Python for beginners?
Roughly: parentheses, exponent, unary + - ~, multiply/divide, add/subtract, shifts, bitwise, comparisons, not, and, or. Use parentheses if you’re unsure.
Keep learning
Explore the full Python path and more beginner-friendly tutorials on our Python section: CodDesire Python Tutorials.
Sources / Further reading
- Python Language Reference — Expressions (operators, precedence, chaining)
- Built-in Types — Truth Value Testing and Boolean operations
- What’s New in Python 3.9 — Dictionary Merge & Update Operators and PEP 584
- PEP 572 — Assignment Expressions (walrus operator)
- PEP 604 — Union types written as X | Y and the typing module
- PEP 465 — Matrix multiplication operator @


