Chapter 0 Β· Section 3 practice
Branches, Loops and Comprehensions
These questions review the examples in the companion slides. Choose an answer, then check the explanation.
Question 1
score = 95. if score >= 60: grade = βCβ then elif score >= 90: grade = βAβ. What is grade?
"C"β the first true branch wins"A"β the best matching branch wins- Both run;
gradeends as"A" - A
SyntaxErrorβ thresholds must be increasing
Question 2
What does list(range(1, 5)) return?
[1, 2, 3, 4, 5][1, 2, 3, 4][0, 1, 2, 3, 4][1, 5]
Question 3
for x in range(1, 11): if x % 3 == 0: continue; print(x, end=β β). Output?
1 23 6 91 2 4 5 7 8 101 2 3 4 5 6 7 8 9 10
Question 4
What does [x**2 for x in range(10) if x % 2 == 0] return?
[0, 4, 16, 36, 64][0, 1, 4, 9, 16, 25, 36, 49, 64, 81][1, 9, 25, 49, 81][0, 4, 16, 36, 64, 100]
Question 5
How many elements does [βevenβ if x % 2 == 0 else βoddβ for x in range(6)] have?
- 3
- 0
- 6 β every element gets a label
- It is a
SyntaxError