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

Note that we’re using double equals here == as opposed to a single equal sign =. It’s important to keep those two straight because they do very different things. == is essentially a question; do these two items have the same value? Where = is you assigning a value to a variable. One is a question, one is instruction.

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

One last interesting comparison: is. The concept of is vs == is pretty nuanced and dizzying. I’m not going to suck up a bunch of our time here with it but I wanted you to know is exists. The basic difference between the two is that is is talking about identity; are these two variables pointing to the exact same object? Where == is talking about equality; do these two variables have the same value? You should dig into these for fun another time, but starting out it’s good to know that is exists, and also that most of the time you’ll want to use == instead.

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 = 2019
>>> 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.