Conditionals

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.

Booleans

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"

It’s True. 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"

It’s 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 in and not in operators.

Is this True or False?

>>> "H" in "Hello"

It’s 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

We use if statements to make choices on what to do when something is True or 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!

Note

You’ll notice that we’ve indented the print function by four spaces. Indentation is really important in Python. While any amount of white space will work as long as you use it consistently, convention is to use four spaces, so we’ll do that throughout the class. After the print function, we enter a blank line to tell the REPL that our block is finished.

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 🏁

Conditional Exercises

Too Hot

Write some code that:

  1. Sets a temperature variable
  2. Prints out “Just right” if the temperature is between 65 and 75
  3. Prints out “Too hot. Gotta run for shelter.” if the temperature is above that range

Bonus:

  1. Prints out “Cold as ice” if the temperature is below 65

Interactive Temperature

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:

  1. Sets a random secret number from 1-14
  2. Prompts input for a guess of what the number is
  3. 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:

  1. Ask for a color chosen from 4 colors
  2. As for a number chosen from a set of 4 numbers depending on which color was chosen
  3. Repeat step 2 as long as you’d like
  4. Finally, have the user choose from a set of 4 numbers and reveal a fortune related to that number

Snobby Greeting

Change greeting.py to have the following behavior:

  1. Check if the given name has a “t” or “m” in it.
  2. Print something very friendly if it does
  3. Print “Oh. Hi.” if it doesn’t
  4. Make sure you check for both upper- and lower-case letters

Upcoming Celebrations

Create a program that takes a birthdate as input and prints any upcoming dates that are important. (Turning 30 years old, turning 90 years old, turning turning 3.14159, etc.) See documentation datetime.date()