Python Fundamentals
Python is an interpreted, high-level, dynamically typed language. It emphasizes code readability with its use of significant indentation.1. How Python Works: Under the Hood
Unlike compiled languages (C++, Go, Rust) that produce standalone executables, Python uses a two-stage process: compilation to bytecode, then interpretation.The Python Execution Pipeline
| Stage | What Happens | Can You See It? |
|---|---|---|
| Lexing | Source code → tokens (keywords, identifiers, operators) | Internal |
| Parsing | Tokens → Abstract Syntax Tree (AST) | ast.parse() |
| Compiling | AST → Bytecode instructions | dis.dis() |
| Interpreting | PVM executes bytecode | Your program runs! |
Why is Python “Interpreted”?
Python is actually both compiled and interpreted:- Compilation (happens automatically): Your
.py→ bytecode (.pyc) - Interpretation: The Python Virtual Machine (PVM) executes bytecode
The __pycache__ Folder
When you import a module, Python saves the compiled bytecode:
cpython-311= CPython interpreter, Python 3.11- Bytecode is cached for faster imports (no recompilation if source unchanged)
- Delete safely: Python will regenerate if needed
CPython vs Other Implementations
| Implementation | Description | Use Case |
|---|---|---|
| CPython | Reference implementation (C) | Default, most libraries |
| PyPy | JIT-compiled Python (faster) | Performance-critical |
| Jython | Python on JVM (Java bytecode) | Java integration |
| MicroPython | Tiny Python for microcontrollers | IoT, embedded |
Performance Tip: Python is slower than compiled languages because the PVM interprets bytecode at runtime. For CPU-intensive tasks, consider:
- NumPy/Pandas: Uses C under the hood
- Cython: Compile Python to C
- PyPy: JIT compilation for speedups
2. Variables & Types
Python is dynamically typed. You don’t declare types (likeint x), but types definitely exist.
Type Hints (Python 3.5+)
While Python doesn’t enforce types at runtime, you can (and should) use Type Hints. They act as documentation and allow tools (like VS Code ormypy) to catch errors.
Mutable vs. Immutable
This is a critical concept in Python that trips up beginners.- Immutable (Cannot change):
int,float,str,tuple,bool. - Mutable (Can change):
list,dict,set.
3. Input & Output
f-Strings (Python 3.6+)
The “Formatted String Literal” is the modern way to insert variables into strings. It’s fast and readable.4. Control Flow
Python uses indentation (whitespace) to define blocks of code. There are no curly braces{} or semicolons ;.
If-Elif-Else
Loops
For Loop Python’s for loop is actually a “for-each” loop. It iterates over a sequence.5. Functions
Functions are defined usingdef.
Default Arguments
You can provide default values for parameters.*args and **kwargs
These allow functions to accept an arbitrary number of arguments.*args: Collects positional arguments into a Tuple.**kwargs: Collects keyword arguments into a Dictionary.
Summary
- Dynamic Typing: Variables can change type, but Type Hints help maintain order.
- Indentation: Whitespace is syntactically significant.
- Immutability: Strings and numbers cannot be changed in place; lists can.
- f-Strings: The best way to format text.