๐Ÿ“˜ Lesson 5 examples: Conditional Statements

๐Ÿ”น 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:

  • if
  • elif
  • else

๐Ÿ”น 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 if runs
  • 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 if block

๐Ÿ”น 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 โ†’ if runs
  • If condition is False โ†’ else runs

๐Ÿ”น 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 True condition 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 if runs only if outer if is 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


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *