What-is-IF-ELSE?

The `IF ELSE` structure is fundamental in Python for controlling the flow of your program based on conditions. By understanding this concept, you can create more dynamic and responsive applications.

In Python, `IF ELSE` is a conditional statement that allows you to execute different blocks of code based on whether a condition is true or false. It helps in making decisions in your code.

Basic Structure

The syntax looks like this:

if condition:
    # code to execute if condition is true
else:
    # code to execute if condition is false

Why is it Useful ?

Using IF ELSE helps us make decisions quickly. Just like in our examples, it tells us what to do based on the situation!

Summary

- IF is a question or a condition.
- ELSE is what to do if that condition isn’t true.

Now, you can think of it like a fun game of choices! Want to try creating your own IF ELSE scenario?

Absolutely! Let's dive deeper into the **IF ELSE** concept in Python, focusing on its structure, usage, and practical applications.

Examples on (if-elif-else) conditions in python

Example 1: Traffic Light System

Imagine a traffic light:

- IF the light is green, cars go.
- ELSE if the light is red, cars stop.

light_color = "green"
if light_color == "green":
    print ("Cars go!")
else:
    print ("Cars stop!")

Example 2: Weather-Based Activity

Suppose you plan your day based on the weather:

- IF it’s sunny, you go for a picnic.
- ELSE if it’s rainy, you stay indoors and read a book.

weather = "rainy"
if weather == "sunny":
    print("Let’s go for a picnic!")
else:
    print("Let’s stay indoors and read.")

Example 3: Grading System

Consider a grading system based on scores:

- IF the score is 50 or above, you pass.
- ELSE you fail.

score = 45
if score >= 50:
    print ("You passed!")
else:
    print ("You failed.")

what is elif condition in python...?

Sometimes, you have multiple conditions to check. You can use `elif` (short for "else if") to check additional conditions.

Example 4: Grade Classification

- IF the score is 90 or above, it’s an A.
- ELIF the score is 80 or above, it’s a B.
- ELSE (for all other scores), it’s a C.

score = 85
if score >= 90:
    print ("Grade: A")
elif score >= 80:
    print ("Grade: B")
else:
    print ("Grade: C")

KeyPoints to Remember ?

1. Boolean Conditions: Conditions can be comparisons (like `==`, `!=`, `>`, `<`, etc.) or Boolean expressions.
2. Indentation Matters: Python uses indentation to define blocks of code. Ensure consistent indentation for the code under `if`, `elif`, and `else`.
3. Nested Conditions: You can have `if` statements inside other `if` statements for more complex decision-making.