Python Tutorial
Python Introduction
Learn what Python is, why it's popular, and what you can do with it.
What is Python?
Python is a popular programming language created by Guido van Rossum and released in 1991. It is used for:
- Web development (server-side)
- Software development
- Mathematics and data analysis
- System scripting
- Machine learning and artificial intelligence
What can Python do?
- Python can be used on a server to create web applications
- Python can be used alongside software to create workflows
- Python can connect to database systems and read and modify files
- Python can be used to handle big data and perform complex mathematics
- Python can be used for rapid prototyping or production-ready software development
Why Python?
- Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc)
- Python has a simple syntax similar to the English language
- Python has syntax that allows developers to write programs with fewer lines than some other programming languages
- Python runs on an interpreter system, meaning that code can be executed as soon as it is written
- Python can be treated in a procedural way, an object-oriented way or a functional way
Python Syntax compared to other programming languages
- Python was designed for readability, and has some similarities to the English language with influence from mathematics
- Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses
- Python relies on indentation, using whitespace, to define scope; such as the scope of loops, functions and classes
Example
print("Hello, World!")How Python Runs Your Code
Python is an interpreted, dynamically typed language. You write plain-text .py files, and the Python interpreter reads them top to bottom, compiles them to intermediate bytecode, and executes that on the Python Virtual Machine (PVM). There is no separate compile step to manage — you run the file and see results immediately.
- Interpreted: run code as soon as you write it, great for learning and prototyping.
- Dynamically typed: you do not declare variable types; Python infers them at runtime.
- High-level: memory management and many low-level details are handled for you.
Python 2 vs Python 3
Always use Python 3. Python 2 reached end-of-life in 2020 and no longer receives security updates. The classic difference beginners hit:
# Python 3 (correct today)
print("Hello, World!") # print is a function
# Python 2 (obsolete)
# print "Hello, World!" # print was a statementCheck your version any time with python --version.
Python vs Other Languages
| Feature | Python | Java / C++ |
|---|---|---|
| Line ending | Newline | Semicolon ; |
| Blocks / scope | Indentation | Curly braces |
| Typing | Dynamic | Static (declared) |
| Compile step | Run directly | Compile then run |
| "Hello World" lines | 1 | 3–5 |
Try It Yourself
Exercise 1: Write a program that prints your name and your favourite programming reason on two separate lines.
Show solution
print("Ada Lovelace")
print("I like that Python reads like English.")Exercise 2: In one print call, output Python is fun! exactly, including the exclamation mark.
Show solution
print("Python is fun!")Key Takeaways
- Python is high-level, interpreted, and dynamically typed.
- It is prized for readable, English-like syntax and fast development.
- Always use Python 3;
print()is a function. - Indentation — not braces — defines code blocks.
📘 Real-World Deep Dive
Python's "batteries included" philosophy makes it the default language for scripting, data engineering, ML, automation, and web back-ends. Knowing its shape — indentation, dynamic typing, rich stdlib — pays off on every codebase you'll ever touch.
Real-Life Scenario
A small data-engineering script that reads a CSV, aggregates totals per user, and writes a JSON report — the classic first-week-on-the-job task.
Real-Life Example
import csv, json, sys
from collections import defaultdict
from pathlib import Path
def build_user_totals(csv_path: Path) -> dict[str, float]:
totals: dict[str, float] = defaultdict(float)
with csv_path.open(newline="") as f:
reader = csv.DictReader(f)
for row in reader:
user = row["user"]
amount = float(row["amount"])
totals[user] += amount
return dict(sorted(totals.items(), key=lambda kv: -kv[1]))
def main(in_path: str, out_path: str) -> int:
totals = build_user_totals(Path(in_path))
Path(out_path).write_text(json.dumps(totals, indent=2))
print(f"wrote {len(totals)} users to {out_path}", file=sys.stderr)
return 0
if __name__ == "__main__":
sys.exit(main(*sys.argv[1:]))Expected Output
{"alice": 240.5, "bob": 110.0, "carol": 75.25}Common mistakes
- Forgetting the
if __name__ == "__main__":guard makes the script run side-effects on import, which silently breaks tests. - Mixing tabs and spaces in indentation raises an
IndentationErrorthe very first time the file is opened in a different editor. - Reading the whole CSV with
list(...)loads gigabytes into RAM — usecsv.DictReaderand stream rows.
🚀 Performance & Best Practices
- Use
pathlib.Pathinstead ofos.path.join— chained methods compose and read better. - Prefer
defaultdictorCounterover manualdict.setdefaultfor aggregation. - For JSON > 100 MB consider
ijsonororjsoninstead of the stdlib encoder.
🧪 Try It Yourself
- Rewrite
build_user_totalsto also return the per-user transaction count alongside the total. - Add a
--since YYYY-MM-DDflag that filters rows by date before aggregating. - Convert the script into a
click-style CLI with a help message.