Operators the Evaluate to True or False
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. |
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 |
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 |
What is stored in x?
a = 5�b = 5�x = b >= a
What is stored in x / y?
a = 4�b = 3�x = a == b�y = not x
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)
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
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
While Loops
10
while some condition is True:
# execute code block
Pseudocode
Animation
Demo: lecture12/03_while_animate.py
11
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
Circle Challenges
Demo: 03_while_animate.py
13
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