Skip to content

Chapter 5 :Programming for Everybody (Getting Started with Python) (Python for Everybody Specialization) Answers 2025

Question 1

What is wrong with this Python loop:

n = 5
while n > 0 :
print(n)
print('All done')

❌ This loop will run forever
✅ The print('All done') statement should be indented four spaces
❌ There should be no colon on the while statement
while is not a Python reserved word

Explanation:
The code as shown does not change n inside the loop, so n > 0 stays true and the loop never terminates → infinite loop. (Indenting print('All done') would only change when that line executes; the core bug is the missing decrement, causing an endless loop.)


Question 2

What does the break statement do?

✅ Exits the currently executing loop
❌ Resets the iteration variable to its initial value
❌ Exits the program
❌ Jumps to the “top” of the loop and starts the next iteration

Explanation:
break immediately terminates the innermost loop and execution continues with the first statement after that loop.


Question 3

What does the continue statement do?

❌ Exits the program
❌ Resets the iteration variable to its initial value
✅ Jumps to the “top” of the loop and starts the next iteration
❌ Exits the currently executing loop

Explanation:
continue skips the rest of the current loop iteration and starts the next iteration of that same loop.


Question 4

What does this program print out?

tot = 0
for i in [5, 4, 3, 2, 1] :
tot = tot + 1
print(tot)

❌ 10
❌ 0
❌ 15
✅ 5

Explanation:
The loop runs once for each element (5 elements). Each iteration increments tot by 1 → final tot is 5.


Question 5

What is the iteration variable in the code:

friends = ['Joseph', 'Glenn', 'Sally']
for friend in friends :
print('Happy New Year:', friend)
print('Done!')

friend
Glenn
Sally
friends

Explanation:
In for friend in friends:, friend is the iteration variable that takes each element of the list in turn.


Question 6

Good description of this code:

zork = 0
for thing in [9, 41, 12, 3, 74, 15] :
zork = zork + thing
print('After', zork)

❌ Find the largest item in a list
✅ Sum all the elements of a list
❌ Count all of the elements in a list
❌ Find the smallest item in a list

Explanation:
zork accumulates (+=) each thing → final value is the sum of the list.


Question 7

What will this code print out?

smallest_so_far = -1
for the_num in [9, 41, 12, 3, 74, 15] :
if the_num < smallest_so_far :
smallest_so_far = the_num
print(smallest_so_far)

-1
74
42
3

Explanation:
smallest_so_far starts at -1. No list element is < -1, so it never changes and -1 is printed. (This demonstrates a logic bug if the intent was to find the minimum.)


Question 8

What is a good statement to describe the is operator as used here:

if smallest is None :
smallest = value

✅ matches both type and value
❌ Is true if the smallest variable has a value of -1
❌ The if statement is a syntax error
❌ Looks up ‘None’ in the smallest variable if it is a string

Explanation:
is tests identity (often described as matching the same object). For None the correct idiom is x is None. (This option is the closest correct description among those given — is confirms the object is the same singleton None.)


Question 9

Which reserved word indicates the start of an “indefinite” loop in Python?

❌ for
✅ while
❌ def
❌ break
❌ indef

Explanation:
while starts a loop that continues until its condition becomes false — an indefinite loop controlled by a boolean condition.


Question 10

How many times will the body of this loop be executed?

n = 0
while n > 0 :
print('Lather')
print('Rinse')
print('Dry off!')

0
1
❌ This is an infinite loop
5

Explanation:
The condition n > 0 is false at the start (n = 0), so the loop body runs 0 times; only print('Dry off!') executes.


🧾 Summary Table

Q# Correct Answer Key concept
1 The loop will run forever (infinite loop due to no decrement) Loops must change loop variable or have exit condition
2 break exits the current loop Loop control: break
3 continue jumps to next iteration Loop control: continue
4 5 Loop iteration count
5 friend Iteration variable in for
6 Sum all elements Accumulator pattern
7 -1 Initial value logic bug / no update occurs
8 is checks identity (matches object like None) Identity comparison, use x is None
9 while Indefinite loop keyword
10 0 Loop condition false initially → zero iterations