From Math to Logic – Meet Python’s Operators Squad


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.
Operator | Meaning | Example | Result |
+ | Addition | 5 + 2 | 7 |
- | Subtraction | 5 - 2 | 3 |
* | Multiplication | 5 * 2 | 10 |
/ | Division | 5 / 2 | 2.5 |
// | Floor Division | 5 // 2 | 2 |
% | Modulus (remainder) | 5 % 2 | 1 |
** | Exponent (power) | 2 ** 3 | 8 |
→ Assignment Operators:
These combine an arithmetic operator with the assignment operator to update a variable's value in a shorter way.
Operator | Meaning | Example | Same As |
= | Assign | x = 5 | x = 5 |
+= | Add and assign | x += 3 | x = x + 3 |
-= | Subtract and assign | x -= 2 | x = x - 2 |
*= | Multiply and assign | x *= 4 | x = x * 4 |
/= | Divide and assign | x /= 2 | x = x / 2 |
//= | Floor divide and assign | x //= 2 | x = x // 2 |
%= | Modulus and assign | x %= 3 | x = x % 3 |
**= | Exponent and assign | x **= 2 | x = 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.
Operator | Meaning | Returns True When... |
and | Logical AND | Both conditions are True |
or | Logical OR | At least one condition is True |
not | Logical NOT (negation) | The original condition is False |
and-
or-
not-
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:
10
→1010
in binary3
→0011
in binary
Operator | Symbol | What It Does |
AND | & | Returns 1 if both bits are 1 |
Or | ‘ | Returns 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).
→ 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
→ 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.
Here are some code snippets in Colab (↖︎click here) for some hands-on practice!
Thanks for reading!😁
Subscribe to my newsletter
Read articles from Anindita Ghosh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
