Python Operators – A Complete Guide for Beginners (All 7 Types Explained)

Python Operators – A Complete Guide for Beginners (All 7 Types Explained)

Published on Code with Py  |  Category: Python Basics  |  Reading time: ~16 min

When you write a Python program, you do not just store values — you perform actions on them. Adding numbers, comparing values, combining conditions, checking membership — all of these are done using operators. In this complete guide, you will learn all 7 types of Python operators with clear syntax, tables, code examples with outputs, and real-life use cases — everything you need to use operators confidently.

Table of Contents

  1. What are Operators in Python?
  2. Arithmetic Operators
  3. Assignment Operators
  4. Comparison (Relational) Operators
  5. Logical Operators
  6. Bitwise Operators
  7. Membership Operators
  8. Identity Operators
  9. Operator Precedence
  10. FAQ

1. What are Operators in Python?

An operator is a special symbol that tells Python what action to perform on one or more values. The values that an operator works on are called operands.

a = 10
b = 5
print(a + b)   # + is the operator, a and b are operands
15

Python has 7 main categories of operators. Here is a quick overview before we dive into each one:

1
Arithmetic
2
Assignment
3
Comparison
4
Logical
5
Bitwise
6
Membership
7
Identity

2. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical calculations — addition, subtraction, multiplication, division, and more.

OperatorMeaningExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division (float)10 / 33.333...
//Floor Division (integer)10 // 33
%Modulus (remainder)10 % 31
**Exponentiation (power)2 ** 38
a = 10
b = 3

print(a + b)    # 13
print(a - b)    # 7
print(a * b)    # 30
print(a / b)    # 3.3333333333333335
print(a // b)   # 3  (floor – rounds down)
print(a % b)    # 1  (remainder)
print(a ** b)   # 1000  (10 to the power 3)
13
7
30
3.3333333333333335
3
1
1000
// (floor division) always rounds down to the nearest integer. 10 // 3 gives 3, not 3.33. Use % to find the remainder — great for checking even/odd or divisibility.

Real-life use of arithmetic operators

# Calculate total bill with discount
price = 1200
discount = 200
gst = 18

net_price = price - discount
tax = (net_price * gst) // 100
total = net_price + tax

print(f"Net Price: ₹{net_price}")
print(f"GST (18%): ₹{tax}")
print(f"Total Bill: ₹{total}")
Net Price: ₹1000
GST (18%): ₹180
Total Bill: ₹1180

3. Assignment Operators

Assignment operators are used to assign or update the value of a variable. The basic assignment operator is =. The compound operators like += and -= combine an operation with assignment — they are shorthand for writing a = a + 3 as a += 3.

OperatorMeaningEquivalent to
=Assign valuea = 5
+=Add and assigna = a + 3
-=Subtract and assigna = a - 3
*=Multiply and assigna = a * 3
/=Divide and assigna = a / 3
//=Floor divide and assigna = a // 3
%=Modulus and assigna = a % 3
**=Exponent and assigna = a ** 2
x = 10
print(f"Start: {x}")

x += 5
print(f"After += 5: {x}")

x -= 3
print(f"After -= 3: {x}")

x *= 2
print(f"After *= 2: {x}")

x //= 4
print(f"After //= 4: {x}")

x **= 2
print(f"After **= 2: {x}")
Start: 10
After += 5: 15
After -= 3: 12
After *= 2: 24
After //= 4: 6
After **= 2: 36

4. Comparison (Relational) Operators

Comparison operators compare two values and always return either True or False. They are the backbone of all condition-based logic — used heavily in if statements, loops, and filters.

OperatorMeaningExampleResult
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than10 > 5True
<Less than3 < 7True
>=Greater than or equal5 >= 5True
<=Less than or equal4 <= 6True
x = 5
y = 10

print(x == y)   # False
print(x != y)   # True
print(x > y)    # False
print(x < y)    # True
print(x >= y)   # False
print(x <= y)   # True
False
True
False
True
False
True
Do not confuse = (assignment) with == (comparison). Writing if x = 5 causes a SyntaxError. Always use == to compare values inside conditions.

Comparison in real programs

marks = 72
pass_mark = 40

if marks >= pass_mark:
    print("Passed")
else:
    print("Failed")
Passed

5. Logical Operators

Logical operators are used to combine two or more conditions together. They are most commonly used inside if statements when you need to check multiple things at once.

OperatorMeaningReturns True when
andLogical ANDBoth conditions are True
orLogical ORAt least one condition is True
notLogical NOTThe condition is False (reverses it)
age = 20
has_id = True

print(age >= 18 and has_id)   # True (both are True)
print(age >= 18 or has_id)    # True (at least one is True)
print(not has_id)              # False (reverses True)
True
True
False
# Real use: Check eligibility
salary = 35000
experience = 3

if salary >= 30000 and experience >= 2:
    print("Eligible for loan")
else:
    print("Not eligible")
Eligible for loan
Use and when ALL conditions must be True. Use or when ANY one condition being True is enough. Use not to flip a True to False or False to True.

6. Bitwise Operators

Bitwise operators work directly on the binary (bit-level) representation of numbers. They are used in low-level programming, encryption, image processing, and performance optimization. Each number is first converted to binary, then the operation is applied bit by bit.

OperatorMeaningExampleResult
&Bitwise AND5 & 31
|Bitwise OR5 | 37
^Bitwise XOR5 ^ 36
~Bitwise NOT~5-6
<<Left Shift5 << 110
>>Right Shift5 >> 12
a = 5   # Binary: 0101
b = 3   # Binary: 0011

print(a & b)    # AND:  1  (0001)
print(a | b)    # OR:   7  (0111)
print(a ^ b)    # XOR:  6  (0110)
print(~a)       # NOT: -6  (two's complement)
print(a << 1)  # Left shift:  10 (1010)
print(a >> 1)  # Right shift:  2 (0010)
1
7
6
-6
10
2

How bitwise AND and OR work — step by step

AND operation: 5 & 3 → 1  (both bits must be 1)
a = 50101
b = 30011
result0001= 1
OR operation: 5 | 3 → 7  (at least one bit must be 1)
a = 50101
b = 30011
result0111= 7
XOR operation: 5 ^ 3 → 6  (bits must be different)
a = 50101
b = 30011
result0110= 6
Left shift << n multiplies the number by 2ⁿ. Right shift >> n divides by 2ⁿ (integer division). So 5 << 1 = 10 and 5 >> 1 = 2.

7. Membership Operators

Membership operators test whether a value exists inside a sequence such as a list, tuple, string, or set. They return True or False and are very commonly used in real-world Python programs.

OperatorMeaningExampleResult
inValue exists in sequence'a' in 'cat'True
not inValue does not exist'z' not in 'cat'True
fruits = ["apple", "banana", "mango"]

print("apple" in fruits)       # True
print("orange" in fruits)      # False
print("grape" not in fruits)   # True

# Also works with strings
print("Py" in "Python")         # True
print("java" in "Python")       # False
True
False
True
True
False
# Real use: Check allowed users
allowed_users = ["admin", "rohan", "priya"]
username = "rohan"

if username in allowed_users:
    print("Access granted")
else:
    print("Access denied")
Access granted

8. Identity Operators

Identity operators check whether two variables point to the exact same object in memory — not just whether they have equal values. This is a subtle but important difference.

OperatorMeaningReturns True when
isSame object in memoryBoth variables point to same object
is notDifferent object in memoryVariables point to different objects
x = [1, 2, 3]
y = [1, 2, 3]
z = x           # z points to the SAME object as x

print(x is z)       # True  (same object)
print(x is y)       # False (different objects, same content)
print(x == y)       # True  (same content/value)
print(x is not y)  # True  (different objects)
True
False
True
True
is checks memory location (are they the same object?). == checks value equality (do they have the same content?). Two variables can have equal values but still be different objects — use == for value comparison in most programs.

9. Operator Precedence

When an expression has multiple operators, Python follows a specific order called operator precedence — similar to the BODMAS rule in mathematics. Operators with higher precedence are evaluated first.

result1 = 2 + 3 * 4      # * has higher precedence than +
result2 = (2 + 3) * 4    # () evaluated first

print(result1)   # 14
print(result2)   # 20
14
20

Here is the precedence order from highest to lowest:

PriorityOperator(s)Description
1 (highest)()Parentheses
2**Exponentiation
3+x, -x, ~xUnary operators
4*, /, //, %Multiplication, division, modulus
5+, -Addition, subtraction
6<<, >>Bitwise shifts
7&Bitwise AND
8^Bitwise XOR
9|Bitwise OR
10==, !=, <, >, <=, >=, is, inComparisons, identity, membership
11notLogical NOT
12andLogical AND
13 (lowest)orLogical OR
When in doubt about precedence, use parentheses () to make the order explicit. It also makes your code easier to read for others.

Quick Summary – All 7 Operator Types
  • Arithmetic – + - * / // % ** for math calculations
  • Assignment – = += -= *= etc. to assign or update variables
  • Comparison – == != > < >= <= return True or False
  • Logical – and or not combine or reverse conditions
  • Bitwise – & | ^ ~ << >> operate on binary bits
  • Membership – in not in check if value exists in a sequence
  • Identity – is is not check if two variables are the same object
  • Use parentheses to control the order when multiple operators appear together

Frequently Asked Questions (FAQ)

Q1. What is the difference between == and = in Python?

= is the assignment operator — it stores a value in a variable. == is the comparison operator — it checks if two values are equal and returns True or False. Using = inside an if condition causes a SyntaxError.

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

/ is regular division and always returns a float, even if the result is a whole number (e.g. 10/2 = 5.0). // is floor division and returns an integer by rounding down (e.g. 10//3 = 3, not 3.33).

Q3. What is the difference between is and == in Python?

== checks if two values are equal. is checks if two variables point to the exact same object in memory. Two variables can have equal values (== True) but still be different objects (is False).

Q4. When should I use logical operators vs bitwise operators?

Use logical operators (andornot) for combining boolean conditions in if statements and loops. Use bitwise operators (&|^) when working directly with binary data, flags, or low-level operations.

Q5. What does the % operator do in Python?

The % operator returns the remainder of a division. For example, 10 % 3 = 1 because 10 divided by 3 gives 3 with a remainder of 1. It is commonly used to check if a number is even (n % 2 == 0) or divisible by something.

Q6. Can membership operators be used with strings?

Yes. The in and not in operators work with strings, lists, tuples, sets, and dictionaries. For strings, they check whether a substring exists — for example, "Py" in "Python" returns True.


Found this post helpful? Share it with someone learning Python. Drop your questions in the comments — happy to help!

"Every Python expert once struggled with the basics. Learn slowly, practise daily, and trust the process."

Comments

Popular posts from this blog

Python Strings – A Complete Guide for Beginners (with Examples)

Python Control Flow Statements – Complete Guide with Real-Life Examples

Python Loops Explained – for Loop and while Loop with Examples and Output