Your First Python Program
See what Python is, run code in the interactive prompt and in scripts, use print, write comments, and read your first error messages calmly.
Welcome back!
Step 1 of 14Learn
What Python is and why people choose it
Every week, Sam spends two hours copying numbers from emailed reports into a spreadsheet. A colleague writes a 20-line Python script that does it in four seconds. That's the everyday promise of programming: you describe a task precisely once, and the computer repeats it perfectly, as often as you like.
Python is a programming language: a precise way of writing instructions a computer can follow. It was designed to be readable, so its code often looks close to plain English. Compare a Python line like if age >= 18: with the punctuation-heavy syntax of many older languages. That readability is why Python is a favorite first language, and also why professionals use it for data analysis, automation, web servers, testing, science and machine learning.
Python code is run by a program called the interpreter. You write instructions as text; the interpreter reads them one line at a time, from top to bottom, and carries them out. If a line makes no sense to it, it stops and tells you why.
This course uses Python 3. You may still find old tutorials written for Python 2, which was retired in 2020; small differences (such as print "hi" without parentheses) will not work in Python 3. To follow along, install Python from python.org or open any free online Python editor in your browser.
Step 2 of 14Diagram
From your code to a result
Tap a shape with to see more.
- You write code
Plain text in a .py file or typed at the prompt.
- Interpreter reads it
The Python program checks the syntax and turns your code into instructions.
- Runs line by line
Top to bottom, one statement at a time.
- Output or error
Text printed, files written, or an error message explaining what went wrong.
Step 3 of 14Learn
Two ways to run code
There are two ways to talk to the interpreter, and you'll use both.
1. The interactive prompt (REPL). Type python (or python3, or py on Windows) in a terminal, or open IDLE. You'll see the prompt >>>. Type a line, press Enter, and Python answers immediately. It is perfect for quick experiments:
>>> 2 + 3
5
>>> "Py" + "thon"
'Python'2. Scripts. For anything longer, write the code in a file whose name ends in .py, such as hello.py, and run it with python hello.py or the Run button in your editor. A script runs top to bottom and then ends:
print("Hello, world!")
print("Python is running my code.")print is a built-in function: a named action that you call by writing its name followed by parentheses. Whatever you put inside the parentheses is shown on the screen. Text must be wrapped in quotes, either double ("…") or single ('…'), so Python knows it's text and not a command. Numbers and calculations need no quotes.
One difference to remember: in the REPL, typing an expression shows its value automatically. In a script, nothing appears on screen unless you print it.
Step 4 of 14Compare
REPL or script?
| Aspect | Interactive prompt (REPL) | Script file (.py) |
|---|---|---|
| Looks like | >>> prompt, one line at a time | A saved text file you run |
| Shows results | Automatically, after each expression | Only what you print() |
| Kept afterward? | No, gone when you close it | Yes, rerun any time |
| Best for | Quick experiments, checking a value | Real programs you reuse |
Step 5 of 14Practice · Choose one
What does the line print("7 + 3") display?
Step 6 of 14Learn
How Python reads your code
The interpreter is extremely literal. It does exactly what you wrote, not what you meant. A few rules prevent most early errors:
- Case matters.
printworks;PrintandPRINTcause aNameError, because Python sees a different, unknown name. - Quotes must match. "Hello' starts with a double quote and ends with a single one, which is a
SyntaxError. If your text contains an apostrophe, wrap it in double quotes: "Let's go". - Brackets must close. Every ( needs a matching ). A missing parenthesis often makes Python complain about the next line, so look one line up.
- One instruction per line. Python reads line by line, from the top. A line cannot use something that is only defined further down.
- Indentation means something. Spaces at the start of a line show structure (you'll see this with if statements and loops). For now, start every line at the left edge.
Comments are notes for humans. Anything after a # on a line is ignored by Python:
# Calculate the total for one order
print(3 * 4.50) # three items at $4.50Good comments explain why the code does something, not what is obvious from the code itself. They are also handy for temporarily switching a line off while you test.
Step 7 of 14Practice · Spot the mistake
A beginner wrote this five-line script. Tap the lines that will cause an error.
Step 8 of 14Note
Step 9 of 14Learn
Errors are messages, not failures
Every programmer, at every level, sees error messages all day. An error is the interpreter telling you precisely what confused it. Learning to read them calmly is one of the most valuable skills you'll build.
Suppose a script contains print("Total:" 42), missing the comma between the two items. Python responds with something like:
File "shop.py", line 3
print("Total:" 42)
^^^^^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?Read it from the bottom up:
- The last line names the kind of error and often suggests a fix. Recent Python versions give surprisingly helpful hints like "Perhaps you forgot a comma?".
- The line number tells you where Python noticed the problem.
- The carets (^) point at the spot.
A SyntaxError means the code breaks Python's grammar, so nothing runs at all. Other errors, such as NameError, happen while the program is running: lines before the bad one still run, and the program stops at the error.
Fix the code, run it again, repeat. This loop of write, run, read the error, fix is what programming actually looks like, even for experts. And print can show several items at once if you separate them with commas: print("Total:", 42) prints Total: 42, adding a space between items automatically.
Step 10 of 14Tap to reveal
Reading an error message
Start at the last line
It names the error type, such as
NameError, and says what confused Python.Find the line number
Look for
line 3or similar: that is where Python noticed the problem.Look just before it, too
A missing
)or"often breaks the line after the real mistake.Fix one thing and rerun
Change one thing at a time so you know what fixed it.
Step 11 of 14Practice · Code challenge · Python
Code challenge · Python
Write a three-line program that prints exactly:
Hello, Python!
I am learning to code.
2 + 3 = 5Use one print() call per line. The third line can be a single string, or you can pass several items to print() separated by commas.
- Printed output matches the expected text
Step 12 of 14Practice · True or false
Python ignores everything after a # symbol on a line, unless the # is inside a string.
Step 13 of 14Practice · Match the pairs
Match each term to its description.
Tap an answer, then tap the place where it belongs (or drag it there).
- Interpreter
- Script
- REPL
- Comment
- SyntaxError
Step 14 of 14Practice · Choose one
What does print("Score:", 8, "of", 10) display?
Lesson complete!