Conditions (What If?) 101
What You Learned
In this chapter, you learned how to program a computer to make
decisions based on conditions in code. We saw that the if state-
ment lets a program execute a set of statements only if a condition
is true (like age >= 16). We used Boolean (true/false) expressions
to represent the conditions we wanted to check for, and we built
expressions using conditional operators like <, >, <=, and more.
We combined if and else statements to run one piece of code
or the other, so that if our if statement is not executed, the else
statement runs. We extended this further by selecting among mul-
tiple options using if-elif-else statements, like in our letter grade
program that gave out grades of A, B, C, D, or F depending on the
numeric score entered.
We learned how to test multiple conditions at the same time
using the and and or logical operators to combine conditions (like
rainy == 'y' and cold == 'y'). We used the not operator to check
whether a variable or expression is False.
In our secret message program at the end of the chapter, you
learned that all letters and characters are converted into numeric
values when stored on a computer and that ASCII is one method
of storing text as number values. We used the chr() and ord() func-
tions to convert characters into their ASCII values and back again.
We changed strings of letters to all uppercase or lowercase with
upper() and lower(), and we checked whether a string was upper-
case or lowercase with isupper() and islower(). We built a string
by adding letters one at a time onto the end of the string using
the + operator, and we learned that adding strings together is
sometimes called appending or concatenating.
At this point you should be able to do the following:
• Use if statements to make decisions using conditionals.
• Use conditionals and Boolean expressions to control
program flow.
• Describe how a Boolean expression evaluates to True or False.
• Write conditional expressions using comparison operators (<, >,
==, !=, <=, >=).
• Use if-else statement combinations to choose between two
alternative program paths.