NEW
Font size
WorksheetsPython - Chpt 4 - Review
Total questions: 25
Worksheet time: 13mins
What would be the output of the following code:
numbers = [1, 1, 2, 3]
for number in numbers:
if number % 2 == 0:
continue
print(number)
1 1 2
2
1 1
1 1 3
What would be the output of the following code:
numbers = [2, 4, 6, 8]
for number in numbers:
print("hello!")
hello!hello!hello!hello!
hello!
2 4 6 8
2hello! 4hello! 6hello! 8hello!
What would be the output of the following code:
for i in range(3):
print(5)
5 5 5
3 3 3 3 3
0 1 2
0 1 2 3 4
What would be the output of the following code:
for i in range(3):
print(i)
i i i i
0 1 2
i i i
1 2 3
What would be the output of the following code:
drink_choices = ["coffee", "tea", "water", "juice", "soda"] for for drink in drink_choices:
print(drink)
"coffee"
"coffee" "tea" "water" "juice" "soda"
drink
["coffee", "tea", "water", "juice", "soda"]
What would be the output of the following code:
numbers = [1, 1, 2, 3]
for number in numbers:
if number % 2 == 0:
break
print(number)
1 1 3
2
1 1
1 1 2 3
Fill in the blank with the appropriate while condition in order to print the numbers 1 through 10 in order:
i = 1
____________
print(i)
i += 1
while i <= 10:
while i > 10:
while i < 10:
while i == range(11)
Which of the following for loops would print the following numbers?
0
1
2
3
4
5
for i in range(5):
print(i)
for i in range(1, 5, 1):
print(i)
for i in range (6):
print(i)
for i in range(0, 5, 1):
print(i)
Which of the following for loops would print the following numbers?
3
5
7
9
for i in range(3, 10, 2):
print(i)
for i in range(3, 9, 2):
print(i)
for i in range(9):
print(i)
for i in range(3,9):
print(i)
What is the value of num when this loop completes?
num = 0
for i in range(2, 8, 2):
num = num + i
2
8
12
20
Which of the following for loops will give the same values for i as the loop below?
for i in range(10):
for i in range(11,0):
for i in range(10, 0, -1):
for i in range(0, 10):
for i in range(0, 11, 1):
Three of the for loops below will provide identical values for i. Which for loop will provide values that do not match the others?
for i in range(0, 5):
for i in range(5):
for i in range(0, 5, 1):
for i in range(5, 0, 1):
What does this program print?
temperature = 65
while temperature < 80:
print "It's still a nice day"
if temperature < 70:
temperature = temperature + 5
elif temperature > 75:
break
else:
temperature = temperature + 3
It's still a nice day
It's still a nice day
It's still a nice day
It's still a nice day
It's still a nice day
It's still a nice day
It's still a nice day
It's still a nice day
It's still a nice day
It's still a nice day
It's still a nice day
It's still a nice day
It's still a nice day
It's still a nice day
Which of the following while loops would continue to loop as long as num has values in the range 3 to 12, exclusive?
while num > 12 and num < 3:
# do something with num
while num < 12 and num < 3:
# do something with num
while num <= 12 and num >= 3:
# do something with num
while num < 12 and num > 3:
# do something with num
Which of the following while loops will cause an infinite loop?
secret_num = 10
while secret_num == 10:
secret_num = secret_num - 1
secret_num = 10
while secret_num == 10:
print(secret_num)
secret_num = 10
while secret_num != 10:
print(secret_num)
secret_num = 10
while secret_num > 0:
secret_num = secret_num - 1
When would a while loop be a better control structure to use than a for loop?
When you need to repeat multiple commands
When you need to repeat something 5 times
When you don't know how many times something will need to repeat
When the user is inputting how many times to repeat something
What will be printed to the screen when this program is run?
for j in range(2):
for i in range(0, 2, 1):
print i
0
1
0
1
0
2
0
1
0
1
0
1
1
2
1
2
1
2
What is the value of sum when this loop completes?
sum = 0
for i in range(3):
sum = sum + 5
for j in range(2):
sum = sum - 1
8
9
20
0
What will be printed to the screen when this program is run?
num = 100
while num > 0:
for i in range(100, 0, -25):
num = num - i
print num
100
0
-75
-125
-150
-150
-175
175
What does this program print?
for i in range(10):
if i == 3:
continue
print i
0
1
2
0
1
2
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
0
1
2
3
What is the benefit of using the break command as in the program below?
magic_number = 10
while True:
guess = int(input("Guess my number: "))
if guess == magic_number:
print "You got it!"
break
print "Try again!"
The user can enter as many answers as they want
The program will break out of the loop as soon as the user enters the magic_number
The loop will prompt the user to enter a number no matter what input they give
The loop will give the magic_number after a certain of tries
for loops ______, meaning it marches through the sequence one element at a time.
sequencing
iterate
index
slice
If the user enters ‘4’, I want the loop to exit. Where should the break command be placed in order to end the loop when the user enters this value?
On line 3
On line 5
On line 7
On line 9
The _____ command allows you to stop a for loop from inside the loop.
break
pass
continue
skip
Specifying a position (or index) number in a sequence and then getting the element at that position is called ____.
sequencing
accessing
slicing
indexing
