Python Mini Lesson: If, Else, and % Operator

So far in Python:

We have performed operations and done some work using variables and operators.
We have used functions to separate and deduplicate our code.
We have used tools like lists, loops, and range to both simplify and create more powerful code.

1. Overview

What are flow charts and how can we better plan our programs?
What is a conditional statement?
Why are conditional statements useful?
How do we write conditional statements in Python?
What is the modulo (%) operator?
Why is the modulo operator useful?

2. Python: Conditional Statements

Computer Scientists can benefit from planning before they write the program.
Before any code has been written creating a flow chart can help visualize and outline what you want your program to do.

When you go to write the code a flow chart can easily guide you like a map.
See the example below:


We could use the tools we have learned to try to write this program.
Possibly using a for loop that only ends once the time is 10:00pm. At the beginning of every loop we check the time, the time could be maintained in a variable.

But how do we reason about whether or not it is 10:00pm?
How do we make that yes or no decision?

3. Python: Conditional Statements

Sometimes, you will only want to run code when a certain condition is met.
There are three important keywords and subsequently statements that help us accomplish this.

Let's use bridges as an example.

If statement:

The if statement is like a guard on a bridge.
The guard checks whether the person wanting to cross the bridge satsfies a condition.
If the condition is satisfied, they let you cross the bridge

If the condition is true, the if statement allows the indented code below it to run.

if condition:
     here is code that is dependent on the if (note the indentation)
here is code not dependent on the if

Else If statement:

The else if, or "elif" in Python, statement follows an if statement, and is like a second bridge that is only checked if the person cannot cross the previous bridge(s).
There is still a guard on each one and a condition must be checked.
If the condition is satisfied, they let you cross the elif bridge

The person takes only one bridge.

if condition:
     here is code that is dependent on the if (note the indentation)
elif condition:
     here is code that is dependent on the elif
here is code not dependent on the if

Else statement:

The else statement follows an if or elif statement, and is like a bridge that has no guard.

It allows anything the if/elif statements do not allow to pass, to pass.
There is no condition to check for an else statement

if condition:
     here is code that is dependent on the if (note the indentation)
     here is code that will run if the above if fails

here is code not dependent on the if or the else, it will run after either finishes

All three together:

Combining all three allows you to set up branching your program based off multiple conditions.

Maybe Kings and Queens take the first bridge, Knights take the second, and everyone else takes the third


if King or Queen:
     here is code for the King and/or Queen
elif Knight:
     here is code for the Knights
     here is code for everyone else

here is code for everyone (including Kings, Queens, and Knights) after the three bridges

A key difference:

If statements are independent, meaning they will all be checked regardless of the other conditonal statements outcomes.

Elif and else statements are dependent, meaning they will be checked only if the other conditional statements before them fail.

Imagine these three bridges are if statements. 1, 2, or all 3 of them may be crossed. The first guard may reject them, the second lets them pass, and the third rejects them again. Compare it to the three bridges above where only 1 bridge will be taken.


3. Conditions and some new Python operators:

Up until now we have been saying "condition", but what is a condition?

A condition is something that evaluates to true or false.
3 > 5 is a condition, as it evaluates to false.

Note the use of ">".
Conditional statements often require the use of boolean operators

Often compared to operators like + or - that return numerical values.

Boolean operators return true/false.

OperatorMeaningtrue Examplefalse Example
<Is less than1 < 25 < 5
<=Is less than or equal to1 <= 25 <= 4
==Is equal to1 == 15 == 6
>=Is greater than or equal to2 >= 25 >= 7
>Is greater than2 > 14 > 5
!=Is not equal to1 != 25 != 5

We can use variables as well as boolean operators in conditional statements.

Quick Questions:

1. Maybe you want to see if your list has a size greater than zero, how would you do this?

2. What will this code print?

x = 2
y = 3
if x == y:
    print "It's True!"
    y = x + x
    print "It's False!"
    y = x
print x
print y

4. Python: Modulo Operator

While we're introducing new operators, there's an important operator that shows up frequently in computer science. This is the modulo operator, %, which performs the modulo operation. That's just a fancy way of saying it finds the remainder left over after dividing two numbers.

Let's take a look:

quotient = 5 / 3
remainder = 5 % 3

When is this operator useful? Printing every 10th element of a list!

longList = range(9001)
for i in range(len(longList)):
    if i % 10 == 9:
        print longList[i]

When is this operator useful? Reducing the range of possible values!

smallerNumber = reallyBigNumber % 256

5. Putting Everything Together

Here's a challenge! What will be printed by the code below?

if 9 % 4 == 1:
    print "A"
    if 3 % 3 == 4:
        print "B"
if 7 < 0:
    print "C"
    if 7 >= 0:
        print "D"
print "E"