Author: Siddharth Pampana | Last updated: 2025-02-24
Conditional Execution in Python
What You'll Learn and Why It's Important
In this lesson, you'll explore how to make decisions in your programs using conditional statements. This ability allows your programs to respond differently depending on the inputs or situations, which is key to creating dynamic, flexible software.
You'll learn about:
- Boolean Expressions
- Conditional Statements
- Relational Operators
- Logical Operators
- Nested Conditionals
- Chained Conditionals
- The
if
andelse
Statements
By the end of this lesson, you will be able to control the flow of your programs based on different conditions, enabling your code to make decisions like a human would!
3.1 Boolean Expressions
A Boolean expression is an expression that evaluates to either True
or False
. These expressions are used to make decisions in your programs.
In Python, you can create a Boolean expression using relational operators such as ==
, !=
, >
, <
, >=
, and <=
.
Boolean Expressions
3.2 Conditional Statements
Conditional statements allow your program to execute certain code only when a specific condition is True
. Python uses the if
, else
, and elif
(else if) keywords for this.
Conditional Statements
3.3 Relational Operators
Relational operators compare values and produce Boolean results (True
or False
). Here are the most common ones:
==
: Equal to!=
: Not equal to>
: Greater than<
: Less than>=
: Greater than or equal to<=
: Less than or equal to
Relational Operators
3.4 Logical Operators
You can combine multiple conditions using logical operators:
and
: ReturnsTrue
if both conditions areTrue
.or
: ReturnsTrue
if at least one condition isTrue
.not
: Reverses the result of a condition.
Logical Operators
3.5 Nested Conditionals
Nested conditionals occur when you place one if
statement inside another. This is useful when you have multiple conditions to check in sequence.
Nested Conditionals
3.6 Chained Conditionals
Chained conditionals allow you to check multiple conditions using elif
. This is often more efficient than using multiple if
statements.
Chained Conditionals
3.7 The if
and else
Statements
The if
statement is used to test a condition. If the condition is true, the indented block of code that follows the if
statement will execute. If not, the code after the else
will execute.
If and Else Statements
3.8 Glossary
- Name
Boolean Expression
- Type
- Description
An expression that evaluates to either
True
orFalse
.
- Name
Relational Operators
- Type
- Description
Operators that compare two values:
==
,!=
,>
,<
,>=
,<=
.
- Name
Logical Operators
- Type
- Description
Operators that combine multiple Boolean expressions:
and
,or
,not
.
- Name
Nested Conditionals
- Type
- Description
A conditional statement inside another conditional statement.
- Name
Chained Conditionals
- Type
- Description
Multiple
if
,elif
, andelse
conditions checked in sequence.
3.9 Exercises
- Exercise 1: Write a program that asks the user for their age and tells them if they are eligible to vote (18 years or older).
Exercise 1
- Exercise 2: Write a program that checks if a number is positive, negative, or zero using chained conditionals.
Exercise 2
- Exercise 3: Write a program that asks the user for two numbers and prints which one is larger.
Exercise 3
- Exercise 4: Write a program that checks if a number is divisible by both 3 and 5 using logical operators.
Exercise 4
- Exercise 5: Create a program that asks the user to enter a year and determines if it's a leap year. A year is a leap year if it's divisible by 4 but not by 100, unless it's also divisible by 400.
Exercise 5
These exercises cover the topics discussed in the lesson. Each exercise provides an empty code editor for you to write your solutions, and you can validate your answers by running the code to check for correctness. Happy coding!