Learn Python in 30 Days - Day 4: Conditional Statements (if, elif, else)

Rifat Seniabad
By -
0

 Learn Python in 30 Days - Day 4: Conditional Statements (if, elif, else)



Welcome to Day 4! Today, we’ll explore conditional statements in Python, which allow your program to make decisions based on certain conditions. This is a fundamental concept in programming, so let’s master it step by step with examples!


1. What are Conditional Statements?

Conditional statements control the flow of a program by executing code blocks only when specific conditions are met.

Syntax:

if condition:
    # Code to execute if condition is True
elif another_condition:
    # Code to execute if another_condition is True
else:
    # Code to execute if all conditions are False

2. The if Statement

The if statement checks a condition and runs the code block if the condition evaluates to True.

Example:

age = 18

if age >= 18:
    print("You are eligible to vote.")

Output:

You are eligible to vote.

3. The if-else Statement

Use if-else to specify an alternative block of code to run when the condition is False.

Example:

num = 10

if num % 2 == 0:
    print("The number is even.")
else:
    print("The number is odd.")

Output:

The number is even.

4. The if-elif-else Statement

Use if-elif-else for multiple conditions.

Example:

marks = 75

if marks >= 90:
    print("Grade: A+")
elif marks >= 80:
    print("Grade: A")
elif marks >= 70:
    print("Grade: B")
else:
    print("Grade: F")

Output:

Grade: B

5. Nested if Statements

You can use one if statement inside another for more complex conditions.

Example:

num = 15

if num > 0:
    if num % 2 == 0:
        print("The number is positive and even.")
    else:
        print("The number is positive and odd.")
else:
    print("The number is not positive.")

Output:

The number is positive and odd.

6. Logical Operators with Conditions

You can combine multiple conditions using logical operators (and, or, not).

Example:

age = 25
income = 40000

if age > 18 and income > 30000:
    print("You are eligible for the loan.")
else:
    print("You are not eligible for the loan.")

Output:

You are eligible for the loan.

7. Practice Tasks for Day 4

Task 1: Check Pass or Fail

Write a program that checks if a student passed or failed based on their marks:

marks = 50

if marks >= 40:
    print("You passed!")
else:
    print("You failed.")

Task 2: Age Group Checker

Write a program to determine if a person is a child, teenager, or adult:

age = 16

if age < 13:
    print("You are a child.")
elif age < 20:
    print("You are a teenager.")
else:
    print("You are an adult.")

Task 3: Traffic Light Simulator

Write a program to simulate a traffic light:

light = "green"

if light == "red":
    print("Stop!")
elif light == "yellow":
    print("Get ready to stop.")
elif light == "green":
    print("Go!")
else:
    print("Invalid light signal.")

8. Common Mistakes to Avoid

  1. Indentation Errors:
    Python relies on indentation to define code blocks. Ensure consistent spacing (e.g., 4 spaces).

    if True:
    print("Hello")  # Incorrect
        print("Hello")  # Correct
    
  2. Using = Instead of ==:
    Remember, = is for assignment, and == is for comparison.

  3. Forgetting the Colon (:):
    Every if, elif, and else must end with a colon.


9. Pro Tips for Day 4

  • Test your code with different inputs to ensure all conditions are covered.
  • Use comments to explain complex conditions.
  • Start combining what you’ve learned so far to build mini-programs.

🎯 Day 4 Goal: Write at least 3 programs using if, elif, and else statements. Understand how conditional statements work with logical operators.

Let me know when you’re ready for Day 5! 🚀

Tags:

Post a Comment

0Comments

Post a Comment (0)