💻 Why do we use Python?
1. Interpreted vs. Compiled Languages: Fast Feedback Loop
- Compiled Languages (C/C++/Java)
- Source → (traditional) machine code → execution
- Pros: native performance; optimizations possible
- Cons: long write → compile → run cycle; full rebuild required for every change
- Interpreted Languages (Python, Ruby, etc.)
- Source → bytecode (.pyc) → executed line-by-line in a virtual machine
- Pros: immediate execution after modification; convenient REPL experimentation
- Cons: slower than native; concurrency limitations (GIL)
- Python: A Compromise
- Bytecode compilation + interpretation avoids full parsing on each run
- Combines fast startup of compiled languages with short feedback loop of interpreters
1.1 How to Use the Python Interpreter (Supplement to Tutorial Chapter 2)
- Execution & Invocation Methods (REPL: “Read → Eval → Print → Loop”)
- Interactive Mode: Run
python3.13in the terminal to start the REPL environment immediately; execute code line by line with instant results - Script Mode: Run
python script.pyto execute a file in batch; all input is parsed and executed before exit - Inline Execution: Use
python -c "command"to run short code snippets instantly - Module Execution: Use
python -m module_nameto run a module as__main__, treating the module path like a script
2. Developer’s Perspective: Productivity vs. Performance Trade-off
- “Fast development” equals fast experimentation
- When prototyping ML models, a few lines of script can run data preprocessing → training → evaluation in one go
- Impossible to compare with writing, building, and debugging preprocessing logic in C/C++
- High-level data structures & rich standard/third-party libraries
- Built-in containers (lists, dicts, tuples) handle complex data manipulation in a single line
- Ecosystem (NumPy, Pandas, Scikit-learn, TensorFlow, PyTorch) lets you piece together code snippets for format conversion, numerical computation, and model training
- Disadvantages can be mitigated
- Slow parts can be offloaded to Cython/C extensions or addressed with PyPy JIT
- Need parallelism? Use a process pool or external distributed systems (Ray, Dask)
3. Best for Studying AI
- Instant Gratification
- Inspect DataFrame output and visualize immediately in the REPL
- Launch
ipdborpdbfor step-by-step debugging
- Tutorials, Examples, Community
- Tens of thousands of Jupyter notebooks on official tutorials, Stack Overflow, and GitHub
- Directly reuse “I ran this code and got these results” examples or derive new experiments
- Platform Independence
- Install and run the same way on Windows, macOS, and Linux
- Bytecode (.pyc) and PVM structure minimize hardware dependencies
4. Python Prioritizes Productivity Over Performance
- Fast development feedback loop, high-level data structures, vast libraries, portability, and community culture all come together
- Essential for quickly building everything from ML/DL model experiments to deployment scripts, data visualizations, and automated reports