๐น 1. What are Conditional Statements?
Conditional statements allow a program to make decisions.
๐ Python checks a condition
๐ If it is True, one block runs
๐ If it is False, another block runs
Python uses:
ifelifelse
๐น 2. Basic if Statement
Example 1: Simple Condition
age = 20
if age >= 18:
print("Eligible to vote")
Output
Eligible to vote
๐ Explanation
- Condition:
age >= 18 - Result is
True - Code inside
ifruns - Indentation is mandatory
๐น 3. if Condition Fails
age = 15
if age >= 18:
print("Eligible to vote")
Output
(no output)
๐ Explanation
- Condition is
False - Python skips the
ifblock
๐น 4. if โ else Statement
Example 2: Voting Eligibility
age = 16
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
Output
Not eligible to vote
๐ Explanation
- If condition is
Trueโifruns - If condition is
Falseโelseruns
๐น 5. if โ elif โ else Statement
Used when there are multiple conditions.
Example 3: Student Grade System
marks = 82
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 60:
print("Grade C")
else:
print("Fail")
Output
Grade B
๐ Explanation
- Python checks conditions top to bottom
- First
Truecondition runs - Remaining conditions are skipped
๐น 6. Conditions Using Logical Operators
Example 4: Admission Eligibility
age = 20
has_id = True
if age >= 18 and has_id:
print("Admission allowed")
else:
print("Admission denied")
Output
Admission allowed
๐ Explanation
andโ both conditions must be true
Example 5: Scholarship Eligibility
marks = 85
income = 20000
if marks >= 80 or income <= 25000:
print("Eligible for scholarship")
else:
print("Not eligible")
Output
Eligible for scholarship
๐น 7. Nested if (if inside if)
Example 6: Login System
username = "Sameer"
password = "1234"
if username == "Sameer":
if password == "1234":
print("Login successful")
else:
print("Wrong password")
else:
print("Wrong username")
Output
Login successful
๐ Explanation
- Inner
ifruns only if outerifis true
๐น 8. if with User Input (VERY IMPORTANT)
Example 7: Even or Odd Number
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even number")
else:
print("Odd number")
Sample Output
Enter a number: 7
Odd number
๐น 9. Real-Life Example: Salary Bonus
Example 8
salary = float(input("Enter salary: "))
if salary >= 30000:
bonus = 5000
else:
bonus = 2000
print("Bonus:", bonus)
Sample Output
Enter salary: 28000
Bonus: 2000
๐น 10. Comparison of Strings
Example 9
name = "Ayesha"
if name == "Ayesha":
print("Welcome Ayesha")
else:
print("Unknown user")
Output
Welcome Ayesha
๐น 11. Multiple Conditions Practice
Example 10: Age Group
age = 65
if age < 13:
print("Child")
elif age < 20:
print("Teenager")
elif age < 60:
print("Adult")
else:
print("Senior Citizen")
Output
Senior Citizen
๐น 12. Common Mistakes (IMPORTANT)
โ Missing indentation
โ Using = instead of ==
โ Forgetting type casting for input
โ Wrong condition order
๐ง Final Summary
โ if checks condition
โ else runs when condition fails
โ elif handles multiple conditions
โ Python uses indentation instead of brackets
โ Conditions return True or Falsehen condition fails
โ elif handles multiple conditions
โ Python uses indentation instead of brackets
โ Conditions return True or False

Leave a Reply