Booleans and Conditionals
Jake Shoudy
Aug 17, 2021
CSCI 110 - Lecture 4
Today’s Agenda
|
|
| |
| |
| |
| |
Announcements
Reminder: Quizzes
Stop by my OH by this Friday (August 19th) to get 100% on quiz #0!
Quiz 1 will be this Friday in class! It will be the first 15 minutes of class on Edstem
Reminder: HW0
|
|
| |
| |
| |
| |
HW1 Posted
|
|
| |
| |
| |
| |
Pre-class Surveys
Two surveys posted on Ed
Please take them 🙏 🙏 🙏
CS Emailing List
Posted in Ed.
If you are planning to be a Computer Science or Data Science minor or major fill out this form. Dr. Qian will be using it to create an email list that will be used for sending out information about important events, scholarships, internships, etc.
Fisk CS Club
Fisk Hackathon Hosted by Equinix
Sign up at: Programming problems and Competitions :: HackerRank
Timing: Aug 18 2022, 11:30 am to Aug 18 2022, 1:00 pm
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
Recap
Review: String operators
+ Add
* Multiply
Review: Built-in functions
int() converts input value to an integer
float() converts input value to a float
str() converts input value to a string
bool() converts input value to a boolean
type() determines data type of input value
print() displays input value in console
Review: Practice Expressions
int(str(7) * 3)
int(‘ 3 ‘)
int(‘3 5’)
bool(“False”)
bool(-4)
float(‘1’ + ‘2’) + 3
int(-6.99)
int(‘3.0’)
int(float(‘3.0’))
bool(“”)
777
3
ValueError
True
True
15.0
-6
ValueError
3
False
Review: Variables
What are they?
Give name to value and variable gets assigned: [variable name] = [value]
First time variable is assigned: declare variable, then access variable value with variable name
Why are they important?
Know what a value is representing
Reuse and change value throughout program
Review: Variables
> firstName = "Jake”
> lastName = "Shoudy"
> print("Hello, " + firstName + “ “ + lastName)
What's in my box?
wishing_well = 3
wishing_Well = ‘easy money’
my_box = wishing_well * 2
wishing_well = my_box * 2
my_box = my_box * my_box - my_box
wishing_well = 12
wishing_Well = “easy money”
my_box = 30
Motivating Example
Is order over $25?
yes
Shipping is free!
no
Shipping is $5.99
order_cost = 26.95�print(“Shipping is free!”)
print(“Shipping is $5.99”)
if [something_is_true]:
[do_something]
else:
[do_something_else]
Conditional
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 gets the value
my_var = 20
==
equality operator
think is equivalent to?
22 == 23
Practice!
5 < 6
10 > 15
4 + 3 <= 7
1 * 3 != 10
“55” == “5” * 2
9 != 3 * 3
'hi' == 'bye'
2 != 1 + 1
True
False
True
True
True
False
False
False
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
Conditionals
Objective: Conditionals
What are they?
Why are they important?
Examples
Is order over $25?
yes
Shipping is free!
no
Shipping is $5.99
Making Decisions
conditional: control structure to run code block only if a certain condition is met
condition: expression that evaluates to boolean value
Conditionals are often referred to as if statements
order > 25
otherwise
Free shipping!
$5.99 shipping
If Statement
if order > 25:
shipping = 0.0
else:
shipping = 5.99
indented!
If statements
if [condition]:� [execute this code]
if this CONDITIONAL evaluates to True
If statements
if [condition]:� [execute this code]
run the code that is INDENTED after if statement
If statements
if [condition]:� [execute this code]
Indent is important! It's how program knows that this code "belongs" to this if statement.
If statements
if [condition]:� [execute this code]� [execute this code]
If there are two lines of indented code, both lines "belong" to if statement.
if order > 25:
shipping = 0.0
else:
shipping = 5.99
print('Total: ' + str(order + shipping))
order = 30
True
skip
What if order isn’t enough for free shipping?
if order > 25:
shipping = 0.0
else:
shipping = 5.99
print('Total: ' + str(order + shipping))
order = 20
False
run this!
Conditionals in the wild!
Conditionals in the wild
if you are wearing a black shirt:
raise your hand
Black shirt?
yes
no
Raise your hand
Start
Do nothing
Conditionals in the wild
if you're a coffee drinker:
raise your right hand
stand up
else:
raise your left hand
Drink coffee?
yes
no
Raise your right hand
Start
Raise your left hand
standup
Conditionals in the wild
if you play an instrument:
touch your nose
else:
touch your ears
stand up
Instrument?
no
Touch your nose
Start
Touch your ears
yes
Stand up
Conditionals in the wild
if you like dogs:
salute
if you like cats:
stand up
raise your left hand
Dogs?
no
Salute
Start
yes
Raise left hand
Cats?
no
Stand up
yes
Conditionals in the wild
if you like cookies:
show me a peace sign
else:
if you like brownies:
hands in armpits
else:
cover your eyes
Do you like cookies?
yes
no
Do you like brownies?
Peace sign
Hands in armpits
Cover eyes
yes
no
Start
Elif
if you like cookies:
show me a peace sign
elif you like brownies:
hands in armpits
else:
cover your eyes
Do you like cookies?
yes
no
Do you like brownies?
Peace sign
Hands in armpits
Cover eyes
yes
no
Start
If / elif / else: what code will run?
if or elif blocks execute (aka expression evaluates to True), then no other code will execute (else will NOT execute)
else will only execute when NONE of the if and elif expressions evaluate to True (aka they all evaluate to False)
Review: Conditionals
What are they?
if, elif, and else used to make decisions
Why are they important?
Allow for different behaviors based on conditions
Questions?