Learn Python 3 the Hard Way: A Very Simple Introduction to the Terrifyingly Beautiful World of Computers and Code
by Zed A. Shaw
Exercise 30. Else and If
In the last exercise you worked out some if-statements and then tried to guess what they are and how they work. Before you learn more I’ll explain what everything is by answering the questions you had from the previous Study Drills section. You did the Study Drills, right?
1. What do you think the if does to the code under it? An if-statement creates what is called a “branch” in the code. It’s kind of like those choose your own adventure books where you are asked to turn to one page if you make one choice and another if you go a different direction. The if-statement tells your script, “If this Boolean expression is True, then run the code under it; otherwise, skip it.”
2. Why does the code under the if need to be indented ...
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