Programmers need to ask a lot of yes/no questions to determine what the program should do next. It’s kind of like in life. We decide what we should do based on some kind of data around us. Is the light red? Yes. I should stop my car and wait. Is the light green? Yes. I should continue driving. Is it Monday? Yes. I should go back to bed.
Let’s explore how we can use conditionals in our programs.
Yes/no (true/false) tests are called booleans. Let’s look at some simple booleans.
Python has conditionals that return boolean values. Python supports
== for “equal” and
!= for “not equal”.
>>> 0 == 1 False >>> 0 == 0 True >>> 0 != 1 True
Other comparisons on numbers are just as you’d expect:
>>> 1 > 0 True >>> 2 >= 3 False >>> -1<0 True >>> .5 <= 1 True
What do you think happens here?
>>> "a" < "b"
a comes before
b so it’s considered
True. Comparisons also work for strings, and a number of other types that we haven’t learned about yet.
>>> "a" == "A" False >>> "a" == "a" True>>> "a" < "b" True >>> "apple" < "animal" False
What about this one?
>>> "Z" < "a"
True! Even though
Z clearly comes after
a in the alphabet,
Z—and all other upper-case letters—comes before
a and all lower-case letters ASCIIbetically.
In Python 2, you could also compare strings to integers and other types, but this was weird, and the behavior was removed in Python 3.
Let’s look at the
not in operators.
>>> "H" in "Hello"
True. We can us
in to check to see if some value is in another value. That is, it checks for containment:
>>> "lo" in "Hello" True >>> "x" in "Hello" False
We can also use
not in in the same way:
>>> "H" not in "Hello" False >>> "lo" not in "Hello" False >>> "x" not in "Hello" True
if statements to make choices on what to do when something is
False. Let’s take a look:
>>> year = 1999 >>> if year == 1999: ... print("It's 1999 so we should party a lot!") ... It's 1999 so we should party a lot!
You’ll notice that we’ve indented the
We can also add
else to do something no other conditions are met.
>>> year = 2017 >>> if year == 1999: ... print("It's 1999 so we should party a lot!") ... else: ... print("What a boring year. Let's take a nap.") ... What a boring year. Let's take a nap.
Your Turn: Conditionals 🏁¶
Write some code that:
- Sets a temperature variable
- Prints out “Just right” if the temperature is between 65 and 75
- Prints out “Too hot. Gotta run for shelter.” if the temperature is above that range
- Prints out “Cold as ice” if the temperature is below 65
Update your code for the excercise above to set the temperature based on input from the user in the command line.
Rock, Paper, Scissors¶
Create a rock, paper, scissors program that evaluates two moves given as input and prints out which move would win. You can reference the rules of rock, paper, scissors here.
Guess the Number¶
Write a program
number_guesser.py that does the following:
- Sets a random secret number from 1-14
- Prompts input for a guess of what the number is
- Prints a success message if the guess is correct and the program ends
4. If the number is incorrect, print a message indicating if the number is higher or lower 4. Prints “Out of tries!” after the third incorrct guess
Virtual Paper Fortune Teller¶
Create a program that emulates the experience of a paper fortune teller. The program should:
- Ask for a color chosen from 4 colors
- As for a number chosen from a set of 4 numbers depending on which color was chosen
- Repeat step 2 as long as you’d like
- Finally, have the user choose from a set of 4 numbers and reveal a fortune related to that number
greeting.py to have the following behavior:
- Check if the given name has a “t” or “m” in it.
- Print something very friendly if it does
- Print “Oh. Hi.” if it doesn’t
- Make sure you check for both upper- and lower-case letters