Operators in Python
Dataset එකක් filter කරන එක — "වයස 30 ට වැඩි rows විතරක්" — කියන්නෙ comparison operators column එකකට එකපාරටම දාන එක විතරයි.
Filtering a dataset — "only rows where age > 30" — is just comparison operators applied to a whole column at once.
Lesson 5 of 64 · Part 1 · Python & Data Basics · Python Fundamentals · bilingual notes (English + සිංහල)
Why this matters. Filtering a dataset — "only rows where age > 30" — is just comparison operators applied to a whole column at once.
සිංහලෙන්: Dataset එකක් filter කරන එක — "වයස 30 ට වැඩි rows විතරක්" — කියන්නෙ comparison operators column එකකට එකපාරටම දාන එක විතරයි.
What you'll use this for. Every if condition and every Pandas filter you'll ever write starts here.
සිංහලෙන්: ඔයා ලියන හැම if condition එකක්ම, හැම Pandas filter එකක්ම පටන් ගන්නෙ මෙතනින්.
Key principles
- == compares, = assigns — mixing these is the #1 beginner bug.
x == 5asks "is x 5?".x = 5makes x become 5. Confusing the two silently overwrites data instead of checking it.
== සංසන්දනය කරනවා, = assign කරනවා — මේ දෙක පටලවා ගැනීම beginner bug #1
Walkthrough
Operators in Python:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Identity Operators
- Membership Operators
1. Arithmetic Operators
num_1 = 20
num_2 = 10
# addition
sum = num_1 + num_2
print('sum = ',sum)
# subtraction
diff = num_1 - num_2
print('difference = ',diff)
# multiplication
pro = num_1 * num_2
print('product = ',pro)
# division
quo = num_1 / num_2
print('quotient = ',quo)
# exponent
exp = num_1**num_2 # 20^10
print('exponent = ',exp)
# modulus
mod = num_1 % num_2
print('reminder = ',mod)Output
sum = 30
difference = 10
product = 200
quotient = 2.0
exponent = 10240000000000
reminder = 02. Assignment Operators
+=
-=
*=
**=
/=
%=
a = 5
print(a)Output
5a = 5
a += 5 # a = a + 5
print(a)Output
10b = 5
b -= 2 # b = b-2
print(b)Output
33. Comparison Operators
a = 5
b = 10
print(a == b) # equal to
print(a != b) # not equal to
print(a > b) # greater than
print(a < b)
print(a <= b)
print(a >= b)Output
False
True
False
True
True
False4. Logical Operators
and
or
not
a = 10
print( a>20 and a>5)
print( a>20 or a>5)
print( not( a>8 and a>5))Output
False
True
False- Identity Operator:
is
is not
x = 5
y = 5
print(x is y)Output
Truex = 5
y = 10
print(x is y)Output
Falsex = 5
y = 5
print(x is not y)Output
Falsex = 5
y = 10
print(x is not y)Output
True- Membership Operator:
in
not in
a = 5
b =10
c = [1,2,3,4,5]
print( a in c)
print( b in c)Output
True
Falsea = 5
b =10
c = [1,2,3,4,5]
print( a not in c)
print( b not in c)Output
False
TrueKeep going: ← Lesson 4 · Lesson 6 → · Track it in the Self Guide
Finished this one?
Part of Self Guide · Stage 13: ML 1 — Python & Data Basics. Progress is saved in this browser.



