TGViewer
Data Science & Machine Learning Data Science & Machine Learning @datasciencefun ยท 77.8K subscribers
Post #4433 3.72K
๐Ÿš€ Data Science Roadmap 2026

๐Ÿ“˜ Phase 1: Programming Fundamentals

๐Ÿ Topic 4: Python Conditional Statements (if, elif, else)

Welcome back! ๐Ÿ‘‹

In the previous lesson, you learned how to take input from users and display output. Now it's time to learn how Python makes decisions.

Conditional statements allow a program to execute different blocks of code based on whether a condition is True or False.

They are one of the most important concepts in Python and are used extensively in Data Science, Machine Learning, and real-world applications.

๐Ÿ”น 1. What are Conditional Statements?

Conditional statements help Python decide which code should run based on a condition.

Think of it like making everyday decisions:

โ€ข If it's raining โ†’ Carry an umbrella.

โ€ข Else โ†’ Wear sunglasses.

Similarly, Python checks a condition and decides what to execute.

๐Ÿ”น 2. The "if" Statement

The "if" statement executes a block of code only if the condition is True.

Syntax

if condition:
# Code to execute


Example

age = 20
if age >= 18:
print("Eligible to vote")


Output

Eligible to vote


๐Ÿ”น 3. The "if...else" Statement

Use "else" when you want to execute another block if the condition is False.

Example

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


Output

Not eligible to vote


๐Ÿ”น 4. The "if...elif...else" Statement โญ

Use "elif" when you need to check multiple conditions.

Example

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


Output

Grade B


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

๐Ÿ”น 5. Nested "if" Statements

You can place one "if" statement inside another.

Example

age = 25
citizen = True
if age >= 18:
if citizen:
print("Eligible to vote")


Output

Eligible to vote


๐Ÿ”น 6. Using Logical Operators

Conditional statements often use logical operators.

"and"

age = 25
if age >= 18 and age <= 60:
print("Working Age")


"or"

marks = 35
if marks >= 40 or marks == 35:
print("Eligible for Grace Marks")


"not"

is_holiday = False
if not is_holiday:
print("Go to Office")


๐Ÿ”น 7. Checking Multiple Conditions

salary = 60000
experience = 4
if salary > 50000 and experience >= 3:
print("Eligible for Promotion")
else:
print("Not Eligible")


๐Ÿ”น 8. Ternary Operator โญ

A shorter way to write an "if...else" statement.

Syntax

value_if_true if condition else value_if_false


Example

age = 20
status = "Adult" if age >= 18 else "Minor"
print(status)


Output

Adult


๐Ÿ”น 9. Real-World Example

temperature = 38
if temperature > 35:
print("It's a hot day.")
elif temperature >= 20:
print("Weather is pleasant.")
else:
print("It's cold.")


๐Ÿ”น 10. Common Mistakes

โŒ Missing Colon

if age >= 18
print("Eligible")


This gives a SyntaxError.

Correct:
  • โค 5
  • ๐Ÿฅฐ 1
  • ๐Ÿ˜ 1
More from @datasciencefun
  1. Oct 7, 2026ORDER BY salary DESC LIMIT 5; Mistake 3 โ€” Forgetting that LIMIT applies after sorting Forโ€ฆ
  2. Oct 7, 2026๐Ÿš€ Data Science Roadmap 2026 ๐Ÿ“ Phase 3: SQL for Data Science ๐Ÿ“– Topic 4 โ€” LIMIT LIMIT isโ€ฆ
  3. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  4. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  5. Oct 2, 2026Data Visualisation tips for beginners
  6. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
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 โ†’