Chapter 2: Flow Control
How many times will this range function loop? for i in range(12, 16):
4 times, starting at 12 and ending at 15 12, 13, 14, 15
How many times will this range function loop? for i in range(0, 10, 2):
5 times, starting at 0, increasing by 2 each time, stopping before 10 0, 2, 4, 6, 8
How many times will this range function loop? for i in range(5):
5 times. Starting at 0 and ending at 4. 0, 1, 2, 3, 4
What is the meaning of the following relational operators? A. == B. != C. < D. > E. <= F. >=
A. == equals B. != not equal to C. < less than D. > greater than E. <= less than or equal to F. >= greater than or equal to
An "if" control statement will execute if.... A. The condition is true B. The condition is false C. The condition is either true or false D. The condition is neither true or false
A. The condition is true (p. 28)
You can cause a Python program to terminate by calling the function. A. sys.exit() B. print() C. input() D. str()
A. sys.exit()
How many options does a Boolean type variable have? A. 1 B. 2 C. 3 D. 4 E. Unlimited
B. 2. A Boolean data type had two options, True and False. (p. 22)
What is the difference between break and continue?
Break ends the loop where continue returns to the beginning of the loop.
What is the correct syntax for a Boolean value? A. true B. 'true' C. True D. 'True'
C. True. Boolean values are True or False, and are always capitalized. (p. 22)
What keys can you press if your code is stuck in an infinite loop?
CTRL+C (p. 41)
Which of the following are rules for blocks of code in Python? A. Blocks begin when the indentation increases. B. Blocks can contain other blocks. C. Blocks end when the indentation decreases to zero or to a containing block's indentation D. All of above
D. All of above (p. 27)
Which of the following are rules to follow with if, else, and elif statements? A. There is always only one if statement. B. Any elif statements should follow an if statement. C. If you want to be sure at least one clause is executed, finish with an else statement. D. All of the above.
D. All of the above. (p. 34-35)
You can make a block of code execute over and over again with a(n) _________________ statement. A. emphatic B. if C. if-elif-else D. while
D. while (p. 35)
Which are components of an "if" statement? A. The "if" keyword B. A condition C. A colon D. An indented block of code on the next line called the clause E. All of the above
E. All of the above (p. 29) if password =='Mary': print('Access Granted')
True or False: An "else" statement has a condition?
False. An else statement does NOT have a condition. It is what happens automatically when the condition of an if statement is not evaluated as True. (p. 29) else: print ('Wrong Password')
True or False: An "if" statement always needs to be followed by an "else" statement?
False. The else statement is optional. (p. 29)
If both the "if" statement and the "elif" statement conditions are false, the the clauses are ______________.
If both the "if" statement and the "elif" statement conditions are false, the the clauses are skipped. (p. 31)
What is the difference between range(10), range (0, 10), and range (0, 10, 1)?
Nothing. They all start at 0, continue in increments of 1 until they reach 9.
What is the similarities between an "else" statement and an "elif" statement? What is the differences between an "else" statement and an "elif" statement?
Similarities: both always follow an if statement Differences: an else statement does not have a condition, an elif statement does have a condition if name == 'Alice': print('Hi Alice.') elif age < 12: print('You are too young.')
The "elif" statement executes only if the "if" statement is ___________ and it's condition is ______________.
The "elif" statement executes only if the "if" statement is False and it's condition is True. (p. 30-31)
The __________ statement forces an immediate end to a while loop's clause.
The break statement forces an immediate end to a while loop's clause. (p. 39)
What happens if the the "if" statement is evaluated to False?
The clause is skipped. (p. 28)
The __________ statement forces an immediate return to the start of a while loop's clause.
The continue statement forces an immediate return to the start of a while loop's clause. (p. 40)
True or False: Flow control statements can decide which Python instructions to execute under which conditions.
True (p. 21)
True or False: A truth table shows every possible result of a Boolean operator (and, or, not).
True (p. 25-26) True and True...evaluates to...True True and False...evaluates to...False False and True...evaluates to...False False and False...evaluates to...False True or True...evaluates to...True True or False...evaluates to...True False or True...evaluates to...True False or False...evaluates to...False
True or False: Conditions always evaluated down to a Boolean value, True or False.
True (p. 27)
True or False: Flow control statements often start with a part called the condition, and all are followed by a block of code called the clause.
True (p. 27)
What function can you use to change the negative value -1 to 1?
abs(-1)
What are the three (3) Boolean operators? (Don't confuse these with Boolean values!)
and or not (p. 25)
Write a short program that prints the numbers 1 to 10 using a for loop.
for i in range(1,11): print(i)
Write a short program that prints the numbers 1 to 10 using a while loop.
i = 1 while i < = 10: print(i) i = i + 1
What are some flow control statements? (There are 7 in Ch 2.)
if else elif while break continue for
If you had a function named bacon( ) inside a module named spam, how would you call it after importing spam?
import spam spam.bacon( ) (p. 48)
Write a short program that prints "Hello!" if 1 is stored as spam, "Howdy!" if 2 is stored as spam, or "Greetings!" if anything else is stored as spam.
print('Please enter an integer.') spam = int(input()) if spam == 1: print('Hello!') elif spam ==2: print('Howdy!') else: print('Greetings!')
What function can you use to round the floating point number 3.412 to integer value of 3?
round(3.412)