CHAPTER 6
Making Decisions with if Statements
Python includes all the tools you need to make decisions easily and effectively in your code. In this chapter, you meet Python’s if statements, if… else statements, and if… elif statements and put them to work in your code. You also learn how to nest if statements to make complex decisions in your scripts.

Learn the Essentials of if Statements
Understanding the if Statement
Understanding the if… else Statement
Understanding the if… elif Statement
Understanding the if… elif… else Statement
Create an if… elif… else Statement
Learn the Essentials of if Statements
To make decisions in your code, you use Python’s various types of if statements. When an if statement’s condition evaluates to True, Python runs the code that follows the statement. An if… else statement runs the if code when the condition is True and the else code when it is False. An if… elif statement can evaluate not only the if condition but also one or more elif conditions, as needed; you can add an else statement that runs code when both if and all elif conditions evaluate to False. You can nest if statements to make complex decisions.
Essential Features of if Statements
The three main forms of if statement are plain if, if… ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access