Your First Program
Every data science workflow — whether in a Jupyter Notebook, Google Colab, or a script — starts with running code and reading output. This chapter gets you writing and running your first lines of Python.
Before learning theory, edit the code text below and click ▶ Run to see Python execute your input live.
1.1 Statements and Expressions
A statement (or expression) is an instruction that the computer will run, or execute. Python reads your code line by line and carries out each instruction in order.
The simplest statement you can write is a print statement.
1.2 The print() Function
When you run a print() statement, Python displays whatever value is inside the parentheses. That value is called the argument.
In data science, print() isn't just a "hello world" novelty — it's one of your most-used tools for inspecting data as you work: checking a variable's value, confirming a dataset loaded correctly, or debugging a calculation.
# Printing a number computed from a dataset
average_score = 87.5
print(average_score)
You can also print multiple arguments at once, separated by commas — Python inserts spaces between them automatically:
# Multiple arguments separated by commas
print("Average score:", average_score)
1.3 Running Code in Jupyter Notebooks
Most data science work happens in a Jupyter Notebook (or Google Colab), not a plain script file. Notebooks let you run code in small, self-contained chunks called cells, and see the output immediately below each one.
This "write a little, run it, see the result" rhythm — the same one you tried above in the live cell — is central to how data scientists explore data.
1.4 Writing Comments
It's good practice to comment your code. A comment explains what your code does — for your future self, or for teammates reading your work later. Put a hash symbol (#) before your comment text; Python ignores anything after # on that line.
# This calculates the average of three exam scores
scores = [85, 90, 88]
average = sum(scores) / len(scores)
print(average) # Displays the result
1.5 Errors: Syntax vs. Semantic
As you write code, you'll run into two very different kinds of problems.
Syntax errors happen when Python doesn't understand your code. Python stops and shows you an error message. Semantic errors happen when your code runs fine, but the logic is wrong — Python won't warn you.
Key Takeaways
- A statement/expression is an instruction Python executes.
print()displays its argument — your go-to tool for checking values as you work with data.- Jupyter Notebooks run code in cells; write, run, and see output immediately below.
- Use
#to write comments that explain your code. - Syntax errors: Python doesn't understand your code (it tells you). Semantic errors: your code runs, but the logic is wrong (you have to catch it).