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
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.
| Operator | Mean | Example | Result |
|---|---|---|---|
== | Equal | 5 == 5 | True |
!= | Not equal | 5 != 3 | True |
> | Bigger than | 5 > 3 | True |
< | Smaller than | 5 < 3 | False |
>= | Bigger or equal | 5 >= 5 | True |
<= | Smaller or equal | 3 <= 2 | False |
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.
| Operator | Mean |
|---|---|
and | Both must be True |
or | One is True is enough |
not | Change 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
:afterif,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,orinstead of many nestedif. - Use
18 <= age <= 60โ short and easy to read. - Empty string, empty list,
0andNoneareFalse. Useif not name:to check. - Need compare string? Use
.lower():if answer.lower() == "yes": input()give string. Useint()before compare number.
โ Any Question?
Tell us in comment below! ๐
#Python #Operators #IfElse #Condition #LearnPython #Beginner