Chapter 1 of ?
python 9 min read

Python for Data Science — Chapter 1: Your First Program

Module 1 · Chapter 1

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.

Live Interactive Coderunner

Before learning theory, edit the code text below and click ▶ Run to see Python execute your input live.

In [1]:
print( )
Out[1]:
Hello, Data Science!

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.

Anatomy of a print() Statement function name print("Hello, Data Science!") the argument — everything inside the parentheses gets printed ▶ what appears beneath the cell when you run it Hello, Data Science!

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)
OutputPython Output
87.5

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)
OutputPython Output
Average score: 87.5
Try It Yourself »

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.

Running Code in a Jupyter Notebook Cell Run average_score = 87.5 print(average_score) Out[1]: 87.5 1 Write your code in the cell 2 Click ▶ Run (or Shift+Enter) 3 See the result instantly below

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 Error vs. Semantic Error Syntax Error Python doesn't understand your code frint("Hello") NameError: name 'frint' is not defined Python stops right away and points you to the problem. ! Semantic Error Your logic is wrong — Python won't say so tax_rate = 10 # meant 0.10 ✓ Runs successfully But setting tax_rate = 10 is logically wrong. Python has no way to know what you actually intended.

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.

Warning in Data Science: Semantic errors are the dangerous ones: a script that runs without crashing but silently miscalculates an average or mislabels a column can lead to wrong conclusions nobody notices.

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).

Practice Exercises

Chapter 1 Quiz — Test Your Knowledge

1. What is the primary purpose of the print() function in Python?

2. Which of the following is a SEMANTIC error?

3. Which character starts a single-line comment in Python?

Done with this chapter?
Mark it complete to track your progress and unlock your certificate.
Next Up

Learner Reviews

Write a Review
Share your experience to help other learners.
Your Rating *