• Discover
  • Collections
  • Board
  • Create
  • Profile
  • Settings
Paths

Lesson 3.1: Conditional Statements: `if`, `elif`, `else`

Lesson 3.1: Conditional Statements: `if`, `elif`, `else`

3 min read

Conditional statements allow your program to make decisions based on whether a certain condition is true or false. This is a fundamental concept for creating dynamic and responsive programs. Python uses the if, elif (else if), and else keywords for this purpose.

The if Statement

The if statement is the simplest form of a conditional. It executes a block of code only if its condition is True.

Syntax:

python
if condition: # Code to execute if condition is True statement_1 statement_2

Example:

python
age = 20 if age >= 18: print("You are old enough to vote.") # Output: # You are old enough to vote.

The if-else Statement

The if-else statement allows you to execute one block of code if the condition is True, and a different block if the condition is False.

Syntax:

python
if condition: # Code to execute if condition is True else: # Code to execute if condition is False

Example:

python
temperature = 15 if temperature > 25: print("It's a hot day!") else: print("It's not too hot today.") # Output: # It's not too hot today.

The if-elif-else Statement

When you have multiple conditions to check, the if-elif-else chain is used. Python evaluates the conditions in order from top to bottom. The first condition that evaluates to True will have its corresponding block executed, and the rest of the elif and else blocks will be skipped.

Syntax:

python
if condition1: # Code to execute if condition1 is True elif condition2: # Code to execute if condition1 is False AND condition2 is True elif condition3: # Code to execute if condition1, condition2 are False AND condition3 is True else: # Code to execute if all conditions above are False

Example:

python
score = 85 if score >= 90: print("Grade: A") elif score >= 80: print("Grade: B") elif score >= 70: print("Grade: C") elif score >= 60: print("Grade: D") else: print("Grade: F") # Output: # Grade: B

Important Considerations:

  • Indentation: Remember that indentation is crucial in Python. The code blocks under if, elif, and else must be consistently indented (usually 4 spaces).

  • Boolean Context: The conditions in if statements are evaluated in a Boolean context. Any non-zero number, non-empty string, list, or dictionary is considered True. 0, None, empty strings (""), empty lists ([]), and empty dictionaries ({}) are considered False.

    python
    # Example of Boolean context name = "" if name: print("Name is not empty") else: print("Name is empty") # Output: Name is empty count = 0 if count: print("Count is not zero") else: print("Count is zero") # Output: Count is zero

Conditional statements are the backbone of decision-making in your programs. Mastering if, elif, and else allows your code to respond intelligently to different inputs and scenarios.

Key Takeaways:

  • if executes code if a condition is True.
  • else provides an alternative block for when the if condition is False.
  • elif allows checking multiple conditions sequentially.
  • Indentation defines code blocks for conditionals.
  • Conditions are evaluated in a Boolean context; non-empty/non-zero values are True, empty/zero values are False.
End of lesson
👏Well done!
Previous Lesson
Quiz: Module 2: Variables, Data Types, and Operators
Next Lesson
Lesson 3.2: Loops: `for` Loop and `range()` function

Course Content

0% Complete0/22 Lessons

Lesson 1.1: What is Python? History, Features, and Applications

Lesson 1.2: Setting Up Your Python Environment

Lesson 1.3: Your First Python Program: "Hello, World!" and Basic Syntax

Quiz

Lesson 3.1: Conditional Statements: `if`, `elif`, `else`

Lesson 3.2: Loops: `for` Loop and `range()` function

Lesson 3.3: Loops: `while` Loop and Loop Control (`break`, `continue`)

Quiz

Course Content

0% Complete0/22 Lessons

Lesson 1.1: What is Python? History, Features, and Applications

Lesson 1.2: Setting Up Your Python Environment

Lesson 1.3: Your First Python Program: "Hello, World!" and Basic Syntax

Quiz

Lesson 3.1: Conditional Statements: `if`, `elif`, `else`

Lesson 3.2: Loops: `for` Loop and `range()` function

Lesson 3.3: Loops: `while` Loop and Loop Control (`break`, `continue`)

Quiz