1 of 14

Operators the Evaluate to True or False

  • While Loops Practice

2 of 14

Comparison Operators

Comparison operators compare two operands according to a comparison rule and evaluate to either True or False (boolean)

2

Operator

Description

==

If the values of two operands are equal, then the condition becomes true.

!=

If values of two operands are not equal, then condition becomes true.

>

If the value of left operand is greater than the value of right operand, then condition becomes true.

<

If the value of left operand is less than the value of right operand, then condition becomes true.

>=

If the value of left operand is greater than or equal to the value of right operand, then condition becomes true.

<=

If the value of left operand is less than or equal to the value of right operand, then condition becomes true.

3 of 14

Logical Operators

Logical operators provide additional ways to determine whether something is true or false:

3

Operator

Description

and

If both operands are True then the expression evaluates to True. Otherwise, the expression evaluates to False

or

If either or both operands are True then the expression evaluates to True. If both operands are false, the expression evaluates to False

not

If the operand is False than the expression evaluates to True (and vice versa)

in

If the left operand is an element of the right operand, then the expression evaluates to True. Otherwise, the expression evaluates to False

4 of 14

Truth Tables

a

b

not a

a and b

a or b

False

False

True

False

False

False

True

True

False

True

True

False

False

False

True

True

True

False

True

True

5 of 14

What is stored in x?

a = 5�b = 5�x = b >= a

6 of 14

What is stored in x / y?

a = 4�b = 3�x = a == b�y = not x

7 of 14

What is stored in d / e / f?

a = True�b = False�c = False�d = not a�e = a or b�f = not (c and b)�g = not (a or b)

8 of 14

Practice: Using Logical Operators (lecture11/07_color_mixer.py)

'''

INSTRUCTIONS: Update this function as follows:

If red is turned on, make the background red.

If yellow is turned on, make the background yellow.

If blue is turned on, make the background blue.

If red and yellow are both turned on, make the background orange.

If red and blue are turned on, make the background purple.

If yellow and blue are turned on, make the background green.

If everything is turned on, then make the background black.

'''

def get_color(red_switch:bool, yellow_switch:bool, blue_switch:bool):

pass

8

9 of 14

On Your Own: Using Logical Operators

def get_movie_discount_message(is_senior:bool, is_child:bool, is_student:bool):

# write a function that returns a message in accordance with the

# arguments passed into the functions:

# - if they're a senior, tell them they get a discount

# - if they're a child, tell them they get a discount

# - if they're a student, tell them you need to see their ID

# - if they're an adult, tell them that you're going to charge them full price

pass

print(get_movie_discount_message(True, False, False)) # prints True

print(get_movie_discount_message(False, False, True)) # prints True

print(get_movie_discount_message(False, False, False)) # prints False

9

10 of 14

While Loops

10

while some condition is True:

# execute code block

Pseudocode

11 of 14

Animation

Demo: lecture12/03_while_animate.py

11

12 of 14

Animation: Single Shape

Animation has very similar principles: it’s about rapidly redrawing shapes in slightly different positions to look continuous (think flipbook).

Demo: 03_while_animate.py

12

13 of 14

Circle Challenges

  1. Rewrite the animation so that it uses an infinite while loop
  2. Now, make it stop after 50 iterations
  3. Experiment with motion:
    1. Make it move to the left, right, up, down, diagonal
    2. Make it move faster and slower
    3. Make the animation look smooth
  4. Can you make your circle move back and forth?
  5. Can you make it move so that it seems to trace the outline of a square?

Demo: 03_while_animate.py

13

14 of 14

Animation: Car

Animation has very similar principles: it’s about rapidly redrawing shapes in slightly different positions to look continuous (think flipbook).

Demo: 04_while_animate_car.py

14