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?
β
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?
β
π§ 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:
β
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
β 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?
β
-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 |