Course outline · 0% complete

0/27 lessons0%

Course overview →

Run your first line of Python

lesson 1-2 · ~10 min · 2/27

Time to run real code

In lesson 1-1 you learned that a program is a list of exact instructions. Now you will read one running.

Python is a programming language designed to be readable. You write Python as plain text, and a program called the Python interpreter reads your text from top to bottom and carries out each instruction, one line at a time. Carrying out the instructions is called running, or executing, the program.

Every code example in this course is shown together with the output it produces, so you can follow along by reading. When you want to run Python yourself, install it on your own machine and put the same lines in a file ending in .py.

The first instruction every programmer learns is print. Despite the name, it has nothing to do with paper. print means: show this on the screen.

A complete program in one line

This is a whole program. One line, and it produces one line of output.

print("Hello, world!")

Output

Hello, world!

Nothing is missing. Python has no required setup line, no wrapper, and no declaration of where the program starts, so a single instruction on its own is a valid program.

That is unusual and worth appreciating, because in many languages this same program needs five or six lines of scaffolding before the interesting one. Python's readability is largely an absence of ceremony.

Anatomy of that line

print("Hello, world!")

Three pieces:

  • print is the name of a built-in instruction. Python ships with many of these ready to use.
  • The parentheses ( ) hold the input you give to the instruction. In Python, an instruction's inputs always go inside parentheses right after its name, and that is how the interpreter tells "the thing to do" apart from "the thing to do it with".
  • "Hello, world!" is that input: a piece of text. Text in code is called a string, and the quotes mark where the string starts and ends. The quotes themselves are not printed.

Every symbol matters. Delete a quote or a parenthesis and Python refuses to run, reporting a syntax error, which means "I could not understand this as valid Python".

Getting error messages is completely normal. Professionals see them all day, and a syntax error is the friendliest kind, because the program never ran at all and nothing was damaged. Read the message, fix the line, run again.

What the quote marks do

The quotes mark where the string of text begins and ends.

Quotes are boundaries. They tell Python that everything between these marks is text to be used exactly as written, not instructions to follow.

They never appear in the output, which is why the screen shows Hello, world! without them. Their job finishes the moment Python has worked out where the string stops.

That distinction between text and instructions is one of the most important in programming. Without quotes, print(Hello, world!) would send Python looking for something named Hello, find nothing, and fail. The quotes are how you say "treat this as data".

Printing a sentence of your own

The same pattern prints any sentence, including the final period.

print("I am learning to code.")

Output

I am learning to code.

Reading the pieces

  • The line starts with print and an opening parenthesis, exactly as before. The instruction's name never changes based on what you give it.
  • The sentence goes inside double quotes, and then the parenthesis closes. The period lives inside the quotes, because it is part of the text rather than part of the Python.
  • Everything between the quotes is copied to the screen character for character. That includes spaces, capital letters, and punctuation, which is why the output matches the source exactly.