Find a course

Module 1: Getting Started With PythonLesson 1 of 1011 min

Your First Program

Write your first Python program with print(), add comments, and learn to read the error messages Python shows when something goes wrong.

Step 1 of 15Learn

One line that talks back

Maya wanted her Discord bot to greet new members. She expected to need hundreds of lines of mysterious code. The first working version of the greeting was this:

print("Welcome to the server!")

That is a complete Python program. When you run it, the computer shows Welcome to the server! on the screen. print() is a function: a named tool that does a job. You call it by writing its name followed by parentheses, and whatever you put inside the parentheses is the information it works with.

The text inside the quotes is called a string. The quotes tell Python "this is text, not a command." You can use double quotes "like this" or single quotes 'like this', as long as the opening and closing quotes match.

A program is just a list of instructions that run from top to bottom, one line at a time:

print("Level 1")
print("Loading map...")
print("Go!")

This prints three lines in that exact order. Computers are fast but extremely literal: they do exactly what you wrote, not what you meant. Most of learning to code is learning to say precisely what you mean.

Step 2 of 15Diagram

What happens when you press Run

What happens when you press RunProcess diagram: You write the code → Python reads line 1 → Each line runs in turn → Output appears.You writethe codePython readsline 1Each line runsin turnOutputappears

Tap a shape with to see more.

You write the code

Plain text in a .py file or an online editor.

Python reads line 1

Then line 2, then line 3: always top to bottom unless told otherwise.

Each line runs in turn
Output appears

Anything passed to print() shows up on the screen.

Step 3 of 15Practice · Choose one

What does this program show on the screen? `` print("3 + 4") print(3 + 4) ``

Step 4 of 15Compare

Quotes change everything

Aspectprint("3 + 4")print(3 + 4)
Python seesA string: just charactersA math expression
Screen shows3 + 47
Use it forMessages and labelsCalculations

Step 5 of 15Learn

Comments and multiple values

Code is read by people as often as by computers, including future you, two weeks from now, wondering what you were thinking. Comments are notes for humans. Anything after a # on a line is ignored when the program runs:

# Greeting for new players
print("Hi!")  # this runs
# print("Bye!")  this line is switched off

Programmers also use # to "comment out" a line temporarily while testing, like the last line above.

print() can take several values separated by commas. It puts a space between them automatically:

print("Score:", 250)
print("Lives", 3, "left")

These print Score: 250 and Lives 3 left. Notice that 250 and 3 have no quotes. They are numbers, and print() can show numbers just as well as text.

An empty print() prints a blank line, which is handy for spacing out your output so it is easier to read.

Step 6 of 15Practice · Fill in the blanks

Fill in the blanks

Complete the program so it prints Ready Player One on a single line, with a space added automatically between the two values. print("Ready Player" "One")

Step 7 of 15Learn

Errors are messages, not failures

Every programmer, including professionals with 20 years of experience, sees error messages every day. An error doesn't mean you are bad at coding. It means Python found something it can't understand and is telling you where.

When a program crashes, Python prints a traceback. Read it from the bottom up. The last line names the error type and gives a short message; the lines above show which line of your file caused it.

  File "game.py", line 2
    print("Game over)
          ^
SyntaxError: unterminated string literal

This says: on line 2, a string starts but never ends. The closing quote is missing.

The three errors you'll meet most often as a beginner:

  • SyntaxError: the code breaks Python's grammar rules: a missing quote, parenthesis or colon.
  • NameError: you used a name Python doesn't know, often a typo like prnt or Print. Python is case sensitive, so Print and print are different names.
  • TypeError: you combined values that don't fit together, like adding text to a number.

Fix the first error first. One small mistake can confuse Python about everything after it.

Step 8 of 15Practice · Diagram

Reading a traceback

Read it from the bottom up: the last line tells you what, the lines above tell you where.

Which part tells you what KIND of mistake you made?

Tap the shape that answers the question, then press Check.

Reading a tracebackParts diagram: Traceback, File and line number, The line of code, The ^ marker, Error type, Short message.TracebackFile and linenumberThe line ofcodeThe ^ markerError typeShortmessage
Traceback
File and line number

File "game.py", line 2: where to look.

The line of code

A copy of the line that caused trouble.

The ^ marker

Points near the spot Python got confused.

Error type

SyntaxError, NameError, TypeError…

Short message

"unterminated string literal": the quote never closes.

Step 9 of 15Practice · Spot the mistake

Each line below is meant to print a message. Tap every line that will cause an error.

Step 10 of 15Practice · Code challenge · Python

Code challenge · Python

Write a program that prints this loading screen for a game, exactly as shown, on three lines:

Loading level 1...
Player: Nova Lives: 3
Good luck!

Use one print() per line. For the middle line, pass "Player:", "Nova", "Lives:" and the number 3 to a single print() separated by commas, and let print() add the spaces.

Real Python, running in a sandbox in your browser.
  • Printed output matches the expected text

Step 11 of 15Practice · Match the pairs

Match each error type to the mistake that most likely caused it.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. SyntaxError
  2. NameError
  3. TypeError
  4. IndentationError

Step 12 of 15Note

Step 13 of 15Practice · True or false

A comment that starts with # is run by Python after all the other lines have finished.

Step 14 of 15Practice · Put in order

Put these lines in order so the program prints a countdown: 3, 2, 1, then Liftoff!

Drag the items, or use the arrow buttons, to put them in order.

  1. print(1)
  2. print("Liftoff!")
  3. print(3)
  4. print(2)

Step 15 of 15Practice · Choose one

Your program crashes and the last line of the traceback says NameError: name 'scroe' is not defined. What is the most likely fix?