Logical Operators
Jake Shoudy
August 22, 2022
CSCI 110 - Lecture 6
Today’s Agenda
|
|
| |
| |
| |
| |
Announcements
Pre-class Surveys
Two surveys posted on Ed
Please take them 🙏 🙏 🙏
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
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.
Project 1: Part 1
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% |
Quiz 1 Notes
TA Office Hours
Recap
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
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
Review: I/O
age = int(input(“how old are you today? ”))
print(“Next year you will be “ + str(age + 1) + “ years old”)
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')
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
Review: Conditionals & I/O
fruit = int(input())
if fruit / 2 >= 2 and (fruit % 2 == 1 or fruit % 3 == 2):
print('apples')
else:
print('oranges')
Logical Operators
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?
Objective: Logical Operators
What are they?
Why are they important?
Examples
Logical Operators
Reminder: Arithmetic operators (+, -, *, /) to combine numbers
Logical operators: combine booleans
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
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
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
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
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
Practice: not
False
True
Favorite Colors!
blue = True
red = False
green = True
not blue
not red
not green
False
How is not different from and and or?
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
Combining Operators
Remember PEMDAS? Order of operations for arithmetic operators
Similarly...
PNAO!
(Parentheses, not, and, or)
Practice: Combining Logical Operators
True
False
True and not False
(False or True) and False
not (True or False) or True
True
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
Let’s Code!
One last note...
answer = input("What city is Fisk in?")
if answer == "nashville" or "Nashville":
print("Correct!")
Find the bug!
One last note...
answer = input("What city is Fisk in?")
if answer == "nashville" or "Nashville":
print("Correct!")
Evaluates this first
One last note...
answer = input("What city is Fisk in?")
if answer == "nashville" or "Nashville":
print("Correct!")
then evaluates this???
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
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
One last note...
answer = input("What city is Fisk in?")
if answer == "nashville" or answer == "Nashville":
print("Correct!")
THIS IS RIGHT
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
Questions?