Unit 1: Programming Basics

Lesson 4: Conditional Statements

Notebook Authors

(fill in your two names here)

Team Roles

Facilitator: (fill in name)
Spokesperson: (fill in name)
Process Analyst: (fill in name)
Quality Control: (fill in name)

If there are only three people in your team, have one person serve as both spokesperson and process analyst for the rest of this activity.

At the end of this Lesson, you will be asked to record how long each Model required for your team. The Facilitator should keep track of time for your team.

Computational Focus: Conditional Statements

Model 1: Boolean Values and Expression

Boolean is a data type that only has two values: True or False. (The capitalization is required.) A Boolean expression is a comparison statement that results in one of the two Boolean values. The six relational operators commonly used in a Boolean expression can be found in Table 1 at the end of this lesson.

Type (don't copy and paste) the following code, one line per Jupyter cell, to observe the output (if any):

type(True)
type(3 < 4)
print(3 < 4)
three, four = 3, 4
print(three == four)
check = three > four
print(check)
type(check)
print(three = four)
three = four
print(three == four)

Critical Thinking Questions

1. Identify two examples for each of the following terms demonstrated in the above Model:
1a. Boolean

1b. Boolean operator

1c. Boolean expression

2. Explain why the same Boolean expression three == four evaluated as two different Boolean values in Model 1 (lines 5 and 11).

3. Give an example of a Boolean expression that uses the Booliean operator != and evaluates to False.

4. What is the difference between = and the == operators?

Model 2: The if Statement

Many times it is necessary to control which statements are executed in a program. A branching statement contains a condition that determines whether or not the following block of code is executed. This decision point is signaled by the if keyword followed by a Boolean expression and a colon. All indented lines following the if-statement are executed only if the value of the Boolean expression is True.

Define the following model in a single Jupyter code cell:

def model_two(number):
    """
    this function takes a number as 
    a parameter and determines its 
    relationship to the value 3600.
    """
    if number < 3600:
        print(str(number) + " is less than 3600")
    print("goodbye")

Make sure the code is properly indented. Then call the function twice in two separate Jupyter code cells:

model_two(100)
model_two(9999)

Critical Thinking Questions

5. Give the Boolean expression from the above code.

6. What must the value of that Boolean expression be, in order for the first print function in model_two to execute?

7. After an if-condition, what syntax differentiates between (1) statements that are executed based on the condition and (2) statements that are always executed?

8. Describe an advantage to calling print functions with an if-statement.

Model 3: The if-else Statement

Often times a different execution is desired when the if-statement condition is False. When an if-statement includes the keyword else, the statement decides between executing two different blocks of code.

Define the model_three function in a single Jupyter code cell:

def model_three(number):
    """
    this function takes a number as 
    a parameter and determines its 
    relationship to the value 3600.
    """
    if number < 3600:
        print(number, "is less than 3600")
        answer = True
    else:
        print(number, "is greater than 3600")
        answer = False
    return answer

now add the following Python lines in a separate code cell:

seconds = int(input("Enter a number of seconds:"))
print("is my time less than an hour?")
print(model_three(seconds))

Note: for the multiple test cases in questions below, make sure that you show the results of each of them. To do that you'll need to repeat part of the code above (but not all of it) each time you want to test a different value.

Critical Thinking Questions

9. What type of variable is being stored in the variable answer?

10. Consider what happens in each of the following situations:
10a. What happens if the value of the first Boolean expression (after if) is True?

10b. What is the value of answer when 10 is entered at the prompt (show your results)? Explain how the variables number and answer get their values.

10c. What is the value of answer when 9999 is entered at the prompt (show your results)? Explain how the variable answer gets its value.

11. Are the print functions correct when you input the number 3600 (show your results)? Explain why or why not.

Model 4: The if-elif-else Statement

When there are multiple conditional statements to evaluate that may be True, you can use the keyword elif (short for “else-if”). Each condition statement is evaluated until the Boolean expression is True and then the remaining branching statements (elif or else) are ignored. You also are not required to use final else when you use an elif.

Define the model_four function in a single Jupyter code cell:

def model_four(number):
    """
    this function takes a number as 
    a parameter and determines its 
    relationship to the value 3600.
    """    
    if number < 3600:
         print(number, "is less than 3600")
         answer = False
    elif number > 3600:
         print(number, "is greater than 3600")
         answer = False
    else:
         print("your number is 3600")
         answer = True 
    return answer

now add the following Python lines in a separate code cell:

seconds = int(input("Enter a number of seconds:"))
print("is my time equal to an hour?")
print(model_four(seconds))

Critical Thinking Questions

12. Which print functions execute if the values of the first two Boolean expressions are False?

13. If the value of the first Boolean expressions is True, how many remaining lines of code are ignored or skipped?

14. Run code to show the value of answer, then explain how it gets its value for each of the following:
14a. the number entered is 10

14b. the number entered is 3600

14c. the number entered is 9999

15. Consider how to solve for the time it takes a car initially traveling at a velocity of 11 m/s (~25 mph) to go 400 meters while applying a constant acceleration of 6.0 m/s2. Using your group's answers to the pre-activity, identify the appropriate input values for a, b, & c to solve this problem.

16. Describe how a branching statement might be useful in a program solving the quadratic formula (from the Lesson 4 pre-activity).

Temporal Analysis Report

How much time did it require for your team to complete each Model?

Model 1:
Model 2:
Model 3:
Model 4:

END OF CLASS

Table 1: Boolean Comparisons (copied from and more info here)

Operation Meaning
< strictly less than
<= less than or equal
> strictly greater than
>= greater than or equal
== equal
!= not equal
is object identity
is not negated object identity

In [ ]: