TGViewer
Artificial Intelligence Artificial Intelligence @machinelearning_deeplearning · 56.3K subscribers
Post #1862 1.85K
In the previous post, we explored Python operators and even tested ourselves with some tricky questions. Now, let's learn how Python makes decisions using Conditional Statements.

📖 Phase 1: Programming Fundamentals 
📌 Topic 9: Conditional Statements (if, elif, else)

Conditional statements allow a program to make decisions based on whether a condition is "True" or "False".

Think about a real-life decision: 
👉 If it is raining → Take an umbrella. ☔ 
👉 Otherwise → Don't take an umbrella.

Programming works in a similar way.

Why Do We Need Conditional Statements? 
They allow programs to:
• Make decisions
• Execute different blocks of code
• Validate user input
• Control program behavior
• Handle different scenarios

1. The "if" Statement 
The "if" statement executes a block of code only when a condition is "True".

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

Output: You are eligible to vote.

If the condition is "False", the code inside the "if" block will not execute.

Important: Indentation 
Python uses indentation to define blocks of code.

Correct:
age = 25
if age >= 18:
    print("Eligible")

Incorrect:
age = 25
if age >= 18:
print("Eligible")

The second example will produce an indentation error.

2. The "else" Statement 
"else" executes when the "if" condition is "False".

Example:
age = 16
if age >= 18:
    print("Eligible to vote")
else:
    print("Not eligible to vote")

Output: Not eligible to vote

Think of it as: If condition is true → Do this. Otherwise → Do that.

3. The "elif" Statement 
"elif" means "else if". It allows you to check multiple conditions.

Example:
marks = 75
if marks >= 90:
    print("Grade A+")
elif marks >= 75:
    print("Grade A")
elif marks >= 60:
    print("Grade B")
else:
    print("Grade C")

Output: Grade A

Python checks the conditions from top to bottom and executes the first condition that is "True".

4. Multiple Conditions 
You can combine conditions using logical operators.

Example:
age = 25
has_id = True
if age >= 18 and has_id:
    print("Access granted")
else:
    print("Access denied")

Output: Access granted

5. Nested "if" Statements 
An "if" statement can be placed inside another "if" statement.

Example:
age = 25
country = "India"
if age >= 18:
    if country == "India":
        print("Eligible")

Nested conditions are useful when one decision depends on another.

6. Short-Hand "if" 
For simple conditions, Python allows a one-line "if".
age = 25
if age >= 18: print("Adult")

Output: Adult

Common Beginner Mistakes 
❌ Forgetting the colon ":" after "if", "elif", and "else" 
❌ Using incorrect indentation 
❌ Using "=" instead of "==" for comparison 
❌ Writing conditions in the wrong order

Example of wrong order:
marks = 95
if marks >= 60:
    print("Grade B")
elif marks >= 90:
    print("Grade A+")

Output: Grade B 

Why? Because Python stops at the first condition that is true.

Correct order:
if marks >= 90:
    print("Grade A+")
elif marks >= 60:
    print("Grade B")

Real-World AI Example 
Conditional statements are also used in AI applications.
confidence = 0.92
if confidence >= 0.90:
    print("High confidence prediction")
elif confidence >= 0.70:
    print("Medium confidence prediction")
else:
    print("Low confidence prediction")

Output: High confidence prediction 

This type of logic can be used alongside Machine Learning models to decide what action to take based on a prediction or confidence score.

Key Takeaways 
• "if" checks a condition
• "elif" checks additional conditions
• "else" handles everything that doesn't match the previous conditions
• Python uses indentation to define code blocks
• Conditions can be combined using "and", "or", and "not"
• Python executes the first matching condition in an "if/elif/else" chain

➡️ Double Tap ❤️ For More
  • ❤ 13
More from @machinelearning_deeplearning
  1. Sep 22, 2026🚀 Welcome back to our AI Engineer Roadmap! ❤️ In the previous posts, we learned about fun…
  2. Sep 21, 2026🚀 Welcome back to our AI Engineer Roadmap! ❤️ In the previous post, we explored functions…
  3. Sep 20, 2026In the previous post, we learned how conditional statements allow Python programs to make…
  4. Sep 18, 2026In the previous post, we learned how to convert one data type into another using Type Cast…
  5. Sep 17, 2026In the previous post, we learned how to take input from users and display output. One impo…
  6. Sep 16, 2026🚀 GigaChat 3.5 Reasoning — a new open-source LLM that thinks before it answers. It breaks…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →