Conditional Logic

CS 106

Boolean

  • Built-in type
  • Only 2 possible values:
    • True
    • False

Boolean Operators (evaluate to bool)

>
>=
<
<=
==
!=
and
or
not
in
not in

What is the output of the following code?

a = 3
b = (a != 3)
print(b)
  • True
  • False
  • 3
  • Syntax error

Logical Operators

Combine two boolean expressions:

  • p and q (“both p and q”)
    • True only if both are true, otherwise False
  • p or q (“p or q or both”)
    • True if at least one is true.
  • not p (“opposite of p”)
    • True if p is False
    • False if p is True

Order of operations: not, then and, then or

Which expression evaluates to True exactly when at least one of the following is true:

  • a and b are equal
  • a has a value of 5
  • a == b == 5
  • a == b or a == 5
  • a == b and a == 5
  • a == (b == 5)

Suppose we run

a = True
b = False
c = True

What is the value of this expression:

not a and b or c

  • True
  • False

Teenager

Write a boolean expression that evaluates to True if age indicates a teenager.

(Assume age has been previously defined.)

13 <= age and age < 20

13 <= age < 20

13 <= age and 20 > age

Leap Year

Write a single expression that determines whether or not a previously defined year is a leap year.

A leap year is:

  • Divisible by 4 and not divisible by 100
  • or
  • Divisible by 400

hint: year % 4 == 0

When your bool isn’t a bool

What does this do?

result = (a == 3 or 4) # 3 and 4 are both valid
>>> False or "something"
'something'
  • When a is 3: True or 4 -> True.
  • otherwise: False or 4 -> 4.
>>> bool(4)
True
  • “4 is truth-y”
  • So bool(result) is always True

if

if examples

x = 5
if x > 2:
      x = -3
      x = 1
else:
      x = 3
      x = 2

What is the value of x after this code runs?

  • -3
  • 1
  • 2
  • 3
  • 5

What does this do?

x = int(input("Enter your score: "))
if x >= 90:
    print("You’ve got an A")
if x >= 80:
    print("You’ve got a B")
else:
    print("Keep studying!")

Fixed?

if x >= 90:
    print("You’ve got an A")
if not (x >= 90) and x >= 80:
    print("You’ve got a B")
else:
    print("Keep studying!")
if x >= 90:
    print("You’ve got an A")
elif x >= 80:
    print("You’ve got a B")
else:
    print("Keep studying!")
x = int(input("Enter your score:"))
if x >= 60:
    print("You’ve got a D")
elif x >= 70:
    print("You’ve got a C")
elif x >= 80:
    print("You’ve got a B")
elif x >= 90:
    print("You’ve got an A")
else:
    print("Keep studying!")

Examples

Complete the following code to find the max of x, y and z. Do not use the built-in max function.

x = int(input("Enter value 1:"))
y = int(input("Enter value 2:"))
z = int(input("Enter value 3:"))
my_max = ...
<your code here>
print('The maximum value is:', my_max)

pH

Write a program that

  • prompts the user to enter a pH value
  • tells the user whether the pH given indicates an acid (value < 7.0), base (value > 7.0) or neutral (value is 7.0) substance.

pH values range from 0 to 14.

Now, update your program so that only valid pH values are considered. Display an error for invalid inputs.