Kodewick
Lesson 1 of 31Contents

Hello, World!

Lesson 1 of 31 · Part 1 · Basics · about 15 minutes

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.

main.py
1print("Hello, World!")
Output
Hello, World!

That line is a complete Python program. Here is what each piece does:

PieceWhat it is
printA 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.

main.py
1print("Wick wakes up")2print("Lamps to light:", 3)3print("Off we go!")
Output
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.

Guess first

What does the second line print?

1print("7 + 3")2print(7 + 3)

Single quotes work too, as long as both ends match. Pick the kind that is not inside your text:

main.py
1print('Single quotes work too')2print("It's easy with double quotes")3print('She said "hi"')
Output
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.

main.py
1print("Lamps:", 3, "Houses:", 12)2print("09", "45", sep=":")3print("Loading", end="...")4print("ready")
Output
Lamps: 3 Houses: 12
09:45
Loading...ready
SettingWhat it doesIf you leave it out
sep="..."Goes between the valuesone space
end="..."Goes after the last valuea new line

Special characters

A backslash inside a string starts a short code for a character you cannot type directly:

WriteYou get
\na new line
\ta tab, for lining things up
\" or \'a quote that does not end the string
\\one backslash
main.py
1print("Lamp 1\nLamp 2")2print("Name:\tWick")3print("Wick said \"good night\"")
Output
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.

main.py
1# This line is a note for people2print("Lit")  # a note after code3# print("This line is switched off")
Output
Lit

Your first error

Mistakes are normal. When Python cannot run a line, it stops and tells you why. Run this:

main.py
1print("Good evening")2prnt("Lamps on")
Output
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.

MistakeExampleError
Missing closing quoteprint("Hi)SyntaxError
Missing parenthesisprint("Hi"SyntaxError
Capital letterPrint("Hi")NameError
Text without quotesprint(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.

Exercise 1

An apostrophe

Print the sentence below exactly, apostrophe included.

It should print
I'm learning Python!
exercise1.py
1# Your code here
Output
Press Run or Ctrl+Enter
Exercise 2

Tell the time

Use one print with two values and sep to show the time.

It should print
09:45
exercise2.py
1print("09", "45")
Output
Press Run or Ctrl+Enter
Exercise 3

Fix three bugs

This program has three mistakes. Fix them so it prints three lines.

It should print
Ready
Set
Go
exercise3.py
1Print("Ready")2print("Set)3print "Go"
Output
Press Run or Ctrl+Enter

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; sep and end change what goes between and after.
  • \n starts a new line; # starts a comment.
  • Read errors from the bottom up: what went wrong, then where.