1 of 26

Operators

Andrea Wu

2 of 26

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

3 of 26

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

4 of 26

Comparison Operators

5 of 26

Objective: Comparison Operators

What are they?

Why are they important?

Examples

6 of 26

Remember these?

<

=

>

less than

less than or equal to

greater than or equal to

greater than

equal to

not equal to

7 of 26

Comparison Operators

<

<=

==

>

>=

!=

less than

less than or equal to

greater than or equal to

greater than

equal to

not equal to

8 of 26

What's up with equals?

=

assignment operator

think contains

my_var = 20

==

equality operator

think is equivalent to?

22 == 23

9 of 26

EdStem: comparison operators

10 of 26

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

11 of 26

Logical Operators

12 of 26

Objective: Logical Operators

What are they?

Why are they important?

Examples

13 of 26

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?

14 of 26

Logical Operators

Reminder: Arithmetic operators (+, -, *, /) to combine numbers

Logical operators: combine booleans

  • and
  • or
  • not

15 of 26

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

16 of 26

EdStem: milkshake time!

17 of 26

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

18 of 26

EdStem Practice: outdoor activities!

19 of 26

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

20 of 26

EdStem Practice: favorite colors!

21 of 26

How is not different from and and or?

22 of 26

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

23 of 26

Combining Operators

Remember PEMDAS? Order of operations for arithmetic operators

Similarly...

PNAO!

(parentheses, not, and, or)

24 of 26

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

25 of 26

Edstem: Combining Logical Operators

26 of 26

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