Lesson 1 of 12 · about 10 minutes
Your first program
What a program is
A program is a list of instructions. Python reads them from top to bottom and does each one in turn. Your first instruction will be the one almost every programmer starts with: show some text on the screen.
print("Hello, world!")Run it and you get:
Hello, world!How print works
print is a function: a named command that does a job. You call it by writing its name followed by round brackets, and you put what you want shown inside the brackets.
Text has to go inside quotes. Python calls a piece of text a string. Single and double quotes both work, as long as they match:
print("Double quotes work")
print('So do single quotes')Every print starts a new line. Calling it three times gives you three lines:
print("One")
print("Two")
print("Three")You can also give print several things at once, separated by commas. It puts a space between them:
print("I am", 12, "years old")I am 12 years oldNotice that 12 has no quotes. It is a number, not text. You will learn more about numbers in the next lesson.
Quick check
What does this print?
print("Hi", "there")Comments
A line that starts with # is a comment. Python ignores it completely. Comments are notes for people reading your code, including future you.
# This line is a comment, so nothing happens here
print("This line runs") # A comment can also go at the end of a lineReading error messages
Everyone makes mistakes, and Python tells you when it cannot understand something. Try running this:
print("Hello, world!)The closing quote is missing, so Python stops with a SyntaxError. Syntax means the grammar of the language. The message includes the line number, which tells you where to look.
Another common one is a NameError:
Print("Hello")Python is case sensitive. print and Print are different names, and only the lowercase one exists. Error messages can look scary at first, but they are clues. Read the last line first: it says what kind of problem Python found.
Quick check
Which of these lines causes a NameError?
Exercise 1: Say hello
Print the message Hello, world! exactly as written, with the comma and the exclamation mark.
Exercise 2: Ready, set, go
Print three lines: Ready, then Set, then Go!. The first line is done for you.
Lesson 1 complete
You finished your first program and passed every exercise.
What you learned
print(...)shows something on the screen, one line per call.- Text goes in quotes and is called a string.
- Lines starting with
#are comments and are ignored. - Error messages tell you what went wrong and on which line.