These exercises practice what the Module 10 reading explained. They aren’t graded.
Do them in a real terminal, in your py101 folder, as in Module 9. Write your answer or your
prediction first, then run the commands to check it. The answers are in the collapsed blocks;
open one only after you have an answer of your own.
As in Module 9, python stands for your environment’s Python. From a project folder inside
py101, type ..\.venv\Scripts\python on Windows and ../.venv/bin/python on macOS and Linux in
its place. Exercise 10.6 needs your GitHub account and an internet connection; the others don’t.
Git’s hashes and times in your output will differ from the ones shown here.
Worked example: from an edit to a commit¶
This example puts the countdown tool from Exercise
9.5
under Git, then changes its default last word from Go to Launch. (If you skipped that
exercise, save its answer as countdown.py in m09 first.)
First, make the project’s folder and repository, copy the file in, and commit it:
mkdir countdown
cd countdown
git init
cp ../m09/countdown.py .
git add countdown.py
git commit -m "Add a countdown with --step and --word"The commit’s summary ends with 1 file changed, 30 insertions(+) and
create mode 100644 countdown.py.
Next, in VS Code, change the --word line so that both Gos are Launch, and save. Check what
Git sees before you stage anything:
git status
git diffgit status lists modified: countdown.py under “Changes not staged for commit”, and the diff
shows the one changed line, as a - line and a + line:
- parser.add_argument("--word", default="Go", help="the word to end with (default Go)")
+ parser.add_argument("--word", default="Launch", help="the word to end with (default Launch)")If the diff shows anything you didn’t mean to change, fix it now, before it’s committed.
Last, stage and commit the change, and check the history:
git add countdown.py
git commit -m 'Make "Launch" the default last word'
git log --onelineba112e8 Make "Launch" the default last word
e622f34 Add a countdown with --step and --wordThe message has double quotes in it, so single quotes go around it. That works in bash, zsh, and PowerShell alike.
Exercise 10.1 (LO-10.1, ★, about 10 minutes)¶
Do the same for the bill splitter from Exercise 9.3:
Make a repository in a new folder,
py101/bill-splitter, and commitbillmath.pyandsplit.pyfromm09together, in one commit.Add a third test to the main guard of
billmath.py, after the other two:assert share_per_person(10, 0, 4) == 2.5. Runpython billmath.pyto check that the tests still pass.Check the change with
git diff, then commit it with a message of your own.
Answer
mkdir bill-splitter
cd bill-splitter
git init
cp ../m09/billmath.py ../m09/split.py .
git add billmath.py split.py
git commit -m "Add a bill splitter: a module and a script"The commit reports 2 files changed, 32 insertions(+). After the edit, python billmath.py prints billmath tests passed, and git diff shows one line added, starting with +. Then:
git add billmath.py
git commit -m "Test a share that isn't a whole number"
git log --onelinegit log --oneline lists two commits. Any message that says what the test checks is fine; Add a test says too little, because every test is a test.
Exercise 10.2 (LO-10.1, ★★, about 10 minutes)¶
Someone changed billmath.py, and git diff printed this:
@@ -7,8 +7,8 @@ def tip_amount(bill, percent):
def share_per_person(bill, tip, people):
- """Return what each of people pays for bill plus tip."""
- return (bill + tip) / people
+ """Return what each of people pays for bill plus tip, rounded to the cent."""
+ return round((bill + tip) / people, 2)
if __name__ == "__main__":Which function changed, and how many lines did the change remove and add?
What does
share_per_person(10, 0, 3)return before the change, and after it?At which line of the new file does the shown part start?
The
@@line namestip_amount. Didtip_amountchange?
Answer
share_per_person: two lines removed and two added, its docstring and itsreturnline.Before,
3.3333333333333335; after,3.33, becauseround(x, 2)rounds to two decimal places.Line 7:
+7,8means the part starts at line 7 of the new file and shows 8 lines.No. Git adds the name of the nearest function or class above the change, to help you find your place. Only the lines that start with
-or+changed, and none of them is intip_amount.
Exercise 10.3 (LO-10.2, ★★, about 10 minutes)¶
Each message below is for a real change, described in brackets. Say what is wrong with each, and rewrite it.
fixed bug[the countdown printed0before the last word when--stepdivided the start exactly]Changes to countdown.py[--stepnow rejects values below 1]Adding the README and also the .gitignore and fixing the help text for --word[three changes, in one commit]Changed default="Go" to default="Launch" on line 19[the default last word is now Launch]Make the countdown better.[the tool now waits one second between numbers, so it feels like a real countdown]
Answer
Vague, and not an instruction.
Stop printing 0 before the last word.Says where, not what, and isn’t an instruction.
Reject --step values below 1.Three changes belong in three commits, each with its own message:
Add a README that explains how to run the countdown,Ignore Python's cache and the environment, andFix the help text for --word.Repeats the diff and isn’t an instruction.
Make "Launch" the default last word.Vague, with a period at the end.
Wait a second between numbers, with a body that says why: “Printing every number at once didn’t feel like a countdown.”
Other wordings are right too, if they are instructions that say specifically what changed.
Exercise 10.4 (LO-10.2, ★★, about 10 minutes)¶
A project folder holds these files:
weather/
├── weather.py
├── test_weather.py
├── requirements.txt
├── run.log
├── secrets.txt
├── .venv/
├── __pycache__/
│ └── weather.cpython-312.pyc
└── data/
├── stations.csv
└── raw/
└── 2026-11.csvsecrets.txt holds a password for a weather service. data/raw/ holds downloaded files that are
200 MB in all and can be downloaded again; data/stations.csv is a small file someone wrote by
hand.
Write the project’s
.gitignore.run.logwas committed last week, before there was a.gitignore. After you add yours, the program writes another line torun.log. What doesgit statussay aboutrun.log, and how do you make Git stop tracking it?
Answer
One pattern for each kind of file that doesn’t belong:
__pycache__/ .venv/ data/raw/ *.log secrets.txtdata/raw/ignores only that folder, sodata/stations.csvis still committed.*.logcatches every log, not only this one. To check, make the files in a scratch repository and rungit check-ignore -von each path; it names the line that ignores each one and prints nothing fordata/stations.csv.git statusstill listsrun.logas modified: a.gitignoreonly applies to untracked files.git rm --cached run.logstops tracking it and leaves the file on your disk; it printsrm 'run.log', andgit statusthen listsdeleted: run.logunder “Changes to be committed”. Commit that, and the log stays out of the history from then on. (The old versions stay in earlier commits.)
Exercise 10.5 (LO-10.3, ★, about 5 minutes)¶
A classmate’s repository has this README:
# project1
my python project for the course. run it with python.List three things a stranger can’t learn from it. Then write a README for your countdown
repository, preview it in VS Code (Markdown: Open Preview to the Side), and commit it.
Answer
A stranger can’t learn what the program does, which file to run and with what command, which version of Python it needs, what its options are, or what it prints. A README for countdown:
# Countdown
A command-line tool that counts down from a number and ends with a word, such as a launch
countdown.
## How to run
You need Python 3. The tool uses only the standard library. In a terminal, in this folder:
```bash
python countdown.py 3
```
```text
3
2
1
Launch!
```
Options:
- `--step N`: count down by N instead of 1
- `--word WORD`: the word to end with (default Launch)Commit it with git add README.md and a message such as Add a README that explains how to run the countdown.
Exercise 10.6 (LO-10.3, ★★, about 20 minutes)¶
Publish countdown on GitHub, following “Publishing on GitHub” in the reading: create an empty
public repository named countdown, connect it as origin, and push. Then check that a stranger
could use it: clone it into a new folder, py101/countdown-check, and run it from there.
Answer
After creating the repository on GitHub, with no README, .gitignore, or license:
git remote add origin https://github.com/USERNAME/countdown.git
git push -u origin mainwith your username. The first push asks you to sign in, unless you already did; it ends with * [new branch] main -> main and branch 'main' set up to track 'origin/main'. Reload the repository’s page: it lists countdown.py and README.md, shows the README below them, and its commit count matches git log --oneline. Then, from countdown:
cd ..
git clone https://github.com/USERNAME/countdown.git countdown-check
cd countdown-check
python countdown.py 3The last argument of git clone names the new folder. The program prints 3, 2, 1, and Launch!, as in your README. If the push fails with an authentication error, check the sign-in steps for your operating system. If it says the repository wasn’t found, compare the address in git remote -v with the one GitHub shows.
Exercise 10.7 (LO-10.4, ★★, about 10 minutes)¶
For each mistake, name the command that undoes it, then try the first two in your countdown
repository.
You changed three lines of
countdown.pyan hour ago, haven’t staged or committed them, and want them gone.You deleted
countdown.pyin VS Code’s Explorer by mistake.You ran
git add notes.txtby mistake, and don’t want your notes in the next commit.A commit you pushed yesterday broke
--step, and a classmate has already cloned your repository.
Answer
git restore countdown.py. Be sure first: the three lines are gone for good.git restore countdown.pytoo. An uncommitted deletion is a change like any other, sorestorebrings back the committed version. (git statuslists it asdeleted: countdown.pybefore you restore it.)git restore --staged notes.txt, which leaves the file as it is. Addnotes.txtto.gitignoreif it should never be committed.git revert <hash>, with the hash fromgit log --oneline, thengit push. Revert adds a commit instead of removing one, so your classmate’s copy still agrees with yours.
Exercise 10.8 (LO-10.4, ★★★, about 15 minutes)¶
Make a merge conflict on purpose in countdown, and resolve it:
Create a branch named
start-word. On it, change the default last word fromLaunchtoStart(both places in the--wordline), and commit.Switch back to
main. Change the same line toLiftoffinstead, and commit.Merge
start-wordintomain. Resolve the conflict by keepingStart, check that the program works, and finish the merge.Look at the history with
git log --oneline --graph, and delete the branch.
Hint
The commands are git switch -c, git commit -am "…" (-a stages every tracked file you changed, which saves a git add), git switch, git merge, git add, git commit, and git branch -d.
Answer
git switch -c start-word
git commit -am 'Make "Start" the default last word'
git switch main
git commit -am 'Make "Liftoff" the default last word'
git merge start-wordmaking each edit in VS Code before the commit after it. The merge stops with CONFLICT (content): Merge conflict in countdown.py, and countdown.py holds both lines:
<<<<<<< HEAD
parser.add_argument("--word", default="Liftoff", help="the word to end with (default Liftoff)")
=======
parser.add_argument("--word", default="Start", help="the word to end with (default Start)")
>>>>>>> start-wordDelete the marker lines and the Liftoff line, or select Accept Incoming Change in VS Code, and save. python countdown.py 2 prints 2, 1, and Start!. Then:
git add countdown.py
git commit
git log --oneline --graph
git branch -d start-wordClose the message in VS Code to finish the commit. The graph shows the two commits side by side, joined by Merge branch 'start-word':
* d3f6215 Merge branch 'start-word'
|\
| * 5939365 Make "Start" the default last word
* | 9d90236 Make "Liftoff" the default last word
|/
* ba112e8 Make "Launch" the default last wordIf you published countdown in Exercise 10.6, git push sends the merge to GitHub too.