Operators
Andrea Wu
CONCEPTS, not language
Learn concepts
Use language as tool to perform and practice concepts
Class is NOT about learning Python
Class IS about learning computer science CONCEPTS
We are learning CONCEPTS
Review: I / O
What is it?
User providing input and receiving output
Why is this important?
Many programs and apps need user input, and based on input, program provides output
How (in Python)?
input(): get input from user
print(): show output to user
Comparison Operators
Objective: Comparison Operators
What are they?
Why are they important?
Examples
Remember these?
<
≤
=
>
≥
≠
less than
less than or equal to
greater than or equal to
greater than
equal to
not equal to
Comparison Operators
<
<=
==
>
>=
!=
less than
less than or equal to
greater than or equal to
greater than
equal to
not equal to
What's up with equals?
=
assignment operator
think contains
my_var = 20
==
equality operator
think is equivalent to?
22 == 23
EdStem: comparison operators
Review: Comparison Operators
What are they?
Operators comparing values to return boolean value
Why are they important?
Compare values and make decisions based on evaluated outcome
Examples
<
<=
==
>
>=
!=
less than
less than or equal to
greater than or equal to
greater than
equal to
not equal to
Logical Operators
Objective: Logical Operators
What are they?
Why are they important?
Examples
True or False
Are your favorite activities hiking AND traveling?
Have you had coffee OR tea this morning?
Are you NOT excited for the weekend?
Do we have Monday AND NOT Tuesday off for Labor Day?
Logical Operators
Reminder: Arithmetic operators (+, -, *, /) to combine numbers
Logical operators: combine booleans
Logical Operators: AND
Peanut
Butter
Jelly
Likes peanut butter | Likes jelly | Likes peanut butter and Jelly |
True | True | True |
True | False | False |
False | True | False |
False | False | False |
AND returns true only if both operands are true
EdStem: milkshake time!
Logical Operators: OR
Has Sneakers | Has Flip Flops | Has Sneakers or Flip Flops |
True | True | True |
True | False | True |
False | True | True |
False | False | False |
OR returns true if either (or both) operand is true
Sneakers
Flip Flops
EdStem Practice: outdoor activities!
Logical Operators: NOT
NOT inverts (gives opposite value of) the value of its operand
Sneakers
Flip Flops
Is Sleep Deprived | Is not Sleep Deprived |
True | False |
False | True |
Sleep Deprived
EdStem Practice: favorite colors!
How is not different from and and or?
Unary
Binary
operates on only one operand, like not
example:
not is_standing
operates on two operands, like and, or
examples:
has_brother and has_sister
is_hot or is_cold
Combining Operators
Remember PEMDAS? Order of operations for arithmetic operators
Similarly...
PNAO!
(parentheses, not, and, or)
Examples: PNAO
False or (True and False)
not True and False
(True or not False) and True
False or False
False
False and False
False
(True or True) and True
True and True
True
Edstem: Combining Logical Operators
Review: Logical Operators
What are they?
Operators that combine booleans
and: True only if both operands are True
or: True if either (or both) operand is True
not: Inverts value of its operand
order of operations: PNAO
Why are they important?
Enable programs to make decisions based on multiple conditions