In [ ]:
# suppress automatic output
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "none"

Conditional Statements

Conditional statements tell the program when to run a particular block of code. Often, we only want something to happen when a certain condition is met.

For example, we might want a robot to stop if it is a particular distance from a wall.

In Python, we can use comparison operators along with the keywords if, else, and elif to write conditional statements.

Comparing Numbers

We often need to know whether one number is larger than, less than, or equal to another. The following operators allow us to do this:

  • <: less than
  • <=: less than or equal to
  • >: greater than
  • >=: greater than or equal to
  • ==: is equal to
  • !=: is not equal to
  • is: is identical to
  • is not: not identical to

Check if two numbers (either int or float) are equal.
Remember to print the output!!!!

In [ ]:
# Start your code here

Check if one number is less than another.

In [ ]:
# Start your code here

Check if one number is greater than or equal to another.

In [ ]:
# Start your code here

Compare 2 and 2 using >. Is the result what you expected?

In [ ]:
# Start your code here

Compare 2 and 2 using >=. Is the result what you expected?

In [ ]:
# Start your code here

Compare 2 and 2.0 using ==.
Before running it, what do you expect the result to be?
What does the result tell us about how == compares integers and floats?

In [ ]:
# Start your code here

Compare 2 and 2.0 using is.
Before running it, do you expect the result to be?
What does the result tell us about how is compares integers and floats?

In [ ]:
# Start your code here

Comparing Booleans

Booleans can be compared using the ==, !=, is, and is not operators.

Check if True is equal to False using ==.

In [ ]:
# Start your code here

Check if False is equal to 0 using ==.
Before running it, do you expect the result to be?
What does the result tell us about how == compares booleans and zero?

In [ ]:
# Start your code here

Check if False is identical to 0 using is.
Before running it, do you expect the result to be?
What does the result tell us about how is compares booleans and zero?

In [ ]:
# Start your code here

Comparing Strings

We can compare strings using the ==, !=, is, and is not operators.
For strings, == and is generally do the same thing.

Check if two strings are equal using ==.

In [ ]:
# Start your code here

Check if two strings are not identical using is not.

In [ ]:
# Start your code here

Logical Operators

We can also use the logical operators not, and, and or on booleans to make more complicated logical statements:

  • not: True if the boolean is False, False otherwise.
  • and: True if both booleans are True, False otherwise.
  • or: True if either boolean is True, False otherwise.

Run not False below. Is the result what you expected?

In [ ]:
# Start your code here

Run True and False below. Is the result what you expected?

In [ ]:
# Start your code here

Run True or True below. Is the result what you expected?

In [ ]:
# Start your code here

Like mathematical operators, logical operators also have an order of operations:

  1. ()
  2. not
  3. and
  4. or

Based on this order, run not False or True and True below.
Is the result what you expected?

In [ ]:
# Start your code here

We can also use parenthesis ( ). Whatever is inside the parenthesis is evaluated first.

Based on this order, run not (False or True) and True below.
Is the result what you expected?

In [ ]:
# Start your code here

Combining Comparisons

As we have seen in previous exercises, comparison statements evaluate to booleans. This means that we can combine multiple comparisons using logical operators!

Run 1 > 3 or 4 == 4.0 below. Is the result what you expected?

In [ ]:
# Start your code here

Run 1 == 1 and 1 > -1 below. Is the result what you expected?

In [ ]:
# Start your code here

Try making your own statement using comparison and logical operators. Do you expect your statement to evaluate to True or False? Why?

In [ ]:
# Start your code here

--- STOP HERE ---


Conditional Statements

We can use if, else, and elif in order to tell the program whether to run a particular indented block of code based on a boolean.

  • if: Run code if boolean is True.
  • elif: Run code if previous conditions are False and boolean is True.
  • else: Run code if previous conditions are False.

Let's start with an example of a simple if statement. What do you expect to be printed for different values of n? Why? Try choosing different numbers and see what happens.

In [ ]:
# Choose n
n = int(raw_input("Input n: "))

if n > 10:
    print('n is greater than 10')

If n is less than or equal to 10, nothing is printed since the condition is false (remember, > means strictly greater than).

We can improve our code by adding an elif statement.

Try running the code again for several numbers and think about what is happening.
Remember, an elif statement only runs the code block if all previous conditions were False.

In [ ]:
# Choose n
n = int(raw_input("Input n: "))

if n > 10:
    print('n is greater than 10')
elif n < 10:
    print('n is less than 10')

Our code is better, but we still get no feedback if n is exactly 10. An else statement will run if all previous conditions were False.

Try running the following code for different values of n, and make sure to try 10.

In [ ]:
# Choose n
n = int(raw_input("Input n: "))

if n > 10:
    print('n is greater than 10')
elif n < 10:
    print('n is less than 10')
else:
    print('n is 10')

We can also nest if statements. Examine the following code and think about what will happen for different values of n. (Remember, an if statement is just a block of code that can be within another if statement!)

In [ ]:
# Choose n
n = int(raw_input("Input n: "))

if n > 0:
    if n > 10:
        print('n is greater than 10')
    elif n < 10:
        print('n is less than 10')
    else:
        print('n is 10')
else:
    print('n is not positive!')

The Candy Party

When beavers get together for a party, they like to have candy. A beaver party is successful when the number of candies is between 40 and 60, inclusive. Unless it is the weekend, in which case there is no upper bound on the number of candies.

Print True if the party with the given values is successful, or False otherwise.

Try your code for different values of candies and is_weekend to test it.

In [ ]:
# Change these to test your code!
candies = 0
is_weekend = True

# Start your code here

Tennis Game

You are playing doubles in a tennis game. The variable you is your tennis skill level, from 0 to 10. tm is your teammate's tennis skill level, from 0 to 10. Your chances of winning are 0 = no, 1 = maybe, and 2 = yes. Your chances are based on the following conditions:

  • If your skill level is 3 or more above your teammate's, then your chances are 2.
  • If your skill level is within 2 of your teammate's, the your chances are 1.
  • If your skill level is 3 or less below your teammate's, then your chances are 0.

Based on you and your teammate's skill levels, print your chances of winning.

Check your answer by running the code several times and manually checking the result.

In [ ]:
# This code generates and prints the skill levels.
# (Don't worry about understanding it.)
from random import randint     # import the randint function
you = randint(0, 10)           # pick a random integer between 0 and 10
tm = randint(0, 10)            # pick a random integer between 0 and 10
print("You: " + str(you))      # print your skill level
print("Teammate: " + str(tm))  # print teammate's skill level

# Start your code here

Lone Sum

Given three random integers a, b, and c, print their sum. However, if two numbers have the same value, neither value counts towards the sum. For example:

Check your answer by running the code several times and manually checking the result.

In [ ]:
# This code generates and prints the random numbers.
# (Don't worry about understanding it for now.)
from random import randint  # import the randint function
a = randint(0, 5)           # pick a random integer between 0 and 5
b = randint(0, 5)           # pick a random integer between 0 and 5
c = randint(0, 5)           # pick a random integer between 0 and 5
print('a: ' + str(a))       # print a
print('b: ' + str(b))       # print b
print('c: ' + str(c))       # print c

# Start your code here