Galle, LK--:--5/6 sites live

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.

2 May 20263 min readSelf Guide · Stage 13#Python & Data Basics#Python Fundamentals#Machine learning

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 == 5 asks "is x 5?". x = 5 makes x become 5. Confusing the two silently overwrites data instead of checking it.
    == සංසන්දනය කරනවා, = assign කරනවා — මේ දෙක පටලවා ගැනීම beginner bug #1

Walkthrough

Operators in Python:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Identity Operators
  6. Membership Operators

1. Arithmetic Operators

ExamplePython
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

Output
sum =  30
difference =  10
product =  200
quotient =  2.0
exponent =  10240000000000
reminder =  0

2. Assignment Operators

+=

-=

*=

**=

/=

%=

ExamplePython
a = 5
print(a)

Output

Output
5
ExamplePython
a = 5
a += 5   # a = a + 5
print(a)

Output

Output
10
ExamplePython
b = 5
b -= 2   # b = b-2
print(b)

Output

Output
3

3. Comparison Operators

ExamplePython
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

Output
False
True
False
True
True
False

4. Logical Operators

and

or

not

ExamplePython
a = 10

print( a>20 and a>5)
print( a>20 or a>5)
print( not( a>8 and a>5))

Output

Output
False
True
False
  1. Identity Operator:

is

is not

ExamplePython
x = 5
y = 5

print(x is y)

Output

Output
True
ExamplePython
x = 5
y = 10

print(x is y)

Output

Output
False
ExamplePython
x = 5
y = 5

print(x is not y)

Output

Output
False
ExamplePython
x = 5
y = 10

print(x is not y)

Output

Output
True
  1. Membership Operator:

in

not in

ExamplePython
a = 5
b =10

c = [1,2,3,4,5]
print( a in c)
print( b in c)

Output

Output
True
False
ExamplePython
a = 5
b =10

c = [1,2,3,4,5]
print( a not in c)
print( b not in c)

Output

Output
False
True

Keep going: ← Lesson 4 · Lesson 6 → · Track it in the Self Guide