Post

Learning Python Operators - From Simple Math to Bitwise Logic

On Day 3 of my Python journey, I dive into operators — the building blocks behind conditions, decisions, and data manipulation. This post captures my learning process, the mistakes I made, and the concepts that finally clicked.

Learning Python Operators - From Simple Math to Bitwise Logic

Python has been relatively easy to understand so far, but I’ve noticed that my biggest hurdle is actually writing code, not reading it. I think this comes from spending more time learning about Python than actually practicing it — a polite way of saying procrastination.

That said, I still believe good preparation leads to better code later. Taking the time to really understand concepts has already saved me confusion further down the line.

On Day 3 of the 30 Days of Python course, the focus is on operators1. While many of these were familiar from basic maths, some — especially bitwise operators — were something I completely missed the first few times around. This time, I wanted to slow things down, understand why they work the way they do, and document what I learn along the way.


What Are Python Operators?

Python operators are symbols that allow us to perform actions on data. These actions include:

  • Performing mathematical calculations
  • Assigning or updating values in variables
  • Comparing values
  • Making decisions using conditions
  • Working directly with binary (bit-level) data

In simple terms, operators help Python decide what to do next based on the data we give it.

For example, imagine we have an age variable:

1
2
3
age = 20
age += 1
print(age)  # 21

Here, the += operator updates the value stored in age. This was one of the first moments where it clicked for me that operators don’t just check values — they often change them.

They can also be used for calculations:

1
2
3
4
5
num1 = 3
num2 = 7

ans = num1 + num2
print(ans)  # 10

As I progress through the course, I notice that operators are rarely used in isolation. Most real logic comes from combining multiple operators together, a concept known as compounding, which is where Python code begins to feel more practical and expressive.

Arithmetic Operators

I decided to start with arithmetic operators because they were the first ones I encountered while learning about variables. They also feel the most intuitive, since they behave just like everyday maths.

OperatorNameExample
+Addition5 + 3
-Subtraction5 - 3
*Multiplication5 * 3
/Division6 / 2
%Modulus (remainder)7 % 2
//Floor Division (rounds down)7 // 2
**Exponentiation (power)2 ** 3

Table source: 30 Days of Python - Day 3

These operators form the backbone of most calculations you’ll do in Python.

Examples:

1
2
3
4
5
6
7
8
9
10
num1 = 2
num2 = 6

result1 = num1 + num2   # Addition → 8
result2 = num2 - num1   # Subtraction → 4
result3 = num1 * num2   # Multiplication → 12
result4 = num2 / num1   # Division → 3.0
result5 = num2 % num1   # Modulus → 0
result6 = num1 ** num2  # Exponentiation → 64
result7 = num2 // num1  # Floor division → 3

These operators are simple, but they appear everywhere — from counters and scores to complex algorithms.

Assignment Operators

Assignment operators are used to store or update values in variables.

The most basic one is =:

1
x = 10

However, Python also provides compound assignment operators, which combine both arithmetic and assignment operators into a single step. These felt awkward at first, especially because I kept wanting to put the = first — but once I got used to them, they actually made my code cleaner.

OperatorNameExampleSame As
=Assignmentx = 5x = 5
+=Add and Assignx += 2x = x + 2
-=Subtract and Assignx -= 2x = x - 2
*=Multiply and Assignx *= 2x = x * 2
/=Divide and Assignx /= 2x = x / 2
%=Modulus and Assignx %= 2x = x % 2
//=Floor Divide and Assignx //= 2x = x // 2
**=Exponent and Assignx **= 2x = x ** 2

Table source: 30 Days of Python - Day 3

Once I started seeing these as shortcuts rather than new syntax, they became much easier to remember.

Logical Operators

Logical operators allow us to combine conditions and make more precise decisions.

OperatorNameExample
andLogical ANDx > 5 and x < 10
orLogical ORx == 5 or x == 10
notLogical NOTnot x == 5

Table source: 30 Days of Python - Day 3

Example:

1
2
3
4
day = "Saturday"

if day == "Saturday" or day == "Sunday":
    print("It's the weekend!")

This was one of the first examples where Python logic started to feel real to me — it closely mirrors how we think and reason in everyday situations.

Comparison Operators

Comparison operators are used to compare values and always return a Boolean (True or False).

OperatorNameExample
==Equal tox == 5
!=Not equal tox != 5
>Greater thanx > 5
<Less thanx < 5
>=Greater than or equal tox >= 5
<=Less than or equal tox <= 5

Table source: 30 Days of Python - Day 3

Example:

1
2
3
4
5
6
7
8
9
10
11
12
var_a = 5
var_b = 10

if var_a > var_b:
    print(f"{var_a} is greater!")
else:
    print(f"{var_b} is greater!")

# Output:
'10 is greater!'

print(var_a > var_b) # False

These operators are essential for branching logic and decision-making.

From that example, we are able to see how we can create conditions using IF Statements which is something that I’ll be learning about in Day 9 - Conditionals.

Other Operators

While reviewing the course content and external sources, I came across operator types that weren’t heavily emphasised in the exercises. Even though they’re more niche, I wanted to include them here so my notes stay complete2.

This is a complete guide to use or resource I would read over - Python Identity Operators

Bitwise (Binary) Assignment Operators

This was the section I had never learned before.

Bitwise operators work directly with binary representations of numbers. They’re not something I expect to use daily right now, but understanding them early makes them far less intimidating later.

Bitwise AND (&=):

1
2
3
4
5
a = 5  # 0101
b = 3  # 0011

a &= b
print(a)  # 1 (0001)

Only bits that are 1 in both numbers remain 1.

Bitwise OR (|=):

1
2
3
4
5
a = 5  # 0101
b = 3  # 0011

b |= a
print(b)  # 7 (0111)

If either bit is 1, the result is 1.

Bitwise XOR (^=):

1
2
3
4
5
a = 5  # 0101
b = 3  # 0011

b ^= a
print(b)  # 6 (0110)

Different bits → 1, same bits → 0.

Right Bitwise Shift (>>=):

1
2
3
a = 5  # 0101
a >>= 2
print(a)  # 1 (0001)

Shifts bits to the right, discarding bits and reducing the number.

Left Bitwise Shift (<<=):

1
2
3
a = 5  # 0101
a <<= 2
print(a)  # 20 (10100)

Shifts bits to the left, adding 0’s on the right and increasing the number.

Identity Operators

Identity operators look similar to comparison operators, but instead of comparing values, they compare memory locations.

OperatorDescription
isReturns True if both variables refer to the same object
is notReturns True if both variables do not refer to the same object

Table source: Identity Operators

Think of these as checking whether two variables point to the exact same thing, not just equal-looking values.

This example has been taken from W3 Schools3.

1
2
3
4
5
6
7
x = ["apple", "banana"]
y = ["apple", "banana"]
z = x

print(x is z) # True
print(x is y) # False
print(x == y) # True

Update: I have recently learnt about constant and runtime sequences which explain that any sequencial data, like strings, created before runtime get stored in the same memory location for as long as the data inside are identical. Therefore, if you where to use the identity operator on these variables it would bring back a True statement.

Example:

1
2
3
4
a = "Hello"
b = "Hel" + "lo"

print(a is b) # True

Membership Operators

Membership operators check whether a value exists within another object, such as a list or a string.

OperatorDescription
inReturns True if a sequence with the specified value is present in the object
not inReturns True if a sequence with the specified value is not present in the object

Table source: Membership Operators

Example (Using a List):

1
2
3
4
fruits = ["apple", "banana", "cherry"]

print("banana" in fruits) # True
print("pineapple" not in fruits) # True

Example (Using a String):

1
2
3
4
5
6
7
text = "Membership Operator"

print("M" in text) # True
print("S" in text) # False (case sensitive)
print("Operator" in text) # True
print("operator" in text) # False (case sensitive)
print("z" not in text) # True

Footnote:

This post is licensed under CC BY 4.0 by the author.