Booleans Flashcards

1
Q

What are booleans?

A

Booleans are operators that allow you to convey True or False statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

We may need to know whether a certain condition has happened is True in order to execute the correct code. Show an example.

A

“Is my pool empty?”
if that is True then turn on the water to fill the pool
else, do nothing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The parameter weekday is True if it is a weekday, and the parameter vacation is True if we are on vacation. We sleep in if it is not a weekday or we’re on vacation. Return True if we sleep in.
def sleep_in(weekday, vacation):

A

def sleep_in(weekday, vacation:
if not weekday or vacation:
return True
else:
return False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

We have two monkeys, a and b, and the parameters a_smile and b_smile indicate if each is smiling. We are in trouble if they are both smiling or if neither of them is smiling. Return True if we are in trouble.
def monkey_trouble(a_smile, b_smile):

A

def monkey_trouble(a_smile, b_smile):
if a_smile and b_smile:
return True
if not a_smile and not b_smile:
return True
else:
return False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

We have a loud talking parrot. The “hour” parameter is the current hour time in the range 0..23. We are in trouble if the parrot is talking and the hour is before 7 or after 20. Return True if we are in trouble.
def parrot_trouble(talking, hour):
–Use # to comment solution

A

def parrot_trouble(talking, hour):
return (talking and (hour < 7 or hour > 20))
# Need extra parenthesis around the or clause
# since and binds more tightly than or.
# and is like arithmetic *, or is like arithmetic +

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

1 > 2

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

1 == 1

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the operator == ?

A

if two values of two operands are equal, then the condition becomes true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the operator != ?

A

if the values of two operands are not equal, then condition becomes true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the operator > ?

A

if the value of the left operand is greater than the value of the right operand, then condition becomes true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the operator < ?

A

if the left operand is less than the value of the right operand, then the condition beecomes true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the operand >= ?

A

if the value of the left operand is greater than or equal to the value of the right operand, then condition becomes true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the operand <= ?

A

if the value of the left operand is less than or equal to the value of the right operand, then condition becomes true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

a = 3
b = 4
return (a == b)

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

a = 3
b = 4
return (a !=b)

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

a = 4
b = 5
return (a >b)

A

false

17
Q

a = 6
b = 4
return (a <b)

A

false

18
Q

a = 3
b = 4
return (a >= b)

A

true

19
Q

a = 4
b = 5
return (a <=b)

A

true