USD ($)
$
United States Dollar
Euro Member Countries
India Rupee
د.إ
United Arab Emirates dirham
ر.س
Saudi Arabia Riyal

Conditional Statements (if, elif, else)

Lesson 9/35 | Study Time: 60 Min

In real life, decisions are made based on conditions — if it rains, carry an umbrella; otherwise, you are fine without one.

Python works the same way. Conditional statements allow a program to make decisions and execute different blocks of code based on whether a condition is true or false. They are one of the most fundamental and frequently used tools in programming.

The if Statement

The if statement checks a condition. If the condition is True, the indented block of code beneath it runs. If it is False, Python skips it.


Syntax:


Example:



The else Statement

The else statement provides an alternative block of code that runs when the if condition is False. Think of it as the fallback option.

Syntax:


Example:




The elif Statement

elif stands for else if. It allows you to check multiple conditions in sequence. Python evaluates each condition from top to bottom and executes the first one that is True.

Syntax:


Example:




How Python Evaluates Conditions

Python checks conditions from top to bottom and stops as soon as one is True. The remaining conditions are skipped entirely.


This behavior is important to keep in mind when ordering your conditions.


Nested Conditional Statements

You can place an  if statement inside another if block. This is called nesting, and it is useful when a decision depends on multiple layers of conditions.

Use nesting carefully — too many levels make code hard to read.

Shorthand if — One-Line Conditionals

For simple conditions, Python allows a compact one-line version called a ternary expression.

This is clean and readable for simple true/false decisions, but avoid using it for complex logic.

Common Mistakes to Avoid

Sales Campaign

Sales Campaign

We have a sales campaign on our promoted courses and products. You can purchase 1 products at a discounted price up to 15% discount.