Lesson 2.3: Selection: Making Decisions
Introduction
Welcome to Lesson 2.3 of Foundation Computing! In this lesson, we will dive into the concept of selection in programming using Python. 🌟 By the end of this lesson, you should be able to understand and implement decision-making in your code effectively.
Learning Objectives:
- Understand Boolean expressions and comparison operators.
- Utilize if, if–else, and if–elif–else structures, including nesting decisions.
- Combine conditions using the logical operators: and, or, and not.
- Design decision logic based on specifications and trace which branch executes.
- Write selection statements to choose between alternative actions correctly.
What are Boolean Expressions?
In programming, a Boolean expression is an expression that evaluates to either true or false. 🤔 This is fundamental because decisions in programming are based on the outcomes of these expressions.
Comparison Operators
Comparison operators are used to compare values. Here are the most common ones in Python:
==: Equal to!=: Not equal to>: Greater than<: Less than>=: Greater than or equal to<=: Less than or equal to
For example:
age = 18
if age >= 18:
print("You are an adult!")
In this snippet, the comparison checks if age is greater than or equal to 18, and if true, it prints a message. Here, the expression age >= 18 is a Boolean expression.
Control Structures: if, if–else, and if–elif–else
Control structures allow us to dictate the flow of execution based on conditions. Let’s explore the most commonly used structures in decision-making:
The if Statement
The if statement executes a block of code only when a specified condition is true.
light = "green"
if light == "green":
print("Go!")
The if–else Statement
An if–else statement provides an alternative action if the condition is false.
light = "red"
if light == "green":
print("Go!")
else:
print("Stop!")
In this example, if the light is not green, it will execute the else block and print "Stop!".
The if–elif–else Statement
The if–elif–else structure allows for multiple conditions to be checked in sequence.
light = "yellow"
if light == "green":
print("Go!")
elif light == "yellow":
print("Slow down!")
else:
print("Stop!")
Here, if the light is yellow, it will print "Slow down!".
Nesting Decisions
Sometimes, you may want to place one if statement inside another. This is called nesting.
age = 20
if age >= 18:
print("You are an adult!")
if age > 21:
print("You can legally drink alcohol.")
else:
print("You cannot legally drink alcohol yet.")
In the above example, the program first checks if the age is 18 or older, and then, if that is true, it checks if the age is greater than 21, leading to different outputs accordingly.
Combining Conditions with Logical Operators
In programming, you often need to check multiple conditions at once. This is where logical operators come in:
and: True if both expressions are true.or: True if at least one expression is true.not: Reverses the truth value.
Example with Logical Operators
age = 25
has_license = True
if age >= 18 and has_license:
print("You can drive!")
else:
print("You cannot drive!")
In this case, both conditions must be true for the person to be able to drive.
Decision Logic and Tracing Execution
When designing your decision logic, it’s crucial to understand how the decisions are executed in your code. You can trace through your code by following the logic flow and determining which branch will execute based on the conditions provided.
For instance:
If you have several conditions, outline each one clearly:
if condition_1:elif condition_2:else:
This helps you ensure that your logic is clear and your code functions as intended.
Conclusion
In this lesson, we explored the essentials of selection and decision-making in Python. You now have the power to create dynamic programs that react to user input or system states based on conditions you've defined. 🎉 Remember, mastering these concepts is key to programming!
Study Notes
- Boolean Expressions: Evaluate to true or false.
- Comparison Operators:
==,!=,>,<,>=,<= - Control Structures:
if,if–else,if–elif–else - Nesting: Placing one if statement inside another.
- Logical Operators:
and,or,not - Decision Logic: Design and trace execution paths.
