Instructions
If Statements 🤔
Namba daily life-la neraya mudivugal (decisions) edupom la? For example, "Mazhai peidha, kodai eduthutu poven" (If it rains, I will take an umbrella). Programming-layum idhe madhiri decision edukka if statements use aagudhu!
1. How 'if' works under the hood (Ulla enna nadakkudhu?)
Oru if statement eppavume oru kelvi kekum. Andha kelvikku badhil "Yes" (True) illana "No" (False) nu dhaan irukanum.
Condition unmaya (True) irundha mattum dhaan if block-kulla irukka code run aagum. False aaga irundha, Python andha code-a apdiye thandi poidum (Skip pannidum).
score = 90
if score > 80:
print("Super, neenga pass!")
Inga Python kekura kelvi: "Is 90 greater than 80?". Badhil True. Adhunala, ulla irukka print statement run aagum.
Keezhe irukka Interactive Diagram-a test panni parunga:
2. Real Life Example: Google Pay 💸
Ippa oru GPay transaction example eduthupom. Unga account balance-a vida neenga anuppura amount chinnadha irundha dhaan payment pogum, correct?
balance = 1000
amount_to_send = 500
if balance >= amount_to_send:
print("Payment Successful! ✅")
balance = balance - amount_to_send
print("New Balance:", balance)
print("Have a nice day!")
Try it in your head! Will the payment be successful? Yes, because 1000 is greater than 500!
3. Indentation (Oru step ulla)
Python-la if statement ezhudhum bodhu, adhuku ulla varra code-a oru 'Tab' space (illana 4 spaces) thalli ezhudhanum. Idhuku peru dhaan Indentation.
Idhu Python-ku endha code if block-kulla varum nu puriyavekkum. Space vidalana Python kovichikum (Error varum)!
weather = "rain"
if weather == "rain":
# Indha code 4 space thalli irukku, so if block-kulla varum
print("Take an umbrella")
print("Wear a raincoat")
# Indha code space illama veliya irukku, so eppavum run aagum (Mazhai peidhalum illanalum)
print("Have a safe journey!")
4. Comparison Operators (Oppittu pakradhu)
Conditions create panna namba Comparison Operators use pannuvom:
| Operator | Meaning (Artham) | Example |
|---|---|---|
== | Equal to (Samam) | age == 18 |
!= | Not equal to (Samam illa) | age != 18 |
> | Greater than (Perisu) | score > 50 |
< | Less than (Chinnadu) | score < 50 |
>= | Greater than or equal to | score >= 50 |
<= | Less than or equal to | score <= 50 |
⚠️ Warning!
=vs==: Oru single=vandhu variable-la value store panna use aagum. Double==vandhu rendu values samama nu check panna use aagum. Idhula dhaan neraya paer thappu pannuvanga!
5. Common Mistakes to Avoid (Thavirkka vendiya thavurugal) 🚫
- Marandhu poyi colon
:vaikama vidradhu:if age > 18nu potutu colon vaikama vita, SyntaxError varum. Eppavumifline mudiyum idathula:irukkanum! - Indentation thappu:
ifku adutha line-a thalli vaikama (space illama) ezhudhina IndentationError varum.
Your Turn! (Unga neram!)
Oru if statement ezhudhunga. Variable age 18 illana adhuku mela irundha, "You can vote" nu print pannanum.