← All lessons
Python basics · Lesson 1 of 10

Your first program

A program is a list of instructions for a computer, written in a language it can follow. The computer does exactly what the instructions say, one line at a time from the top, and nothing else. It never guesses what you meant, which is annoying at first and very useful later.

This course uses Python, one of the most popular programming languages in the world. It's used for websites, science, data, games and automating boring jobs, and it reads more like English than most. To run Python you type it into an editor and press Run: install it free from python.org, or search for "online Python editor" and use one in your browser. You don't need either to follow the lessons here, but trying the examples yourself is the fastest way to learn.

The first instruction is print(). Whatever you put between its brackets is shown on the screen. Something written with a name and brackets like this is called a function: a ready-made job you can ask Python to do. Asking is called calling the function.

print("Hello, world!")
print("I am learning Python.")
Prints
Hello, world!
I am learning Python.

Text goes inside quotes, either "double" or 'single'. Python calls a piece of text a string (a string of letters). The quotes tell Python "this is text, don't try to understand it as an instruction". Numbers don't need quotes, and Python can do sums with them.

print(7)
print(3 + 4)
print("3 + 4")
Prints
7
7
3 + 4

If you give print() several things separated by commas, it prints them all on one line with a space between each.

print("Total:", 3 + 4)
Prints
Total: 7

When Python can't follow a line, it stops and prints an error message. Everyone gets these all day, beginners and experts alike. The last line of the message says what went wrong, and it names the line it got stuck on. Forgetting a closing quote or bracket is the most common one, and Python reports it as a SyntaxError (syntax means the grammar of the language).

A # starts a comment: Python ignores everything after it on that line. Comments are notes for people reading the code.

# This line is a note and does nothing
print("Hi")  # a comment can follow code too
Prints
Hi

Practice

What does this print?

print("Good morning")
More info

Quick reference

programA list of instructions the computer follows from the top, one line at a time.
print(x)Shows x on the screen. Several things with commas between are printed on one line with spaces.
"text" or 'text'A string: a piece of text. The quotes aren't part of it.
# noteA comment. Python ignores the rest of the line.
SyntaxErrorPython couldn't read the line at all, often a missing quote or bracket.

Related courses