Before you can invent anything, you need a place to write and run code. This chapter gets you set up and running your very first Python program — in under 15 minutes.
There are two ways to run the code in this book. Both work for every chapter. Pick whichever suits you — you can always switch later.
| Path A: Google Colab | Path B: VS Code | |
|---|---|---|
| Install anything? | No — runs in your browser | Yes — Python + VS Code |
| Internet needed? | Yes, always | Only for setup |
| Best for | Quick start, Chromebooks, shared machines | Offline work, larger projects, professional setup |
| Speed | Depends on Google's servers | As fast as your computer |
You can do both — many developers use Colab for quick experiments and VS Code for serious projects. After completing your chosen path, skip ahead to Part 4 for your first real program.
Google Colab gives you a free Python notebook in your browser. No installation, no configuration — just a Google account.
Step 1. Open colab.research.google.com
Step 2. Sign in with your Google account (the same one you use for Gmail or YouTube).
Step 3. Click "New Notebook" (or File → New notebook).
You'll see something like this: a blank page with a grey box that says [ ] on
the left. That grey box is a code cell — it's where you write Python.
The [ ] turns into a number (like [1]) after you run the cell, showing the
order in which cells were executed.
Click inside the code cell and type exactly this:
print("Hello, World!")
Now run it: press Shift+Enter (or click the play button ▶ on the left of the cell).
What appears below the cell?
You should see Hello, World! printed below the cell. If you see an error, check that you typed the line exactly — including the quotes and parentheses.
The print() function tells Python to display whatever is inside the parentheses. The quotes tell Python it's text (a string), not a command.
The text Hello, World! appears below the cell. You just ran your first line of
Python code.
print("Hello, World!")
# Output: Hello, World!
Click "+ Code" at the top to add a new cell. In the new cell, try each of these one at a time (you can put them all in one cell too):
2 + 3
10 * 5
7 / 2
2 ** 10
What does ** do? What does / give you — is it a whole number or a decimal?
** is Python's exponentiation operator. 2 ** 10 means $2^{10}$ = 1024.
/ always gives a decimal (float) in Python, even when the result is whole. 10 / 2 gives 5.0, not 5. If you want integer division, use //.
Python works as a calculator. The operators are: + (add), - (subtract),
* (multiply), / (divide), ** (power).
print(2 + 3) # 5
print(10 * 5) # 50
print(7 / 2) # 3.5
print(2 ** 10) # 1024
A variable stores a value so you can use it later. In a new cell, type:
name = "your name here"
print("Hello, " + name + "!")
Replace "your name here" with your actual name. Run it.
Now change the name to someone else's and run again. The code is the same — only the data changed.
Make sure your name is inside quotes — name = "Alice", not name = Alice. Without quotes, Python thinks Alice is a variable name and will throw an error.
Variables let you store and reuse values. The = sign means "store this value
in this name" — it's not a math equation.
name = "Alice"
print("Hello, " + name + "!")
# Output: Hello, Alice!
You're set up with Google Colab!
Every chapter in this book can be opened directly in Colab — look for the "Open in Colab" button at the top of each chapter page.
Now skip to Part 4 to write your first real program.
This path sets up a full Python environment on your computer. It takes a few more minutes, but you'll have a professional-grade setup that works offline.
Important (Windows): Check the box that says "Add Python to PATH" before clicking Install. If you miss this, Python won't be found from the terminal.
Mac: Python 3 may already be installed. Open Terminal and type python3 --version
to check. If not, the python.org installer works, or use brew install python3
if you have Homebrew.
Open a terminal in VS Code:
Type this and press Enter:
python --version
You should see something like Python 3.12.4 (the exact version doesn't matter as
long as it starts with 3).
Mac users: If python doesn't work, try python3 --version instead.
If you get command not found or 'python' is not recognized, Python isn't on your PATH. On Windows, re-run the Python installer and check 'Add Python to PATH'. On Mac/Linux, try python3 instead of python.
You should see output like Python 3.12.4. Any version 3.8 or higher works
for this book.
Before we open a notebook, let's use Python directly in the terminal. This is the Python interpreter — a place where you type one line at a time and Python responds immediately.
In the same VS Code terminal, type:
python
(Mac/Linux: use python3 if python doesn't work.)
You should see something like:
Python 3.12.4 (...)
Type "help", "copyright", "credits" or "license" for more information.
>>>
The >>> is Python's prompt — it's waiting for you. Now try these one at a time:
>>> 2 + 3
>>> 10 * 5
>>> 2 ** 10
>>> print("Hello from the interpreter!")
>>> name = "Alice"
>>> print("Hello, " + name)
Notice that the interpreter shows results immediately — you don't even need
print() for expressions like 2 + 3.
When you're done, type exit() to leave the interpreter and return to the
normal terminal.
If you see >>>, you're in the Python interpreter. Each line you type runs immediately. This is great for quick experiments.
To exit, type exit() and press Enter. You'll return to the regular terminal prompt.
The interpreter is Python's simplest mode — no files, no notebooks, just type and see. It's useful for quick tests and exploring how things work. Notebooks build on this same idea but let you save your work.
The interpreter is great for quick experiments, but real programs live in files. Let's create one.
hello.py — pick any folder you like
(File → Save As, or Ctrl+S / Cmd+S)name = "Alice"
print("Hello, " + name + "!")
print(name, "can count to", 2 ** 10)
cd if needed) and run:python hello.py
(Mac/Linux: use python3 hello.py if needed.)
You should see the output printed in the terminal. Change the name in the file, save, and run again — notice you have to save before running, unlike the interpreter.
If you get No such file or directory, make sure your terminal is in the same folder as hello.py. Use ls (Mac/Linux) or dir (Windows) to list files and cd foldername to navigate.
The .py extension tells VS Code (and you) that this is a Python file. VS Code will automatically highlight the syntax for you.
You've now used Python in three ways: the interpreter (one line at a time), a script file (saved and run from the terminal), and next you'll try notebooks (the best of both — save your work and see results inline).
# hello.py
name = "Alice"
print("Hello, " + name + "!")
print(name, "can count to", 2 ** 10)
# In terminal:
# $ python hello.py
# Hello, Alice!
# Alice can count to 1024
Python 3.12.4)Now type this in the first cell:
print("Hello from VS Code!")
Press Shift+Enter to run it.
If you don't see the Jupyter option in the Command Palette, make sure you installed the Jupyter extension (Step 3 above). You may need to restart VS Code after installing it.
If 'Select Kernel' appears at the top right, click it and choose your Python installation. If no Python appears, your Python installation may not be on the PATH.
You should see Hello from VS Code! printed below the cell. You now have a
working Jupyter notebook inside VS Code.
print("Hello from VS Code!")
# Output: Hello from VS Code!
Click "+ Code" (or press B when no cell is selected) to add a new cell. Try these:
2 + 3
10 * 5
7 / 2
2 ** 10
What does ** do? What does / give you?
** is Python's exponentiation operator. 2 ** 10 means $2^{10}$ = 1024.
/ always gives a decimal (float). Use // for integer division.
print(2 + 3) # 5
print(10 * 5) # 50
print(7 / 2) # 3.5
print(2 ** 10) # 1024
In a new cell:
name = "your name here"
print("Hello, " + name + "!")
Replace "your name here" with your actual name. Run it, then change the name
and run again.
Make sure your name is inside quotes. Without quotes, Python treats it as a variable name and throws a NameError.
name = "Alice"
print("Hello, " + name + "!")
# Output: Hello, Alice!
You're set up with VS Code!
You can open any .ipynb file from this book in VS Code — just download the
notebook and open it with File → Open File. VS Code will render it just like
Colab does.
Now continue to Part 4 for your first real program.
Whichever path you chose, you now have a working notebook. Let's write a few small programs to make sure everything clicks — and learn a couple of important lessons along the way.
Before running each line, write down what you think it will print. Then run it and check.
print(3 + 4)
print("3 + 4")
print("Result:", 3 + 4)
Why do the first two give different outputs?
The first line computes 3 + 4 and prints the number 7. The second prints the text 3 + 4 — the quotes tell Python it's a string, not a calculation.
print() can take multiple things separated by commas. It prints them with spaces in between: print("Result:", 7) gives Result: 7.
Quotes make all the difference. Without quotes, 3 + 4 is an expression that
Python evaluates. With quotes, "3 + 4" is just text — Python doesn't touch it.
print(3 + 4) # 7 — Python computes the sum
print("3 + 4") # 3 + 4 — Python prints the text as-is
print("Result:", 3 + 4) # Result: 7 — mixed text and computation
Type this exactly — with the deliberate mistake:
print("Hello World)
Run it. You'll get an error. Read the error message carefully:
Errors are not failures — they're Python telling you exactly what went wrong. Learning to read error messages is one of the most important programming skills.
You should see a SyntaxError with a message about an unterminated string. The closing " is missing.
Fix: print("Hello World") — add the missing quote before the closing parenthesis.
Python says SyntaxError: unterminated string literal. It even points to where
the problem is. Every error message has useful information — always read it
before asking for help.
# The bug: missing closing quote
# print("Hello World) # SyntaxError: unterminated string literal
# The fix:
print("Hello World")
Write a program that:
namefavHere's a skeleton to get you started:
name = "..."
fav = ...
print(name, "likes the number", fav)
print(fav, "squared is", fav ** 2)
Replace "..." and ... with your actual name and number.
Remember: text goes in quotes (name = "Alice"), numbers don't (fav = 7).
The ** operator raises a number to a power. fav ** 2 squares it.
You used variables, arithmetic, and print() — the three building blocks
you'll use in every chapter of this book.
name = "Alice"
fav = 7
print(name, "likes the number", fav)
print(fav, "squared is", fav ** 2)
# Output:
# Alice likes the number 7
# 7 squared is 49
Congratulations — you're ready to start inventing!
You now know how to:
print() to see output+, -, *, /, **)These are the only tools you need. Every algorithm in this book will be built from just these basics — one small step at a time.
Quick Reference
| What | How |
|---|---|
| Run a cell | Shift+Enter |
| Add a new cell | + Code button (Colab) or B key (VS Code) |
| Print something | print("Hello") |
| Store a value | x = 42 |
| Add / subtract | + / - |
| Multiply / divide | * / / |
| Power (exponent) | ** — e.g. 2 ** 10 = 1024 |
| Text (string) | Wrap in quotes: "Hello" |
| Read an error | Look at the last line — it tells you what went wrong |
.ipynb.