Chapter 2 Exercises

These exercises progress from concept checks and output prediction to your first small programs, then to open-ended exploration of your tools.

Difficulty Guide: - ⭐ Foundational (5-10 min each) - ⭐⭐ Intermediate (10-20 min each) - ⭐⭐⭐ Challenging (20-40 min each) - ⭐⭐⭐⭐ Advanced/Research (40+ min each)


Part A: Concept Check (Short Answer) ⭐

A1. What does REPL stand for, and what does each letter mean in practice?

A2. Explain the difference between an interpreted language and a compiled language. Which is Python?

A3. What is the difference between the terminal and the shell?

A4. Why does Python care about indentation, while many other languages do not?

A5. What three pieces of information does every Python error message give you?


Part B: Predict the Output ⭐

For each code snippet, predict what Python will print before running it. Then run it to check.

B1.

print("CS", "is", "fun")

B2.

print("CS", "is", "fun", sep="-")

B3.

print("one", end=" ")
print("two", end=" ")
print("three")

B4.

print("*" * 20)
print("*" * 0)
print("ha" * 4)

B5.

name = input("Name: ")
print("Hello", name)
print(f"Hello {name}")

(Assume the user types Ada.)

B6.

print("Line 1")
print()
print("Line 3")

Part C: Fix the Bug ⭐-⭐⭐

Each snippet contains an error. Identify the error type, explain why it happens, and fix it.

C1.

print("Welcome to Python!)

C2.

Print("Hello, World!")

C3.

message = "Hello"
print(mesage)

C4.

print("line one")
    print("line two")

C5. (This error type is new; run the code, read the message, and see how much of it you can work out. Chapter 3 explains it fully.)

print("Your age is: " + 25)

Part D: Code Writing ⭐⭐

D1. Personal Info Card Write a program that asks the user for their name, age, and favorite food, then prints a formatted "info card" like this:

===== INFO CARD =====
Name: Ada
Age: 20
Favorite food: pizza
=====================

D2. Mad Libs Write a program that asks the user for a noun, a verb, an adjective, and an adverb, then prints a silly sentence using all four. Example output:

Enter a noun: elephant
Enter a verb: danced
Enter an adjective: purple
Enter an adverb: gracefully

The purple elephant gracefully danced across the stage!

D3. Receipt Generator Write a program that prints a formatted receipt. Hard-code the values (don't use input()). Include at least three items with prices, a subtotal line, and a total. Use string repetition for the divider lines.

D4. Custom Banner Write a program that asks the user for a message and then prints that message centered inside a box made of # characters. The box should be 40 characters wide. Example:

Enter your message: Hello World

########################################
#             Hello World              #
########################################

Hint: The space between the two # borders is 38 characters wide. Look up the .center() string method (message.center(38) pads a string with spaces to 38 characters), or calculate the spacing manually. If the padding can't be split evenly, .center() decides which side gets the extra space; the example above shows its choice.

D5. Interview Bot Write a program that conducts a five-question "interview." Ask the user five different questions (your choice), store each answer in a variable, and then print a summary paragraph that weaves all five answers together. The summary should read like a natural paragraph, not a list.


Part M: Multiple Choice Practice ⭐

M1. What does python --version do? - (a) Installs Python - (b) Starts the REPL - (c) Displays the installed Python version number - (d) Updates Python to the latest version

Answer(c) Displays the installed Python version number.

M2. Which of the following is NOT a valid way to run a Python script? - (a) python myscript.py in the terminal - (b) Clicking the play button in VS Code - (c) Running python3 myscript.py in the macOS Terminal - (d) run myscript.py in the terminal

Answer(d) `run` is not a standard terminal command, so the shell will report that it can't find it. The correct command is `python myscript.py` (or `python3 myscript.py` on macOS, which is why (c) is valid).

M3. What does input() return? - (a) An integer - (b) Whatever type the user types - (c) A string, always - (d) Nothing — it only displays a prompt

Answer(c) A string, always. Even if the user types a number, `input()` returns it as a string.

M4. What will print("ab" * 3) output? - (a) ab 3 - (b) ababab - (c) ab*3 - (d) An error

Answer(b) `ababab` — string repetition repeats the string the specified number of times.

M5. What's wrong with this comment? x = 5 # set x to 5 - (a) Nothing — it's fine - (b) Comments should use // not # - (c) It's not wrong syntactically, but it's a bad comment because it restates the obvious - (d) You can't put a comment on the same line as code

Answer(c) The comment just restates what the code already says. A better comment would explain *why* x is 5 (e.g., `# business days in a work week`).

Part E: Exploration & Extension ⭐⭐-⭐⭐⭐

E1. REPL Explorer ⭐⭐ Open the REPL and try at least 10 expressions you haven't seen in this chapter. Document what you tried and what happened. Try to find at least one thing that surprises you. Write up your findings as comments in a .py file.

E2. Error Catalog ⭐⭐⭐ Intentionally create at least five different Python errors (not just the three from the chapter). For each, record: (1) the code that caused it, (2) the full error message, and (3) how you fixed it. Save your catalog as a .py file with comments explaining each error.

E3. Terminal Mastery ⭐⭐ Using only the terminal (no graphical file manager), accomplish the following: 1. Create a folder called terminal-practice inside your cs1 folder. 2. Inside it, create three subfolders: project-a, project-b, project-c. 3. Create a file called notes.txt in project-a (hint: echo "some text" > notes.txt works in every shell; touch notes.txt works on macOS/Linux). 4. List the contents of terminal-practice to verify your structure. 5. Navigate back to your home directory in one command.

Document all the commands you used.

E4. Editor Customization ⭐⭐ Explore VS Code's settings. Find and change at least three settings (font size, theme, auto-save, etc.). Then install one additional VS Code extension that looks useful for Python development. Write a brief paragraph about what you found and changed.

E5. Language Comparison ⭐⭐⭐ Look up the "Hello, World!" program in three other programming languages (e.g., Java, C, JavaScript, Rust, Go). Write a comparison: how many lines does each version take? What boilerplate is required? Why is Python's version simpler? Save your comparison as a commented .py file.