Closure#

Syllabus Week 3: Conditionals#

  • Conditional execution with if statements, keyword if followed by a Boolean expression

  • Controlling scope with indented block

  • Alternative execution with if-else statements, keyword else

  • Chained conditionals with if-elif-else statements, keyword elif

  • Nested conditionals

  • The concept of debugging

  • Debugging with print statement: using print() to trace and understand code execution

  • Identifying errors in code that runs but does not produce the expected output

  • Problem-solving by constructing the correct conditions

Advanced#

Note about the material in the Advanced section. The advanced material contains additional topics related to the week’s content. You are not required to read this material, and none of the exercises or exam questions will rely on it.

In particular the advanced material this week covers truth value testing and conditional expressions.

We suggest you read:

Advanced 3.1: Non-boolean Conditions#

If the variable or the expression used as a condition is not a boolean, it will automatically be cast to a boolean. Try running the following code:

var = 3
print("Python interprets var=" + str(var) + " as")
if var:
    print("True")
else:
    print("False")

To check what the result of the casting is, you can explicitly cast the variable to a boolean:

print(bool(3))
print(bool('Some text'))
print(bool(0))
print(bool(''))

Advanced 3.2: Conditional Expressions#

Python allows for a very concise way of assigning values to variables based on a condition. For example, try testing what the following code does:

x = 0
y = 5 if x > 2 else 3

Write the following 3 examples in a single line. Experiment with the values given to the variables x, y, override_with_c, a, b, and c to make sure your one-liners work as the longer pieces of code.

x = 3 
if x >= 0:
    my_var_1 = x * x
else:
    my_var_1 = 0
y = "lasagna"
if y == "lasagna":
    my_var_2 = "yummy"
else:
    my_var_2 = "not yummy"
a = 4
b = 999
override_with_c = False
c = "You have been overridden >:)"
if override_with_c:
    my_var_3 = c
else: # assign the larger number
    if a > b: 
        my_var_3 = a
    else:
        my_var_3 = b

Advanced 3.3: Multiline Conditions#

Consider the following code:

if month == "December":
    winter = True
elif month == "January":
    winter = True
elif month == "February":
    winter = True
else:
    winter = False

This can also be written as:

winter = (month == "December") or (month == "January") or (month == "February")

However, both examples are not very readable. To improve readability, you can use multiline conditions. As long as you use parentheses to group the condition, you can split the condition over multiple lines. For example, consider this more readable code:

winter = (month == "December" or 
          month == "January"  or 
          month == "February")

Use the knowledge above to make the following code more readable. Also, remember you can add spaces anywhere in the code that already has spaces (except inside strings!) to improve readability:

if (a > 0 and b > 0) or (a < 0 and b < 0) or (a == 0 and b == 0):
    print("a and b are both non-zero with the same sign or a = b = 0")

Advanced 3.4: Can You Drive a Car? (continued)#

This is a continuation of the problem you solved in the Problem Solving section. Try writing a single boolean expression that is True if and only if the person can drive. In other words, your solution to the problem should resemble the code below, but you should replace condition with an appropriate boolean expression computed from the defined values.

age = 16
has_drivers_license = True
BAC = 0.4
companion_in_car = True

if condition:
    print("Police officer: 'You can drive'")
else:
    print("Police officer: 'You cannot drive'")