Exercises — Chapter 2: Setting Up Your Python Environment
Starred exercises (
*) have worked solutions in Appendix B.
Tier 1: Recall
1.1 What command do you use to check which Python version is installed on your system?
1.2 List the four components of a Python development setup covered in this chapter.
1.3 What is the purpose of a virtual environment? Give one specific problem it prevents.
1.4 Write the command to create a virtual environment named venv in the current directory.
1.5 What does the (venv) prefix in your terminal prompt indicate?
1.6 Which command saves a list of your installed packages to a file?
1.7 Name two situations where you would use the Python REPL rather than a script file.
1.8 What command installs a package named requests into your active virtual environment?
Tier 2: Apply
2.1 ★ Complete the following setup checklist. For each step, write the exact command you would use (or describe the action if it doesn't require a command):
- [ ] Check Python version
- [ ] Create a folder called
exercise-ch02 - [ ] Navigate into that folder
- [ ] Create a virtual environment
- [ ] Activate the virtual environment (for your OS)
- [ ] Install
pandasandrequests - [ ] Verify they installed correctly
- [ ] Save the requirements to a file
- [ ] Create a Python file called
test.py - [ ] Run the Python file
2.2 ★ Create the following script and run it:
# business_info.py
# Fill in values relevant to your own work context
organization = "YOUR ORGANIZATION"
your_role = "YOUR ROLE"
primary_task = "YOUR PRIMARY RECURRING TASK"
estimated_hours_per_week = 0 # Replace with your estimate
annual_hours = estimated_hours_per_week * 52
print(f"Organization: {organization}")
print(f"Role: {your_role}")
print(f"Primary recurring task: {primary_task}")
print(f"Time spent annually: {annual_hours} hours")
print(f"At $50/hour internal cost: ${annual_hours * 50:,.0f}")
Replace the placeholder values with information relevant to your own work. Run the script and include the output in your answer.
2.3 Without looking at the book, write the commands to:
1. Create a virtual environment
2. Activate it on your operating system
3. Install the seaborn library
4. Verify the installation
5. Deactivate the environment
2.4 You share your Python project with a colleague who doesn't have Python set up yet. List the steps they would need to take to get your project running on their machine, given only the code files and a requirements.txt.
2.5 Look at the output of pip freeze in your current environment (run pip freeze in your terminal). How many packages are listed? How many did you explicitly install vs. how many are dependencies?
Tier 3: Analyze
3.1 ★ Explain the difference between:
- python vs python3
- pip vs pip3
- Why do these differences exist, and on which operating system is each convention used?
3.2 A colleague says: "I don't bother with virtual environments. I just install everything into my main Python installation. It's simpler." Explain the specific problems this approach will eventually cause. Use a concrete example.
3.3 Compare the Python REPL, a .py script file, and a Jupyter Notebook across these dimensions:
- Persistence (does it save your work?)
- Interactivity (can you see results immediately?)
- Best use case
- Collaboration (can you share it with others?)
3.4 Someone asks: "What's the difference between a module and a library in Python?" These terms are sometimes used interchangeably. Explain the precise technical distinction and how the terms are used in practice.
3.5 The chapter recommends VS Code over PyCharm for beginners. What factors would lead an intermediate or advanced Python developer to choose PyCharm instead? Under what conditions would the investment in a full IDE pay off?
Tier 4: Synthesize
4.1 Design a Python project folder structure for a real-world business use case of your choosing. Your structure should include:
- A clear folder hierarchy
- A venv/ for the virtual environment
- Separate folders for code, data, and output
- A requirements.txt
- At least 3 Python script files with descriptive names that indicate their purpose
Explain the rationale for each element of your structure.
4.2 Your organization wants to standardize how the analytics team manages Python environments. Write a one-page "Python Environment Standards" document that covers:
- Which Python version to use
- How to name and create virtual environments
- What to include in requirements.txt
- Folder structure conventions
- How to hand off a project to another team member
4.3 You discover that a colleague has been running all their Python work from a single global environment (no virtual environments) and that environment now has conflicting package versions causing errors. Describe the steps you would take to: 1. Diagnose which packages are conflicting 2. Create a clean environment for their most important project 3. Migrate their existing work to the new environment 4. Prevent this from happening again
Tier 5: Challenge
5.1 (Open-Ended) The chapter discusses Anaconda as an alternative to the pip/venv workflow. Research the current state of the Python packaging ecosystem — including newer tools like poetry, uv, and pipenv. Write a comparison of 3–4 environment management approaches, evaluating each for: simplicity, speed, reliability, and business context appropriateness. Which would you recommend for a team of 5 business analysts who are new to Python?
5.2 (Open-Ended) Python version management becomes complex in organizations where different teams use different versions. Research tools like pyenv (macOS/Linux) and py launcher (Windows) and explain how they solve the multi-version problem. Then design a policy for an organization where: Team A needs Python 3.9 (for legacy compatibility), Team B needs Python 3.12, and all team members share the same Windows laptops.