Python Operators with Examples for Beginners: Quick Guide

Python Operators with Examples for Beginners: Quick Guide


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?

Python operators with examples for beginners: arithmetic, comparison, logical, and assignment illustrated
Beginner-friendly view of arithmetic, comparison, logical, and assignment operators in Python

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

Python operators with examples for beginners: membership vs identity with containers and references
Membership vs identity in Python shown with containers and shared references
  • Arithmetic: + - * / // % **
  • Comparison: == != < <= > >= (including chained comparisons)
  • Logical (Boolean): and or not
  • Assignment: = and augmented assignment like +=, plus the “walrus” operator :=
  • Identity: is is not
  • Membership: in not in
  • Bitwise: & | ^ ~ << >> (operate on integer bits)
  • Modern/useful: dictionary merge | and update |=, type-hint unions X | 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

a = 7
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
Output
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

Step 1 — Do you need a decimal (float) result like 2.5?

Yes → Use / (true division)   e.g., 5 / 2 == 2.5
No → Go to Step 2

Step 2 — Do you need just the remainder?

Yes → Use % (modulo)   e.g., 7 % 3 == 1
No → Use // (floor division)   e.g., 7 // 3 == 2

Tip: // 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

x = 5
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
Output
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 == 3True
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 and False) # both must be True
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)
Output
False
True
False
0
5
5
2
guest

Logical 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
Remember: use and/or/not for Boolean logic; use & | ^ ~ for integer bit operations.

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

Code
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)
Output
5
Count: 3

Membership 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.

nums = [1, 2, 3]
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")
Output
True
True
False
True
x has no value

Use 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.

a = 6 # 110 in binary
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)
Output
2
7
5
-7
12
3
  • Use logical not for booleans; avoid applying bitwise ~ to boolean values.
  • & and | are not the same as and and or.

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 |=.

a = {"x": 1, "y": 2}
b = {"y": 99, "z": 3}

c = a | b
print(c)

a |= b
print(a)
Output
{'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.

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 < <= > >= != == in not in is is not
  • not, then and, then or
print(2 + 3 * 4) # * before +
print((2 + 3) * 4) # parentheses first
print(not True or False) # not before or
print(True or 0 and 5) # and before or
Output
14
20
False
True

Chained 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 == in if: if a = 3: is invalid. Write if a == 3:.
  • Using is for numbers/strings: a is 5 is not value comparison. Use a == 5. Reserve is for identity, e.g., is None.
  • Confusing and/or with &/|: Use and/or for Boolean logic; &/| for bitwise on integers.
  • Expecting / to truncate: In Python 3, / is true division; use // for floor division.
  • Thinking in returns an index: It returns True/False. To get an index, use .index() on lists.
  • Forgetting short-circuit results: and/or return operands, not always booleans. Wrap with bool(...) if you need a strict True/False.
  • Applying bitwise NOT to booleans: Prefer not flag instead of ~flag.

Quick operator choices: a beginner-friendly checklist

  • Compare values with ==; compare identity with is (use x is None for None checks).
  • 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

  1. Write an expression that yields 20 using **, *, +, and parentheses.
  2. Check if a score s is between 50 and 80 inclusive using a single chained comparison.
  3. Given name (string), print the name or "Anonymous" using or.
  4. Merge two dicts a and b so that keys from b override a.

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

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted