Learning Objectives
Through readings, lecture and labs, it is expected that you are doing work and seeking help to keep pace with the below objectives:
- Conditionals (
if
statements) - You should be comfortable with:
- Tracing the execution of programs using
if
,elif
,else
- Computing the values of Boolean expressions (expressions that evaluate to
True
orFalse
). - Writing programs that use these constructs.
- Tracing the execution of programs using
- Nested Loops
- You should be comfortable with:
- Write simple nested loops.
- Trace the execution of nested loops.
Required Reading
Supplemental Materials
We all need alternative presentations of the concepts from time to time. The following resources are optional. If something seems unclear after class, or the pratice questions seem hard for you, then explore these topics more using the resources below.
- Conditionals (
if
statements) -
- CS Circles lesson on if-statements
- CS Circles lesson on
else
,and
,or
,not
- Zelle, Sections 7.1–7.3
- Zelle, Sections 7.5
- Nested Loops
Practice
For all these exercises, I recommend you try them on paper and then test them out with PythonTutor.Exercise 1
Write what this code produces, exactly. Be very careful with whitespace and the newlines.
x=3 print('Bananas? We have ', end='') if (x >=3): print('several ', end='') if (x == 2): print('a pair ', end='') elif (x == 1): print('just one', end='') elif (x == 0): print('no', end='') else: print('many ', end='') print('bananas today.')
Exercise 2
Write a piece of code that produces the following input/output behavior. Text that is italics and underlined are user input.
It asks the user for the number of integers it will look at; for each integer, prompts for it, then prints if it is even or odd, and also prints if it is divisible by three.
How many integers are we looking at? 4 Enter integer: 6 Its even! Its divisible by three! Enter integer: 8 Its even! Enter integer: 9 Its odd! Its divisible by three! Enter integer: 11 Its odd!Here is a sample answer, when you think you have solved it. (Don't cheat yourself and look early).
Exercise 3
Write what this code produces, exactly. Be very careful with whitespace and the newlines.
n = 4 for i in range(1,n): for j in range(1,n): print(i-j,end=' ') print()