Skip to content

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

Question 1

What do we do to a Python statement immediately after an if statement to indicate that it should only execute when the condition is true?
βœ… Indent the line below the if statement
❌ Start with #
❌ Underline it
❌ Begin with {

🧠 Explanation:
In Python, indentation defines code blocks. Code indented under an if statement executes only if the condition is true.


Question 2

Which of these operators is not a comparison/logical operator?
βœ… =
❌ !=
❌ ==
❌ <=
❌ >=

🧠 Explanation:
= is the assignment operator, not a comparison operator.
Comparisons use ==, !=, <, >, <=, >=.


Question 3

What is true about the following code segment?

if x == 5:
print('Is 5')
print('Is Still 5')
print('Third 5')

βœ… Depending on the value of x, either all three of the print statements will execute or none will execute
❌ β€˜Is 5’ will always print
❌ β€˜Is 5’ will never print
❌ Only two will print

🧠 Explanation:
All three print statements are indented at the same level inside the if block, so they all run only if x == 5.


Question 4

When you have multiple lines in an if block, how do you indicate the end of the block?
βœ… You de-indent the next line to the same level as the original if statement
❌ Use { }
❌ Use a colon on a separate line
❌ Omit a semicolon

🧠 Explanation:
Python uses indentation to determine block structure. The if block ends when the indentation returns to the previous level.


Question 5

Python gives an ‘Indentation Error’ on the second print statement. Why?
βœ… You have mixed tabs and spaces in the file
❌ Python randomly emits errors πŸ˜„
❌ Python thinks β€œStill” is misspelled
❌ Program is too large

🧠 Explanation:
Indentation must be consistent (either all spaces or all tabs). Mixing them causes IndentationError.


Question 6

What Python reserved word indicates the false block in a two-way if test?
βœ… else
❌ except
❌ iterate
❌ break

🧠 Explanation:
else: defines the block that runs when the if condition is false.


Question 7

What will this code print?

x = 0
if x < 2:
print('Small')
elif x < 10:
print('Medium')
else:
print('LARGE')
print('All done')

βœ…

Small
All done

🧠 Explanation:
x = 0 satisfies x < 2, so only the first condition is true.
Then it prints "All done" after the if block.


Question 8

For this code:

if x < 2:
print('Below 2')
elif x >= 2:
print('Two or more')
else:
print('Something else')

βœ… This code will never print β€˜Something else’ regardless of the value for x
❌ x = -22
❌ x = -2.0
❌ x = 2.0

🧠 Explanation:
Every number is either < 2 or >= 2.
The else block will never execute.


Question 9

Which will be the last line to execute successfully?

(1) astr = 'Hello Bob'
(2) istr = int(astr)
(3) print('First', istr)
(4) astr = '123'
(5) istr = int(astr)
(6) print('Second', istr)

βœ… 1
❌ 2
❌ 3
❌ 6

🧠 Explanation:
Line (2) will fail because 'Hello Bob' cannot be converted to int. So only line (1) executes successfully.


Question 10

What will the value of istr be after this code executes?

astr = 'Hello Bob'
istr = 0
try:
istr = int(astr)
except:
istr = -1

βœ… -1
❌ NaN
❌ Random number
❌ false

🧠 Explanation:
The try block fails (int('Hello Bob') is invalid), so Python runs the except block, setting istr = -1.


🧾 Summary Table

Q# βœ… Correct Answer Key Concept
1 Indent the line below if Indentation defines blocks
2 = Assignment vs comparison
3 All or none print If-block indentation
4 De-indent next line Block structure
5 Mixed tabs/spaces Indentation error
6 else Two-way if test
7 Small / All done Conditional flow
8 Never prints β€œSomething else” Logical completeness
9 1 Conversion error
10 -1 try/except handling