1 of 41

Logical Operators

Jake Shoudy

August 22, 2022

CSCI 110 - Lecture 6

2 of 41

Today’s Agenda

  • Recap
  • Logical Operators
  • Practice!

3 of 41

Announcements

4 of 41

Pre-class Surveys

Two surveys posted on Ed

Please take them 🙏 🙏 🙏

5 of 41

Google TechExchange

What: Google program for HBCU students to learn CS things on Google campus (targeted at sophomores)

Google program leads will be on Fisk Campus to talk about it!

Timing: Aug 29th 2022, 5-7pm See more here

6 of 41

Reminder: HW1

Due Wednesday at 11:59pm

Only your most recent submission will be graded. If you submit after the deadline a late day will be used.

7 of 41

Project 1: Part 1

  • Blackjack!
  • 3 files:
    • Name: take a card # and output the card name
    • Value: take a card # and output the card value
    • End Status: take a hand value and output BLACKJACK or BUST
  • Released in Ed
  • Due on Sunday at 11:59pm

8 of 41

Quiz 1 Scores

Before Curve

Mean

62%

Min

10%

Max

100%

Median

70%

Percent of class above 70%

43%

Percent of class above 90%

8%

9 of 41

Quiz 1 Notes

  • Don’t forget to submit each question! You can always change the answer and resubmit.
  • Make sure you are logged in to YOUR account
  • BE ON TIME! From now on quizzes will start EXACTLY at :05 and end at :20
  • Reminder we drop your lowest 2 scores at the end of the semester and in total they are only 5% of your grade
  • Quiz 2 is this Friday!
    • If you score higher than you did on quiz 1, we will replace your quiz 1 score.

10 of 41

TA Office Hours

  • Aminu
    • Monday’s from 5-6pm
    • Library 3rd floor
  • Aman
    • Monday’s from 10:30-11:30am
    • Library 3rd floor
  • Angel/Arun
    • Will be added soon
  • All available on class calendar

11 of 41

Recap

12 of 41

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

13 of 41

Review: I/O

age = input(“how old are you today? ”)

print(“Next year you will be “ + (age + 1) + “ years old”)

Traceback (most recent call last):

File "main.py", line 2, in <module>

print('Next year you will be ' + (age + 1) + ' years old')

TypeError: can only concatenate str (not "int") to str

14 of 41

Review: I/O

age = int(input(“how old are you today? ”))

print(“Next year you will be “ + str(age + 1) + “ years old”)

15 of 41

Review: Conditionals & I/O

fruit = int(input())

if fruit / 2 >= 2:

if fruit % 2 == 1:

print('apples')

elif fruit % 3 == 2:

print('apples')

else:

print('oranges')

else:

print('oranges')

16 of 41

Review: Conditionals & I/O

fruit = int(input())

if fruit / 2 >= 2:

if fruit % 2 == 1:

print('apples')

elif fruit % 3 == 2:

print('apples')

else:

print('oranges')

else:

print('oranges')

4

6

3

5

8

oranges

oranges

oranges

apples

apples

17 of 41

Review: Conditionals & I/O

fruit = int(input())

if fruit / 2 >= 2 and (fruit % 2 == 1 or fruit % 3 == 2):

print('apples')

else:

print('oranges')

18 of 41

Logical Operators

19 of 41

True or False

Have you been to Nepal and Nigeria?

Have you been to Nepal or Nigeria?

Are you not had coffee or tea this morning?

Do we have Monday and not Tuesday off for Labor Day?

20 of 41

Objective: Logical Operators

What are they?

Why are they important?

Examples

21 of 41

Logical Operators

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

Logical operators: combine booleans

  • and
  • or
  • not

22 of 41

Logical Operators: and

Peanut

Butter

Jelly

Peanut butter

Jelly

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

23 of 41

Practice: and

chocolate and strawberry

chocolate and chocolate

False

True

Milkshake Time!

vanilla = True

chocolate = True

strawberry = False

vanilla and strawberry

chocolate and vanilla

vanilla and chocolate

True

strawberry and strawberry

False

False

True

24 of 41

Logical Operators: or

Sneakers

Flip Flops

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

Also includes this

25 of 41

Practice: or

biking or running

biking or biking

True

True

Outdoor Activities!

hiking = True

running = False

biking = False

hiking or running

biking or hiking

hiking or biking

True

running or biking

False

False

False

26 of 41

Logical Operators: not

not inverts (gives opposite value of) the value of its operand

Sneakers

Flip Flops

Sleep Deprived

not Sleep Deprived

True

False

False

True

Not Sleep Deprived

Sleep

Deprived

27 of 41

Practice: not

False

True

Favorite Colors!

blue = True

red = False

green = True

not blue

not red

not green

False

28 of 41

How is not different from and and or?

29 of 41

Unary

Binary

operates on only one variable, like not

example:

not standing

operates on two variables, like and, or

examples:

brother and sister

hot or cold

30 of 41

Combining Operators

Remember PEMDAS? Order of operations for arithmetic operators

Similarly...

PNAO!

(Parentheses, not, and, or)

31 of 41

Practice: Combining Logical Operators

True

False

True and not False

(False or True) and False

not (True or False) or True

True

32 of 41

Practice: Combining Operators

True

False

Animals on a Farm!

dog = False

chicken = True

sheep = True

not dog or not chicken and sheep

chicken and dog or not sheep

dog and not sheep or chicken

True

not chicken or not sheep or dog

False

33 of 41

Let’s Code!

34 of 41

One last note...

answer = input("What city is Fisk in?")

if answer == "nashville" or "Nashville":

print("Correct!")

Find the bug!

35 of 41

One last note...

answer = input("What city is Fisk in?")

if answer == "nashville" or "Nashville":

print("Correct!")

Evaluates this first

36 of 41

One last note...

answer = input("What city is Fisk in?")

if answer == "nashville" or "Nashville":

print("Correct!")

then evaluates this???

37 of 41

One last note...

answer = input("What city is Fisk in?")

if answer == "nashville" or "Nashville":

print("Correct!")

Python considers "" (empty string) to be FALSE

Python considers all other strings to be TRUE

38 of 41

One last note...

answer = input("What city is Fisk in?")

if answer == "nashville" or "Nashville":

print("Correct!")

Because OR only needs one condition to be True,

this entire statement will always be True

39 of 41

One last note...

answer = input("What city is Fisk in?")

if answer == "nashville" or answer == "Nashville":

print("Correct!")

THIS IS RIGHT

40 of 41

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

Why are they important?

Enable programs to make decisions based on multiple conditions

41 of 41

Questions?