Week 1 · Python and your developer toolkit
Week 1 checkpoint
A short self-check to confirm you're ready for week 2. Answer each question before opening the solution.
Questions
1. Finding your way
You open a terminal and run python3 orders.py, but get No such file or directory. The file definitely exists. What two commands would you run to find out what's wrong?
SolutionShow solution
pwd to see which folder you're in, and ls to see what's in it. The terminal is probably in a different folder from the file. Use cd to move to the right folder, or give the full path, such as python3 ~/projects/fde-course/week-01/orders.py.
2. Types
What does this print, and why?
quantity = "3"
print(quantity * 2)
SolutionShow solution
It prints 33, not 6. quantity is a string, because of the quotes, and multiplying a string repeats it. To do arithmetic, it must be a number: int(quantity) * 2 prints 6. Data from files, forms and APIs often arrives as text, so this mistake is common.
3. Lists or dictionaries
You need to store one customer's name, email and city. Would you use a list or a dictionary, and why?
SolutionShow solution
A dictionary: {"name": "Noor Foods", "email": "...", "city": "Lahore"}. Each value has a meaning, and a key makes that meaning clear. A list is for many values of the same kind in order, such as a list of customers, where each one is a dictionary.
4. Reading a loop
What does this print?
totals = [4500, 12800, 2300]
count = 0
for total in totals:
if total > 3000:
count += 1
print(count)
SolutionShow solution
2. The loop checks each value, and only 4,500 and 12,800 are over 3,000, so count goes up twice.
5. Indentation
This code prints "Large order" for every order, not just large ones. What's wrong?
for order in orders:
if order["total"] > 10000:
print(order["id"])
print("Large order")
SolutionShow solution
print("Large order") is indented to the same level as the if, so it belongs to the loop, not the if. It runs once for every order. It needs to be indented one more level, under the if.
6. Commit or push
You've run git commit, but your changes don't appear on GitHub. Why?
SolutionShow solution
git commit saves a snapshot only on your computer. git push sends your commits to GitHub. The full loop is edit, git add, git commit, git push.
7. What not to commit
Name two kinds of files that should never be committed, and how you stop Git from tracking them.
SolutionShow solution
Secrets such as .env files with API keys, and generated folders such as .venv/ or __pycache__/. List them in .gitignore so Git ignores them.
Ready for week 2?
- I can move between folders and run a Python file from the terminal without notes.
- I can explain the difference between a string, an integer, a list and a dictionary.
- I can write a loop that adds up values from a list of dictionaries.
- My assignment is pushed to GitHub and the numbers match the expected output.
Finished this lesson?