Python Conditional Statements - Jupyter Notebook
Python Conditional Statements - Jupyter Notebook
Conditional statements in Python allow you to make decisions in your code based on
conditions (True/False). The most common conditional statements in Python are if , elif ,
and else .
If Statements
The simplest conditional statement is the if statement. It executes a block of code only if a
specified condition is True .
Syntax:
if condition:
# Code to execute if condition is True
Explanation:
Example: The Python code below checks whether a person is 18 years old or older and prints
a message if the condition is met.
In [1]: age = 18
if age >= 18:
print("You are an adult.")
Indentation
Indentation is crucial in Python as it determines the structure and execution flow of your code.
It's used to define code blocks and indicate the scope of conditional statements, loops, and
functions.
Else Statements
The else statement can be used in conjunction with an if statement to execute a different
block of code if the condition is False .
Syntax:
if condition:
# Code to execute if condition is True
else:
# Code to execute if condition is False
Explanation:
Example: The Python code below checks whether a person is an adult or a minor based on
their age and prints the appropriate message.
In [3]: age = 17
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
Elif Statements
The elif statement (short for "else if") allows you to check multiple conditions. It can be used
in combination with if and else statements.
Syntax:
if condition1:
# Code to execute if condition1 is True
elif condition2:
# Code to execute if condition1 is False and condition2 is True
else:
# Code to execute if both conditions are False
Explanation:
The elif keyword is used to check additional conditions if the previous if or elif
conditions are False .
Multiple elif statements can be used in sequence.
The else statement is optional and is executed if none of the previous if or elif
conditions are True .
Example: The code below demonstrates the use of if , elif , and else statements to
determine a person's age group based on their age.
In [4]: age = 25
if age < 18:
print("You are a minor.")
elif age >= 18 and age < 65:
print("You are an adult.")
else:
print("You are a senior citizen.")
You can often omit the else block in Python conditional statements when the if and elif
conditions collectively cover all possible cases. This means that one of the conditions must be
True for any given input.
Example: Imagine you are managing ticket prices at a theme park where admission prices are
structured as follows:
Example: an online store offers free shipping for premium members within certain locations.
Regular members have different shipping costs based on their location. Here's how nested
conditionals can be used to decide the shipping cost based on a customer's location (domestic
or international) and their membership status (premium or regular).
Shipping is free.
The pass statement is a placeholder in Python that does nothing. It's often used in situations
where you need a statement but don't have any code to execute yet.
1. Placeholder for Future Code: When you're writing the structure of a function, loop, or
class but haven't yet implemented the logic, you can use pass to avoid syntax errors.
2. In Loops: You can use pass in loops where the logic might not be needed yet or you
want to bypass a condition without writing actual code.
3. In Conditional Statements: pass can be used in conditional branches where you don't
need any action for certain conditions.
In [9]: x = 10
if x > 5:
pass # Do nothing when x > 5
else:
print("x is 5 or less")
4. In Class Definitions: When defining a class, if you don’t want to add any methods or
properties immediately, you can use pass .
Without the pass statement, Python will raise an error if a block of code (like a function or
loop) is syntactically required but empty. pass solves this issue by allowing the program to run
without doing anything.