control flow Flashcards

1
Q

what are conditional statments

A

This is the control flow of your program. In Python, your script will execute from the top down, until there is nothing left to run. It is your job to include gateways, known as conditional statements, to tell the computer when it should execute certain blocks of code. If these conditions are met, then run this function.

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

what is boolean expression

A

n order to build control flow into our program, we want to be able to check if something is true or not. A boolean expression is a statement that can either be True or False.

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

what does a Relational operators do

A

Relational operators compare two items and return either True or False. For this reason, you will sometimes hear them called comparators.

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

what does == and != do

A

Equals: ==
Not equals: !=
1 == 1 # True

2 != 4 # True

3 == 5 # False

‘7’ == 7 # False

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

is Friday is the best day of the week a boolean expression

A

No, this statement is an opinion and is not objectively True or False

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

how do we create booleans

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

why does True and False appear in different color

A

This is because True and False are their own special type: bool.

True and False are the only bool types, and any variable that is assigned one of these values is called a boolean variable.

Boolean variables can be created in several ways. The easiest way is to simply assign True or False to a variable:

set_to_true = True
set_to_false = False

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

how do you create a boolean varible

A

Boolean variables can be created in several ways. The easiest way is to simply assign True or False to a variable:

set_to_true = True
set_to_false = False

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

how do you variable equal to a boolean expression.

A

bool_one = 5 != 7
bool_two = 1 + 1 != 2
bool_three = 3 * 3 == 9
These variables now contain boolean values, so when you reference them they will only return the True or False values of the expression they were assigned.

print(bool_one) # True

print(bool_two) # False

print(bool_three) # True

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

What is an if statement

A

If “it is raining” == True then the rest of the conditional statement will be executed and you will bring an umbrella.

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

what is the colon : used for in if statments

A

if is_raining:
print(“bring an umbrella”)

You’ll notice that instead of “then” we have a colon, :. That tells the computer that what’s coming next is what should be executed if the condition is met.

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

what are all the other relational operator symbols

A

> greater than
>= greater than or equal to
< less than
<= less than or equal to

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

How would you set a write a program that checks if our users are over 13 when showing them a PG-13 movie

A

if age <= 13:
print(“Sorry, parental control required”)

This function will take the user’s age and compare it to the number 13. If age is less than or equal to 13, it will print out a message.

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

what is an boolean operateor

A

you can build larger boolean expressions using boolean operators. These operators (also known as logical operators) combine smaller boolean expressions into larger boolean expressions.

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

what are the 3 boolean operators

A
  • and
  • or
  • not
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what does ( and ) boolean operator do

A

and combines two boolean expressions and evaluates as True if both its components are True, but False otherwise.

(1 + 1 == 2) and (2 + 2 == 4) # True

14
Q

what does the boolean opertor ( or ) do

A

The boolean operator or combines two expressions into a larger expression that is True if either component is True.

15
Q

does only one statment need to be true for the whole or statment to be true

A

yes - Oranges are a fruit or apples are a vegetable.

This statement is composed of two expressions: oranges are a fruit which is True and apples are a vegetable which is False. Because the two expressions are connected by the or operator, the entire statement is True. Only one component needs to be True for an or statement to be True.

16
Q

what is the ( not )

A

when applied to any boolean expression it reverses the boolean value. So if we have a True statement and apply a not operator we get a False statement.

not True == False
not False == True

17
Q

how would we add a not operator in pyhton

A

we add the not operator to the very beginning of the statement. Let’s take a look at some of those:

not 1 + 1 == 2 # False
not 7 < 0 # True

18
Q

what is an example of not

A

if not gpa >= 2.0:
print (“Your GPA is not high enough to graduate.”)

19
Q

what is the esle statment used for

A

else statements allow us to elegantly describe what we want our code to do when certain conditions are not met.

else statements always appear in conjunction with if statements. Consider our waking-up example to see how this works:

if weekday:
print(“wake up at 6:30”)
else:
print(“sleep in”)

20
Q

what is a else if statment (elif)

A

An elif statement checks another condition after the previous if statements conditions aren’t met

21
Q

what is can example of ( elif )

A

print(“Thank you for the donation!”)

if donation >= 1000:
print(“You’ve achieved platinum status”)
elif donation >= 500:
print(“You’ve achieved gold donor status”)
elif donation >= 100:
print(“You’ve achieved silver donor status”)
else:
print(“You’ve achieved bronze donor status”)

Take a second to think about this function. What would happen if all of the elif statements were simply if statements? If you donated $1100.00, then the first three messages would all print because each if condition had been met.

But because we used elif statements, it checks each condition sequentially and only prints one message. If I donate $600.00, the code first checks if that is over 1000, which it is not, then it checks if it’s over 500, which it is, so it prints that message, then because all of the other statements are elif and else, none of them get checked and no more messages get printed.

22
Q
A