Python Chapter 4
What is a flag and how does it work?
A flag is a variable that signals when some condition exists in the program. When the flag variable is set to False, it indicates the condition does not exist. When the flag variable is set to True, it means the condition does exist.
What is a control structure?
A logical design that controls the order in which a set of statements execute
What is a flag variable?
A variable that signals when some condition exists in the program
What would the following code display? s1 = 'New York' s2 = 'Boston' if s1 s2: print(s2) print(s1) else: print(s1) print(s2)
Boston New York
Explain what is meant by the term "conditionally executed."
Conditionally executed means an action is only executed when the conditions are true
You need to test a condition and then execute one set of statements if the condition is true. If the condition is false, you need to execute a different set of statements. What structure will you use?
Dual Alternative structure
What is a decision structure?
It is a program structure that can execute a set of statements only under certain circumstances.
What is a compound Boolean expression?
It is an expression that is created by using a logical operator to combine two Boolean subexpressions.
When determining whether a number is inside a range, which logical operator is it best to use?
It is best to use the AND operator
Briefly describe how the and operator works
It requires both/all conditions to be met for an action to execute.
Briefly describe how the or operator works
It requires either of the listed conditions to be true, for the action to work.
Explain how short-circuit evaluation works with the and and or operators.
The and operator: If the expression on the left side of the and operator is false, the expression on the right side will not be checked. The or operator: If the expression on the left side of the or operator is true, the expression on the right side will not be checked.
What values can you assign to a bool variable?
True or False
Convert the following code to an if-elif-else statement: if number == 1: print('One') else: if number == 2: print('Two') else: if number == 3: print('Three') else: print('Unknown')
if number == 1: print('One') elif number == 2: print('Two') elif number == 3: print('Three') else: print('Unknown')
Write an if statement that displays the message "The number is not valid" if the value referenced by speed is outside the range 0 through 200.
if speed < 0 or speed > 200: print('The number is not valid')
Write an if statement that displays the message "The number is valid" if the value referenced by speed is within the range 0 through 200.
if speed >= 0 and speed <= 200: print('The number is valid')
Write an if statement that assigns 20 to the variable y and assigns 40 to the variable z if the variable x is greater than 100.
if x > 100: y = 20 z = 40
What would the following code display? if 'z' 'a': print('z is less than a.') else: print('z is not less than a.')
z is not less than a.