dev.rean.me
python

09 How to Use Operator and IF statement in Python + Tips (Video in Khmer)

Learn Python comparison operators, logical operators, if, elif and else with simple example and tips. Good for beginner!

2026-10-21 ยท 4 min read

Share:

Hello friend! ๐Ÿ‘‹ Program need to make decision. Example: "If age is 18 or more, can vote."

In this post, I show you operators and if statement in Python. Easy and short. ๐Ÿ


โš–๏ธ Comparison Operators

Compare two value. Result is always True or False.

OperatorMeanExampleResult
==Equal5 == 5True
!=Not equal5 != 3True
>Bigger than5 > 3True
<Smaller than5 < 3False
>=Bigger or equal5 >= 5True
<=Smaller or equal3 <= 2False
age = 20

print(age == 20)   # True
print(age != 20)   # False
print(age > 18)    # True
print(age <= 18)   # False

Careful: = put value into variable. == compare value.

x = 5      # put 5 into x
x == 5     # check x is 5 or not

๐ŸŽฅ Video: Using Comparison Operators in Python (Khmer)


๐Ÿ”— Logical Operators

Join many condition together.

OperatorMean
andBoth must be True
orOne is True is enough
notChange True to False
age = 20
has_id = True

print(age >= 18 and has_id)   # True
print(age < 18 or has_id)     # True
print(not has_id)             # False

โœ… if Statement

Run code only if condition is True.

age = 20

if age >= 18:
    print("You can vote")

print("Done")

Remember: put : after condition, and indent the code inside (4 space).


โ†”๏ธ if ... else

Two way: one for True, one for False.

age = 15

if age >= 18:
    print("Adult")
else:
    print("Child")

Output:

Child

๐Ÿชœ if ... elif ... else

Many way. Python check from top to bottom. Stop at first True.

score = 85

if score >= 90:
    print("Grade A")
elif score >= 80:
    print("Grade B")
elif score >= 70:
    print("Grade C")
else:
    print("Fail")

Output:

Grade B

๐ŸŽฅ Video: Using IF and IF ELIF ELSE (Khmer)


๐Ÿงช More Example

Check with user input

age = int(input("Enter age: "))

if age >= 18 and age <= 60:
    print("Working age")
elif age > 60:
    print("Retired")
else:
    print("Student")

Short way: chain compare

age = 25

print(18 <= age <= 60)   # True

Check list and string

fruits = ["apple", "banana"]

if "apple" in fruits:
    print("We have apple")

name = ""
if not name:
    print("Name is empty")

One line if (ternary)

age = 20
status = "Adult" if age >= 18 else "Child"
print(status)   # Adult

โŒ Common Error

Use = instead of ==:

if age = 18:    # SyntaxError
    print("Yes")

Fix: if age == 18:

Forget :

if age >= 18
    print("Adult")    # SyntaxError

Forget indent:

if age >= 18:
print("Adult")        # IndentationError

Wrong order in elif:

score = 95

if score >= 70:
    print("Grade C")   # run this, wrong!
elif score >= 90:
    print("Grade A")   # never run

Fix: check biggest first.


๐Ÿ’ก Tips

  • Use == to compare, = to save value.
  • Always put : after if, elif, else.
  • Use 4 space for indent. Same block must have same space.
  • In elif, put the biggest or most special condition first.
  • Use and, or instead of many nested if.
  • Use 18 <= age <= 60 โ€” short and easy to read.
  • Empty string, empty list, 0 and None are False. Use if not name: to check.
  • Need compare string? Use .lower(): if answer.lower() == "yes":
  • input() give string. Use int() before compare number.

โ“ Any Question?

Tell us in comment below! ๐Ÿ‘‡

#Python #Operators #IfElse #Condition #LearnPython #Beginner


๐Ÿ“š Related Post

โ† Back to python
Share: