Introduction to Python
Python is a high-level, interpreted, general-purpose programming language prized for readable syntax and a massive ecosystem. It's the default language for data science, machine learning, automation, web backends, and scripting — and the foundation for the NumPy, Pandas, and Matplotlib libraries covered later in this tutorial.
Why Python?
| Trait | Why it matters |
|---|---|
| Readable syntax | Indentation-based blocks, no braces or semicolons — code reads close to plain English |
| Dynamically typed | No type declarations; variables can hold any type and can be reassigned |
| Batteries included | Rich standard library — files, JSON, HTTP, dates, regex — with no extra install |
| Huge ecosystem | NumPy, Pandas, Matplotlib, Django, FastAPI, PyTorch, TensorFlow via pip |
| Interpreted | Runs line by line — fast feedback loop, no separate compile step |
| Cross-platform | Same code runs on Windows, macOS, and Linux |
Installing Python
Download the latest release from python.org, or use a package manager. Verify the install with:
python --version
# or on some systems:
python3 --version
pip --version # pip ships with modern Python installers
python command works from any terminal.Running Python code
| Method | Command | Use case |
|---|---|---|
| REPL (interactive shell) | python | Quick experiments, one-off checks |
| Script file | python app.py | Real programs, saved and re-runnable |
| Module flag | python -m http.server | Run an installed package as a program |
| Notebook | Jupyter / VS Code notebooks | Data exploration, mixing code with output & charts |
Your first program
# hello.py
name = "World"
print(f"Hello, {name}!")
# Run it:
# python hello.py
# Output:
# Hello, World!
Comments and style (PEP 8)
# A single-line comment starts with '#'
"""
A triple-quoted string used as a
multi-line comment or docstring.
"""
def greet(name):
"""Return a friendly greeting for `name`.""" # docstring — describes the function
return f"Hello, {name}!"
Python's official style guide is PEP 8: 4-space indentation, snake_case for variables and functions, PascalCase for classes, and constants in UPPER_CASE. Consistent indentation isn't just style in Python — it defines code blocks, so mixing tabs and spaces will cause errors.
Packages and virtual environments
Third-party libraries are installed with pip, Python's package manager. A virtual environment isolates a project's dependencies from your system Python and from other projects.
# Create an isolated environment
python -m venv .venv
# Activate it
.venv\Scripts\activate # Windows
source .venv/bin/activate # macOS / Linux
# Install packages into the active environment
pip install numpy pandas matplotlib
# Freeze exact versions for reproducibility
pip freeze > requirements.txt
pip install -r requirements.txt
The data-science stack
Python
- Core language
- Syntax & control flow
- Standard library
NumPy
- N-dimensional arrays
- Vectorized math
- Foundation library
Pandas
- DataFrames & Series
- Tabular data
- Built on NumPy
Matplotlib
- Charts & plots
- Visualize results
- Works with both
This tutorial follows that exact order: Python fundamentals first, then NumPy for numerical arrays, Pandas for tabular data analysis built on top of NumPy, and finally Matplotlib to visualize the results of both.
