1 of 52

Booleans and Conditionals

Jake Shoudy

Aug 17, 2021

CSCI 110 - Lecture 4

2 of 52

Today’s Agenda

  • Recap
  • Comparison operators
  • Conditionals

3 of 52

Announcements

4 of 52

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

5 of 52

Reminder: HW0

  • Post on EdStem!
  • Due today!

6 of 52

HW1 Posted

  • Available on EdStem under the “Lessons” tab
  • Due next Wednesday
  • If you submitted the hw already “Evaluating expressions” question 4 has changed. PLEASE DOUBLE CHECK.

7 of 52

Pre-class Surveys

Two surveys posted on Ed

Please take them 🙏 🙏 🙏

8 of 52

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.

9 of 52

Fisk CS Club

10 of 52

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

11 of 52

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

12 of 52

Recap

13 of 52

Review: String operators

+ Add

  • Concatenate (combine) two strings
  • Must be used on two strings

* Multiply

  • Repeat a string
  • Must be used on a string and an int

14 of 52

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

15 of 52

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

16 of 52

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

17 of 52

Review: Variables

> firstName = "Jake”

> lastName = "Shoudy"

> print("Hello, " + firstName + “ “ + lastName)

18 of 52

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

19 of 52

Motivating Example

20 of 52

Is order over $25?

yes

Shipping is free!

no

Shipping is $5.99

21 of 52

order_cost = 26.95�print(“Shipping is free!”)

print(“Shipping is $5.99”)

22 of 52

if [something_is_true]:

[do_something]

else:

[do_something_else]

Conditional

23 of 52

Comparison Operators

24 of 52

Objective: Comparison Operators

What are they?

Why are they important?

Examples

25 of 52

Remember these?

<

=

>

less than

less than or equal to

greater than or equal to

greater than

equal to

not equal to

26 of 52

Comparison Operators

<

<=

==

>

>=

!=

less than

less than or equal to

greater than or equal to

greater than

equal to

not equal to

27 of 52

What's up with equals?

=

assignment operator

think gets the value

my_var = 20

==

equality operator

think is equivalent to?

22 == 23

28 of 52

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

29 of 52

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

30 of 52

Conditionals

31 of 52

Objective: Conditionals

What are they?

Why are they important?

Examples

32 of 52

Is order over $25?

yes

Shipping is free!

no

Shipping is $5.99

33 of 52

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

34 of 52

order > 25

otherwise

Free shipping!

$5.99 shipping

35 of 52

If Statement

if order > 25:

shipping = 0.0

else:

shipping = 5.99

indented!

36 of 52

If statements

if [condition]:� [execute this code]

if this CONDITIONAL evaluates to True

37 of 52

If statements

if [condition]:� [execute this code]

run the code that is INDENTED after if statement

38 of 52

If statements

if [condition]:� [execute this code]

Indent is important! It's how program knows that this code "belongs" to this if statement.

39 of 52

If statements

if [condition]:� [execute this code]� [execute this code]

If there are two lines of indented code, both lines "belong" to if statement.

40 of 52

if order > 25:

shipping = 0.0

else:

shipping = 5.99

print('Total: ' + str(order + shipping))

order = 30

True

skip

41 of 52

What if order isn’t enough for free shipping?

42 of 52

if order > 25:

shipping = 0.0

else:

shipping = 5.99

print('Total: ' + str(order + shipping))

order = 20

False

run this!

43 of 52

Conditionals in the wild!

44 of 52

Conditionals in the wild

if you are wearing a black shirt:

raise your hand

Black shirt?

yes

no

Raise your hand

Start

Do nothing

45 of 52

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

46 of 52

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

47 of 52

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

48 of 52

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

49 of 52

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

50 of 52

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)

51 of 52

Review: Conditionals

What are they?

if, elif, and else used to make decisions

Why are they important?

Allow for different behaviors based on conditions

52 of 52

Questions?