Hello, World!
Your first program is one line long. It tells Python to show a message on the screen. Every example on this page runs right here in your browser: press Run, change something, run it again.
1print("Hello, World!")Hello, World!
That line is a complete Python program. Here is what each piece does:
| Piece | What it is |
|---|---|
print | A built-in function: a ready-made command that shows things on the screen. |
( ) | Whatever goes between the parentheses is handed to print. Parentheses always come in pairs. |
"Hello, World!" | A string: a piece of text. The quotes say where the text starts and ends; they are not shown. |
One line at a time
A program can be many lines long. Python runs them in order, from the top down. Press Step to watch it happen one line at a time.
1print("Wick wakes up")2print("Lamps to light:", 3)3print("Off we go!")
Wick wakes up Lamps to light: 3 Off we go!
Tip: Step works on every example in the course. Use it whenever a result surprises you.
Text needs quotes
Quotes tell Python "this is text, show it as it is". Without quotes, Python treats what it sees as code and works it out first.
What does the second line print?
1print("7 + 3")2print(7 + 3)
The first line has quotes, so it shows the text 7 + 3. The second has none, so Python does the sum and shows 10.
Single quotes work too, as long as both ends match. Pick the kind that is not inside your text:
1print('Single quotes work too')2print("It's easy with double quotes")3print('She said "hi"')
Single quotes work too It's easy with double quotes She said "hi"
Several things on one line
Give print several values, separated by commas. It puts a space between them. Two optional settings change that: sep goes between the values, end goes after the last one.
1print("Lamps:", 3, "Houses:", 12)2print("09", "45", sep=":")3print("Loading", end="...")4print("ready")
Lamps: 3 Houses: 12 09:45 Loading...ready
| Setting | What it does | If you leave it out |
|---|---|---|
sep="..." | Goes between the values | one space |
end="..." | Goes after the last value | a new line |
Special characters
A backslash inside a string starts a short code for a character you cannot type directly:
| Write | You get |
|---|---|
\n | a new line |
\t | a tab, for lining things up |
\" or \' | a quote that does not end the string |
\\ | one backslash |
1print("Lamp 1\nLamp 2")2print("Name:\tWick")3print("Wick said \"good night\"")
Lamp 1 Lamp 2 Name: Wick Wick said "good night"
Comments
Python ignores everything after a # on a line. Use comments to leave notes for people, or to switch a line off without deleting it.
1# This line is a note for people2print("Lit") # a note after code3# print("This line is switched off")
Lit
Your first error
Mistakes are normal. When Python cannot run a line, it stops and tells you why. Run this:
1print("Good evening")2prnt("Lamps on")
Good evening
Traceback (most recent call last):
File "main.py", line 2, in <module>
prnt("Lamps on")
NameError: name 'prnt' is not defined. Did you mean: 'print'?Read an error from the bottom up. The last line names the problem: Python has never heard of prnt. The lines above say where: line 2, and show that line. Line 1 had already run, so its message is there.
| Mistake | Example | Error |
|---|---|---|
| Missing closing quote | print("Hi) | SyntaxError |
| Missing parenthesis | print("Hi" | SyntaxError |
| Capital letter | Print("Hi") | NameError |
| Text without quotes | print(Hi) | NameError |
A SyntaxError means Python could not read the program at all, so not even line 1 runs. A NameError happens while it runs, when Python meets a name it does not know.
Practice
Write the code, then press Check. A hint costs nothing; the answer is there when you need it.
An apostrophe
✓Print the sentence below exactly, apostrophe included.
I'm learning Python!
1# Your code hereTell the time
✓Use one print with two values and sep to show the time.
09:45
1print("09", "45")Fix three bugs
✓This program has three mistakes. Fix them so it prints three lines.
Ready Set Go
1Print("Ready")2print("Set)3print "Go"
What you learned
print()shows values on the screen; Python runs lines from top to bottom.- Text goes in matching quotes. Without quotes, Python works it out as code.
- Commas print several values;
sepandendchange what goes between and after. \nstarts a new line;#starts a comment.- Read errors from the bottom up: what went wrong, then where.