Your first program
Last updated
Your first Python program prints one line of text. You'll run it, change what it says, then break it on purpose and read what Python tells you.
By the end of this lesson you can
- Say what a program is, and in what order Python runs its lines
- Run a program and change what it prints
- Read a
SyntaxErrorand find the line it points at
What a program is
A program is a list of instructions, a lot like a recipe. A recipe says boil the water, add the pasta, wait ten minutes, and you do the steps in that order. The computer works through a program the same way, one instruction at a time, from the top down.
The difference is that a cook fills in gaps. If a recipe forgets to say "drain the pasta", you'd do it anyway. The computer never guesses what you meant: it does exactly what the lines say, in the order they say it, and nothing else. That's why a program has to spell out every step.
This program has three instructions, one on each line. Press Watch it run, then Next to go one line at a time. The pink line is the one that just ran, and the box underneath shows what has been printed so far.
Where it starts: the top
A Python program starts at its first line and works down. There's nothing to mark the start: the first instruction in the file is the first one that runs. (Some languages, like Rust, start at a special place called main. Python doesn't need one.)
Run it
print shows something on the screen. The round brackets hold what it should show. The text itself goes between double quotes, so Python knows where the text starts and stops.
Programmers have started with this one for about fifty years. Press Run.
Change the text
Change Hello, world! to something else, like your name, and run it again. Only change what's between the quotes.
Then add a second line: copy line 1, paste it underneath, change its text, and run it. Each print prints its own line, in the order they're written.
Read your first error
Now break it on purpose. In the code below, delete the " just after world!, then press Run.
Python reads the whole program before it runs any of it, and it can't read this one, so instead of output you'll get an error. The first lines are what Python really prints: the file, the line number, the line itself, and a ^ under the place where it got stuck. The last line names the problem. Under it is the same message in plain words.
Put the quote back and run it again to make sure it works. Reading errors is most of programming, so every lesson in this course has a step like this one. A good habit starts here: read an error from the bottom up. The last line says what went wrong; the lines above it say where.
Check what you learned
Three questions. Pick or type an answer, then check it.
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- The Python Tutorial: An Informal Introduction to Python, Text: text in quotes
- Built-in Functions: print(): what print does
- The Python Tutorial: Errors and Exceptions, Syntax Errors: reading a SyntaxError
- The Rust Programming Language: Anatomy of a Rust Program: Rust programs start in main