Lesson 1: Conditional Statements

Conditional statements are a fundamental concept in programming, allowing you to control the flow of execution based on certain conditions. Python provides several tools to perform decision-making in your code, including if, elif, else, nested conditionals, and logical operators. This lesson will guide you through using these statements effectively.

1. if, elif, and else Statements

Conditional statements in Python are structured around the if, elif, and else keywords, which allow you to test conditions and choose different paths of execution based on whether the condition is true or false.

1.1. if Statement

The if statement is used to check if a certain condition is true. If the condition evaluates to True, the block of code inside the if statement is executed.

Basic Syntax:

python

if condition:

# code to execute if condition is true

 

Example:

python
age = 18

if age >= 18:

print(“You are an adult.”)

 

In this example, the condition checks if age is greater than or equal to 18. Since age is 18, the statement evaluates to True, and the code inside the if block is executed, printing “You are an adult.”.

1.2. elif Statement

The elif (short for “else if”) statement is used to check multiple conditions. If the condition in the if statement is false, the program checks the condition of each elif statement in sequence.

Syntax:

python

if condition1:

# code to execute if condition1 is true

elif condition2:

# code to execute if condition2 is true

 

You can have multiple elif statements to handle more than one condition.

Example:

python
age = 20

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

 

In this example, the first condition checks if age is less than 18. If it’s false, the elif condition checks if the person is an adult (i.e., between 18 and 65 years old). If that’s false, the else statement is executed, indicating that the person is a senior citizen.

1.3. else Statement

The else statement is used to define a block of code that will be executed if none of the preceding if or elif conditions are True.

Syntax:

python
if condition1:

# code to execute if condition1 is true

elif condition2:

# code to execute if condition2 is true

else:

# code to execute if none of the above conditions are true

 

Example:

python
temperature = 75

if temperature > 85:

print(“It’s too hot outside.”)

elif temperature > 65:

print(“The weather is perfect.”)

else:

print(“It’s a bit chilly outside.”)

 

Here, the program checks for different temperature ranges, and if none of the conditions are met, the else statement is executed.

2. Nested Conditionals

A nested conditional is a conditional statement placed inside another conditional statement. This allows for more complex decision-making, where you can check multiple conditions in a hierarchical manner.

Syntax:

python

if condition1:

if condition2:

# code if both conditions are true

else:

# code if condition1 is true, but condition2 is false

else:

# code if condition1 is false

 

Example:

python
age = 20

has_ticket = True

 

if age >= 18:

if has_ticket:

print(“You can enter the concert.”)

else:

print(“You cannot enter the concert without a ticket.”)

else:

print(“You are too young to enter the concert.”)

 

In this example, there is a nested if condition. If the person is 18 or older, the program checks whether they have a ticket. If both conditions are true, they are allowed to enter the concert.

3. Logical Operators (and, or, not)

Logical operators are used to combine multiple conditions and create more complex decision-making.

3.1. and Operator

The and operator returns True only if both conditions are True. If either condition is False, the entire expression will evaluate to False.

Example:

python
age = 20

has_ticket = True

 

if age >= 18 and has_ticket:

print(“You are allowed entry.”)

else:

print(“You cannot enter.”)

 

In this example, both conditions (age >= 18 and has_ticket) must be true for the message “You are allowed entry.” to be printed. If either condition is False, the else block will execute.

3.2. or Operator

The or operator returns True if at least one of the conditions is True. It only returns False if both conditions are False.

Example:

python

age = 20

has_ticket = False

 

if age >= 18 or has_ticket:

print(“You can enter the event.”)

else:

print(“You cannot enter.”)

 

In this example, the person will be allowed entry if they are 18 or older, or if they have a ticket. Since age >= 18 is True, the message “You can enter the event.” will be printed, even though has_ticket is False.

3.3. not Operator

The not operator is used to negate a condition. It returns True if the condition is False, and False if the condition is True.

Example:

python

age = 16

 

if not age >= 18:

print(“You are not allowed to enter.”)

else:

print(“You are allowed to enter.”)

 

Here, the condition not age >= 18 evaluates to True because age is less than 18. As a result, the message “You are not allowed to enter.” will be printed.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *