Introduction to Python
Python is a general-purpose programming language built around one central idea: code should be easy to read, even months after you wrote it, even by someone who didn't write it.
What makes Python different
A lot of languages wrap every block of code in curly braces { } and end every line with a semicolon. Python drops both. Instead, it uses line breaks to separate statements and indentation to show what belongs inside what. The result looks a lot like the pseudocode you'd sketch on paper — which is exactly the point. Less punctuation to manage means less to get wrong, and code that reads closer to the idea it's expressing.
Where Python shows up
Part of Python's popularity is that it was never built for just one job, and the ecosystem of libraries built around it now covers nearly everything:
- Web backends — frameworks like Django and Flask power large parts of the web.
- Data analysis and science — pandas, NumPy, and Jupyter notebooks are practically the industry standard.
- Automating repetitive tasks — renaming a folder of files, scraping a webpage, generating a weekly report.
- Machine learning and AI — most of the field is built and taught on top of Python.
Installing Python
Download an installer from python.org for Windows or Mac. On Mac and most Linux systems, some version of Python is often already installed — though possibly an older one. Either way, check what you've got from a terminal:
python3 --version
Python 3.12.4
Some systems use python instead of python3 as the command name — if the first one isn't found, that's the one to try.
Your first program
Open a new file, save it as hello.py, and add one line:
print("Hello, world!")
Hello, world!
print() takes whatever is inside its parentheses — its argument — and writes it to the screen, followed by a new line automatically.
Two ways to run your code
The normal way is to run the whole file at once from a terminal:
python3 hello.py
Hello, world!
The other way is the REPL (short for read-eval-print loop) — an interactive prompt for trying out one line at a time, without saving a file. Run python3 with no filename to open it:
$ python3
>>> 2 + 2
4
>>> print("testing")
testing
>>> exit()
The REPL is great for checking how a single line behaves; a saved .py file is what you'll use for anything longer than a couple of lines.
{ } to mark where a block of code starts and stops — it uses the indentation itself. Keep an eye on the whitespace in every example from here on; in Python, it isn't just a style preference, it's part of the syntax. We'll cover this properly in Conditionals.