From Math to Logic – Meet Python’s Operators Squad

Anindita GhoshAnindita Ghosh
8 min read

At the beginning of our Python journey, we encounter some key concepts—words like operators’, ‘strings’, and the idea of mutable vs. immutable’. At first glance, these terms might sound a bit technical or a bit complex. But here’s the good news: they’re not hard concepts; they’re simpler than they seem. They’re also fundamental to how Python works behind the scenes.

Think of this concept as the grammar and rules of the Python language. Just like how we need to know how to form sentences to speak. But once we understand those concepts, they start to feel easy and natural — just like using our phone every day."😉

So let’s dive in and break it all down, step by step.

What is operators?

Operators are special symbols or keywords in Python that are used to perform operations on values or variables. Think of them as tools that help Python to do some operation - like add numbers, compare values, or even manipulate text.

Types of operators:

In Python, there are 7 types of operators, each serving a different purpose. Let’s explore each one in detail how they work and where to use them.

Arithmetic Operators:

Arithmetic Operators are used to perform basic mathematical operations in Python — like addition, subtraction, multiplication, division, and more.

OperatorMeaningExampleResult
+Addition5 + 27
-Subtraction5 - 23
*Multiplication5 * 210
/Division5 / 22.5
//Floor Division5 // 22
%Modulus (remainder)5 % 21
**Exponent (power)2 ** 38

Assignment Operators:

These combine an arithmetic operator with the assignment operator to update a variable's value in a shorter way.

OperatorMeaningExampleSame As
=Assignx = 5x = 5
+=Add and assignx += 3x = x + 3
-=Subtract and assignx -= 2x = x - 2
*=Multiply and assignx *= 4x = x * 4
/=Divide and assignx /= 2x = x / 2
//=Floor divide and assignx //= 2x = x // 2
%=Modulus and assignx %= 3x = x % 3
**=Exponent and assignx **= 2x = x ** 2

Here are some code snippets:

# = Assign
x = 5
print("x =", x)  # Output: 5

# -= Subtract and assign
x -= 2  # same as x = x - 2
print("x -= 2 -->", x)  # Output: 6

# *= Multiply and assign
x *= 4  # same as x = x * 4
print("x *= 4 -->", x)  # Output: 24

# %= Modulus and assign
x %= 3  # same as x = x % 3
print("x %= 3 -->", x)  # Output: 0.0

Comparison Operators:

Comparison operators are used to compare two values.

x = 8
y = 5

print(x == y)   # Output = False (8 is not equal to 5)
print(x != y)   # Output = True  (they're different)
print(x > y)    # Output = True  (8 is greater than 5)
print(x < y)    # Output = False (8 is not less than 5)
print(x >= 8)   # Output = True  (8 is equal to 8)
print(y <= 5)   # Output = True  (5 is equal to 5)

Logical Operators:

Logical operators are used to combine or modify Boolean expressions — expressions that return either True or False.
These operators are especially useful in control structures like if statements, where Python needs to decide whether or when to execute certain code blocks.

Example 1:
“I will go outside if it is sunny and I am free.”
Both must be true for the action to happen.

Example 2:
“I will eat out if I am hungry or there’s no food at home.”
Only one of the two needs to be true for the action to happen.

OperatorMeaningReturns True When...
andLogical ANDBoth conditions are True
orLogical ORAt least one condition is True
notLogical NOT (negation)The original condition is False

and-

Python Equivalent of the '&&' Operator ...

or-

Bitwise Operators in Python – Real Python

not-

Virtual Labs

Code snippets of And, Or & Not:

x = 8
y = 4

# AND - both conditions must be true
print(x > 5 and y < 10)   # True (8 > 5 AND 4 < 10)

# OR - at least one condition must be true
print(x < 5 or y < 10)    # True (x < 5 is False, but y < 10 is True)

# NOT - reverses the result
print(not(x > 5))         # False (because x > 5 is True, so not True = False)

Bitwise Operators:

Bitwise operators are used to perform operations at the bit level of numbers — meaning they work directly with the binary representation of integers.

Every number in our computer is stored in binary — a series of 0s and 1s (called bits). So when we write numbers like 10 or 3, bitwise operators go deeper and breaks into binary numbers.

For example:

  • 101010 in binary

  • 30011 in binary

OperatorSymbolWhat It Does
AND&Returns 1 if both bits are 1
OrReturns 1 if at least one bit is 1
XOR^Returns 1 if bits are different
NOT~Flips all bits (1 becomes 0, 0 becomes 1)
Left Shift<<Moves bits to the left, adds zeros on the right
Right Shift>>Moves bits to the right, discards bits on the right
a = 5       # 0101
b = 3       # 0011

print(a & b)   # 1 (0001)
print(a | b)   # 7 (0111)

# Let's break down how binary to decimal conversion is working
'''  0   1   1   1
     |   |   |   |
    2^3 2^2 2^1 2^0
     8   4   2   1 

    (0 × 8) + (1 × 4) + (1 × 2) + (1 × 1)
    =    0  +    4  +    2  +    1
    = 7    '''

print(a ^ b)   # 6 (0110)
print(~a)      # -6 (Two's complement of 5)
print(a << 1)  # 10 (1010)
print(a >> 1)  # 2  (0010)

Left shift(<<)

What it does:
Shifts all bits in a number to the left by the number of positions you specify.
For every left shift, you're multiplying the number by 2.

Right shift(>>)

What it does:
Shifts all bits in a number to the right by the number of positions you specify.
For every right shift, you're dividing the number by 2 (and rounding down).

Shift Right

Membership operators:

A membership operator in Python is used to check whether a value exists in a sequence (like a list, string, tuple, set, or dictionary).

# List example
fruits = ["apple", "banana", "cherry"]

print("apple" in fruits)      # Output-> True
print("grape" not in fruits)  # Output-> True

# String example
word = "hello"

print("h" in word)            # Output-> True
print("z" not in word)        # Output-> True

Identity Operators:

We can think of identity operators as checking someone's ID card, and equality as checking if two people have the same name. Two people might have the same name (value equality), but only one will have a specific ID card (object identity).

x = 5
y = x 
print(x is y)      # Output -> True
print(x == y)      # Output -> True
print(id(x))
print(id(y))



x = 7 
y = 9
print(x is y)      # Output -> False

x = 7 
y = 9
print(x is not y)  # Output -> True

Let’s try to understand what ID is and why it plays a huge role:

In Python, id()gives every object a specific memory. It's how Python keeps track of stuff behind the scenes.

We can think of it like this:
Imagine there are two water bottles. They both look the same, but if we put a sticker on each bottle with a unique number, now can these two are differentiable. That sticker is kind of like the id() in Python.

x = 5
y = x 

print(id(x))
# 4352794976 
print(id(y))
# 4352794976 (same as x)

# For list 
x = [1,3,5]
y = x.append(7)

print(id(x))
# 4301256384
print(id(y))  # here the id is not same as (x)
# 4312326936

Image

→ Conclusion:

Programming is deeply rooted in mathematical logic, and Python brings this to life through various operators that let us control and manipulate data efficiently. From performing basic math using arithmetic operators to comparing values with comparison operators or even combining logic through logical operators, each type has a specific role in shaping how our code behaves.

We also use bitwise operators for low-level binary operations, assignment operators for updating variables, and membership operators to check whether something exists within a sequence. On a deeper level, identity operators help us determine whether two variables are referencing the same object in memory.

Thanks for reading!😁

81
Subscribe to my newsletter

Read articles from Anindita Ghosh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anindita Ghosh
Anindita Ghosh