0% found this document useful (0 votes)
2 views

Lecture05-1

This document explains Python's conditional structures: if, elif, and else, detailing their usage with strings and lists. It provides examples demonstrating how to check conditions and print messages based on the evaluation of those conditions. The document emphasizes the importance of these structures for controlling program flow.

Uploaded by

nazish.z.nk
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)
2 views

Lecture05-1

This document explains Python's conditional structures: if, elif, and else, detailing their usage with strings and lists. It provides examples demonstrating how to check conditions and print messages based on the evaluation of those conditions. The document emphasizes the importance of these structures for controlling program flow.

Uploaded by

nazish.z.nk
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

Python Conditional Structures: if, elif, else

1 Introduction
In Python, conditional statements allow you to execute certain blocks of code
based on whether a condition is true or false. The primary conditional structures
are:

ˆ if - Executes a block of code if the condition is true.

ˆ else - Executes a block of code if the condition is false.

ˆ elif - Checks additional conditions after the initial if condition.

These structures can be applied to various data types, including strings and
lists. This document explains how to use these structures effectively in Python
with examples involving strings and lists.

2 Basic Example with Strings


2.1 Problem Description
You are given a string variable word. Your task is to check whether the value
of word is equal to the string ”hello”. If it is, print the message ”The word is
hello!”. If it is not, print the message ”The word is not hello.”

2.2 Code
The following code demonstrates how to use a basic if-else statement to check
the condition:

1. word = "hello"
2. if word == "hello":
3. print("The word is hello!")

4. else:
5. print("The word is not hello.")

1
2.3 Explanation
In this example:

ˆ The if statement checks if the string word is equal to ”hello”.

ˆ If the condition is true, it prints "The word is hello!".

ˆ If the condition is false, it prints "The word is not hello.".

3 Using elif with Strings


3.1 Problem Description
You are given a string variable word. You need to check multiple possible values
for word. First, check if word is equal to ”banana”. If it is, print ”The word is
banana”. If it is not, check if word is equal to ”apple”. If it is, print ”The word
is apple”. If neither of these conditions are met, print a message saying ”The
word is neither banana nor apple”.

3.2 Code
The following code demonstrates how to use elif to check multiple conditions:

1. word = "apple"
2. if word == "banana":

3. print(”The word is banana.”)


4. elif word == "apple":
5. print("The word is apple.")
6. else:

7. print("The word is neither banana nor apple.")

3.3 Explanation
In this case:

ˆ The if statement first checks if word is equal to ”banana”.

ˆ If not, the elif statement checks if word is equal to ”apple”.

ˆ If neither condition is true, the else block executes and prints the message
that the word is neither ”banana” nor ”apple”.

2
4 Example with Lists
4.1 Problem Description
You are given a list of fruits, fruits, and a string variable fruit. You need
to check if fruit is in the list fruits. If it is, print a message saying that the
fruit is in the list. If it is not, print a message saying that the fruit is not in the
list.

4.2 Code
The following code demonstrates how to use in with an if-else statement to
check membership in a list:

1. fruits = ["apple", "banana", "cherry"]


2. fruit = "banana"

3. if fruit in fruits:
4. print(f"fruit is in the list of fruits.")
5. else:
6. print(f"fruit is not in the list of fruits.")

4.3 Explanation
In this case:
ˆ The if statement checks if fruit is present in the list fruits.

ˆ If the fruit is found in the list, the first block of code executes, printing
that the fruit is in the list.
ˆ Otherwise, the else block executes and prints that the fruit is not in the
list.

5 Using elif with Lists


5.1 Problem Description
You are given a list of fruits, fruits, and a string variable fruit. You need to
check if fruit is one of the specific fruits in the list (e.g., ”apple”, ”banana”,
or ”cherry”). If it is any of those, print a corresponding message. If it is none
of those, print that the fruit is not in the list.

3
5.2 Code
The following code demonstrates how to use multiple elif statements with a
list:

1. fruits = ["apple", "banana", "cherry"]


2. fruit = "grape"

3. if fruit == "apple":
4. print("Apple is in the list.")
5. elif fruit == "banana":

6. print("Banana is in the list.")


7. elif fruit == "cherry":
8. print("Cherry is in the list.")
9. else:

10. print(f"fruit is not in the list of fruits.")

5.3 Explanation
In this example:

ˆ The if statement checks if fruit is equal to ”apple”.

ˆ If not, the elif statements check for ”banana” and ”cherry”.

ˆ If none of these conditions are true, the else block prints that the fruit is
not in the list.

6 Combining Multiple Conditions


6.1 Problem Description
You are given a list of fruits and two variables: fruit (a string) and color (also
a string). Check if fruit is in the list of fruits fruits, and whether color is
equal to ”yellow”. If both conditions are true, print that the fruit is yellow. If
either condition is false, print a different message.

4
6.2 Code
The following code demonstrates how to combine conditions using and:

1. fruits = ["apple", "banana", "cherry"]


2. fruit = "banana"
3. color = "yellow"
4. if fruit in fruits and color == "yellow":
5. print(f"fruit is yellow!")
6. else:
7. print(f"Either fruit is not in the list or it’s not yellow.")

6.3 Explanation
Here:
ˆ The condition fruit in fruits checks if fruit is in the list fruits.
ˆ The condition color == "yellow" checks if the color is ”yellow”.
ˆ The and operator ensures that both conditions must be true for the first
block to execute. If either condition is false, the else block executes.

7 Nested Conditional Statements


7.1 Problem Description
You are given a list of fruits and two variables: fruit and color. First, check
if the fruit is in the list of fruits. If it is, check whether the color is ”yellow”.
Based on these conditions, print different messages.

7.2 Code
The following code demonstrates how to nest if statements:

1. fruits = ["apple", "banana", "cherry"]


2. fruit = "banana"
3. color = "yellow"
4. if fruit in fruits:
5. if color == "yellow":
6. print(f"fruit is yellow!")

5
7. else:
8. print(f"fruit is not yellow.")
9. else:

10. print(f"fruit is not in the list of fruits.")

7.3 Explanation
In this example:

ˆ The outer if checks if fruit is in the list fruits.

ˆ If true, the inner if checks if the color is ”yellow”.

ˆ If either condition fails, the respective else blocks execute.

8 Conclusion
In this document, we explored the if, elif, and else conditional structures in
Python, with examples using strings and lists. These structures are crucial for
controlling the flow of a program and are versatile for handling various types of
conditions in Python.

You might also like