Solutions 01: Introduction to Python¶
Overview¶
Chapter 01 has no coding exercises — it is a reflection and exploration chapter. The exercises ask you to think about Python, research its uses, and set your own learning goals. This solution guide provides model answers and discussion points for each exercise.
Notes Before Checking Solutions¶
There are no "wrong" answers here. The goal is to think carefully about the concepts before you start writing code. If your answers differ from the ones below, that is fine — what matters is that you engaged with the questions.
Warm-up: Reflection Questions¶
1. What is Python?¶
Python is a programming language — a set of rules for writing instructions that a computer can follow. Unlike machine code (which is just numbers), Python lets you write instructions in something close to plain English. A program called the Python interpreter reads your code and carries out the instructions.
A good beginner-friendly explanation: "Python is a way to give a computer step-by-step instructions using words and symbols that are easier to read than most other programming languages."
2. Why is Python called an interpreted language?¶
Most languages (like C or C++) are compiled: a separate program translates your entire source code into machine code before you run it. Python is interpreted: the Python interpreter reads and executes your code line by line at runtime. There is no separate compile step.
What this means in practice: - You can run a Python file immediately after writing it. - Errors appear at runtime, not at a separate compile step. - The REPL (interactive prompt) works because Python can execute one line at a time.
3. Three real-world uses of Python¶
Any three from this list are correct: - Web development — Django and Flask power many websites and APIs. - Data science and machine learning — pandas, NumPy, scikit-learn, and PyTorch are all Python libraries. - Automation and scripting — system administrators use Python to automate repetitive tasks. - Scientific computing — researchers in physics, biology, and chemistry use Python for simulations and analysis. - DevOps and infrastructure — tools like Ansible and many AWS/GCP SDKs are Python-based. - Education — Python is the most commonly taught first programming language worldwide.
4. What does "readability counts" mean?¶
It is one of the principles from the Zen of Python (import this). It means that code is read far more often than it is written — by you, by teammates, by future maintainers. Python's design deliberately favors code that is easy to read over code that is clever or compact.
In practice: use clear variable names, write short functions, add comments where the intent is not obvious, and follow the community's style conventions (PEP 8).
5. Python 2 vs Python 3¶
Python 2 was the dominant version from 2000 to roughly 2015. Python 3 was released in 2008 and introduced breaking changes (like making print a function and changing how strings handle Unicode). Because the two versions were incompatible, the transition took many years.
Python 2 reached end-of-life on January 1, 2020. It no longer receives security updates. All new code should use Python 3. The handbook uses Python 3.10+.
Practice Exercise Solutions¶
Exercise 1: The Zen of Python¶
Running import this in the REPL prints 19 principles. A few worth discussing:
"Beautiful is better than ugly." Code that is well-structured and readable is preferable to code that works but is hard to follow. This is a value judgment, not a technical rule — it encourages you to care about craft.
"Explicit is better than implicit."
If something is happening in your code, make it visible. Do not rely on hidden behavior or magic. For example, explicitly converting a string to an integer with int(x) is better than relying on automatic coercion.
"There should be one obvious way to do it." Python tries to have a single idiomatic way to accomplish common tasks. This makes Python code more consistent and easier to read across different codebases.
A seeming contradiction: "Simple is better than complex" vs. "Complex is better than complicated." These are not opposites — they describe a spectrum. Prefer simple solutions. When a problem genuinely requires complexity, embrace it. But never add unnecessary complexity (that is "complicated").
Exercise 2: A Real-World Python Project¶
Example: Instagram
Instagram's backend is built largely in Python using Django. Python was a good choice because: - Django provides a mature, batteries-included web framework. - Python's ecosystem has strong support for image processing (Pillow) and data pipelines. - The team could iterate quickly in the early stages.
As Instagram scaled, they kept Python but optimized heavily — showing that Python can handle large-scale production systems with the right architecture.
Exercise 3: Python's Limitations¶
Mobile app development Python is not suitable because mobile platforms (iOS, Android) do not have native Python runtimes. Apps need to be compiled to native code or run in a managed runtime. Swift and Kotlin are the standard choices. There are tools like Kivy and BeeWare that let you write Python for mobile, but they are not mainstream.
Game development Python is too slow for real-time 3D games that need to process thousands of objects per frame. C++ (with Unreal Engine) and C# (with Unity) are the standard choices. Python is sometimes used for game scripting or prototyping, but not for the performance-critical engine layer.
Ultra-low-latency systems Python's garbage collector and interpreter overhead make it unsuitable for systems where every microsecond matters — high-frequency trading, real-time audio processing, or embedded firmware. C and C++ are used here because they give direct control over memory and execution.
Exercise 4: Your Learning Goals¶
This is personal — there is no model answer. The important thing is to write something specific. "I want to learn Python" is too vague. "I want to build a command-line tool that renames files in bulk" is specific and gives you a concrete target to work toward.
Challenge Exercise Discussion¶
Challenge 1: Comparing Python to Another Language¶
Python vs JavaScript (example comparison):
Easier in Python:
- Readable syntax with enforced indentation
- No semicolons or curly braces
- Cleaner handling of numbers (no NaN surprises)
- Better standard library for data processing
Harder in Python: - Asynchronous programming has a steeper learning curve - No native browser support (JavaScript runs in browsers, Python does not) - Slower execution for CPU-intensive tasks
Choose Python when: building data pipelines, automation scripts, or backend APIs where developer productivity matters more than raw speed.
Choose JavaScript when: building anything that runs in a browser, or when you want one language for both frontend and backend.
Challenge 2: Python's History¶
The Python 2 to Python 3 transition is a good topic. The core issue was that Python 3 broke backward compatibility intentionally — the designers decided it was better to fix long-standing problems (especially Unicode handling) even if it meant existing code would not run without changes. The transition took over a decade because the ecosystem (libraries, tools, tutorials) had to catch up. The lesson: breaking changes in widely-used software have enormous downstream costs, even when the changes are technically correct.
Challenge 3: Evaluating Python for Projects¶
| Project | Python? | Reason |
|---|---|---|
| Mobile app (iOS/Android) | No | No native runtime; use Swift/Kotlin |
| Web server at 1M req/s | Maybe | Possible with async frameworks, but Go or Java may be better |
| CSV data analysis tool | Yes | Excellent fit; pandas or stdlib csv module |
| Real-time multiplayer game | No | Too slow for game engine; use C++/C# |
| CLI tool for sysadmins | Yes | Great fit; rich stdlib, easy to distribute |
| ML image classifier | Yes | PyTorch/TensorFlow are Python-first |
Common Mistakes¶
Thinking Python is only for beginners. Python is used in production at Google, Netflix, Instagram, and NASA. It is beginner-friendly, but it is also a serious professional tool.
Confusing Python the language with Python the interpreter. CPython is the reference implementation, but there are others (PyPy, Jython, MicroPython). When people say "Python," they usually mean CPython.
Worrying about Python being "slow." For most programs beginners write, Python is fast enough. Speed only matters when you are processing millions of records or doing real-time computation. Start with Python; optimize later if needed.
What to Review Next¶
- Review the matching handbook chapter if any exercise felt difficult.
- Revisit the matching exercise set and try solving it again without looking at the solution.
- Continue with the next handbook chapter: Chapter 02 - Installation and Setup