0% found this document useful (0 votes)
26 views6 pages

Python Conditional Statements - Jupyter Notebook

The document explains conditional statements in Python, focusing on if, elif, and else statements for decision-making based on conditions. It emphasizes the importance of indentation, provides examples for each type of statement, and discusses when to omit the else block. Additionally, it covers nested conditionals and the use of the pass statement as a placeholder in code.

Uploaded by

gihananjulayt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views6 pages

Python Conditional Statements - Jupyter Notebook

The document explains conditional statements in Python, focusing on if, elif, and else statements for decision-making based on conditions. It emphasizes the importance of indentation, provides examples for each type of statement, and discusses when to omit the else block. Additionally, it covers nested conditionals and the use of the pass statement as a placeholder in code.

Uploaded by

gihananjulayt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Conditional Statements in Python

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:

The if keyword is followed by a condition.


If the condition evaluates to True , the code block indented below the if statement is
executed.
If the condition evaluates to False , the code block is skipped.

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.")

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.

For example, if statement, without indentation (will raise an error):


In [2]: num = 10
if num > 0:
print(num, "is a positive number.")

Cell In[2], line 3


print(num, "is a positive number.")
^
IndentationError: expected an indented block

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:

The else keyword is used after an if statement.


If the if condition is False , the code block indented below the else statement is
executed.

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.")

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 are an adult.

When to Omit else

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:

Children under 4 years old get free entry.


Children and teenagers aged 4 to 17 pay Rs.25 for admission.
Adults aged 18 to 64 pay Rs.40 for admission.
Senior citizens aged 65 and above pay a discounted price of Rs.20.
In [5]: age = 12

if age < 4:
price = 0
elif age < 18:
price = 25
elif age < 65:
price = 40
elif age >= 65:
price = 20

print("Your admission cost is",price, "rupees.")

Your admission cost is 25 rupees.

Nested Conditional Statements


You can nest conditional statements within each other to create more complex decision-making
structures.

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).

In [6]: location = "domestic" # Other possible value: "international"


membership_status = "premium" # Other possible value: "regular"

if location == "domestic":
if membership_status == "premium":
print("Shipping is free.")
else:
print("Shipping cost is $5 for domestic regular members.")
else:
if membership_status == "premium":
print("Shipping cost is $10 for international premium members.")
else:
print("Shipping cost is $20 for international regular members.")

Shipping is free.

Test the code for the following cases:

For a domestic regular member


For an international premium member
For an international regular member
The pass statement

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.

Common Use Cases:

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.

In [7]: def my_function():


pass # Placeholder for future code

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.

In [8]: for i in range(5):


pass # Loop through, but do nothing

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 .

In [10]: class MyClass:


pass # Class definition with no implementation yet

Why 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.

You might also like