DataLens

Your first program: print, comments, and running code

What a program is, how Python runs it line by line, and your first tools: print() and comments.

Level: Foundations · 8 min read

Practise this module

The questions for this lesson run in your browser and check your answer straight away.

The idea

A program is a list of instructions written in a language the computer understands. Python reads your instructions from top to bottom, one line at a time, and does exactly what each line says. Not what you meant, what you wrote. That sounds harsh, but it's also freeing: when something goes wrong, the answer is always somewhere in the lines you can see.

Think of it like a recipe. A recipe is useless until someone follows it, step by step, in order. Python is the cook. Your job is to write a recipe so clear that a very literal cook can't get it wrong.

Key idea

Python runs your file from the first line to the last. If line 3 breaks, lines 4 onwards never run.

Your first instruction: print()

The first tool every programmer learns is print(). It takes whatever you put between the brackets and shows it on the screen.

print("Hello, Python!")
print("I'm learning to code.")
Hello, Python!
I'm learning to code.

A few things are happening here, and every one of them matters:

Press Try it on the code above to run it yourself. Change the words and run it again. You can't break anything here.

Printing several things at once

You can give print() more than one value, separated by commas. Python puts a space between them:

print("Coffee", "Tea", "Juice")
print("Total:", 3, "drinks")
Coffee Tea Juice
Total: 3 drinks

Notice 3 has no quotes. It's a number, not text, and Python is happy to print numbers too. Try print(2 + 3) and then print("2 + 3"). The first does the maths and shows 5. The second shows the text exactly as written, because anything in quotes is just text to Python.

Two handy options: sep and end

print() has optional settings you can change by name. sep is the separator placed between values (normally a space), and end is what's printed at the very end (normally a new line).

print("2024", "10", "05", sep="-")
print("Loading", end="...")
print("done!")
2024-10-05
Loading...done!

The first line joins the values with dashes. On the second, end="..." replaces the usual new line with three dots, so the next print() continues on the same line.

Comments: notes for humans

A comment is a note in your code that Python completely ignores. In Python, a comment starts with # and runs to the end of the line.

# Monthly report for the sales team
print("Sales report")  # the title the manager asked for

Python sees only print("Sales report"). The comments are for the next person who reads your code, and very often that person is you, three months from now, wondering what on earth you were thinking.

Clean code

Good comments explain why, not what. print("Sales report") # prints the title adds nothing, since the code already says that. # Finance needs the title on its own line for their import script explains a decision you can't see in the code.

Comments are also useful for temporarily switching off a line while you experiment. Put a # in front of it and Python skips it. In the editor on this site, select some lines and press Ctrl + / (or Cmd + / on a Mac) to comment or uncomment them in one go.

Running code: here and on your computer

On this site, press Run to execute your code and see what it prints. Press Submit to run it and check it against the question's requirements.

On your own computer, the usual way is:

  1. Write your code in a file ending in .py, for example hello.py.
  2. Open a terminal in that folder.
  3. Type python3 hello.py and press Enter.
python3 hello.py

Python reads the file top to bottom and prints the results in the terminal. That's all "running a program" means.

Errors are normal (and helpful)

Every programmer sees errors every day. An error isn't Python being mean, it's Python telling you exactly where it got confused. Here's a classic:

print("Hello)
  Line 1
    print("Hello)
          ^
SyntaxError: unterminated string literal

Read errors from the bottom up. The last line names the problem: a SyntaxError means the code doesn't follow Python's grammar, and "unterminated string literal" means a string was opened with a quote but never closed. The line number and the little ^ arrow show where. Add the missing " and it works.

Here's another one you'll meet often:

prnt("Hello")
NameError: name 'prnt' is not defined

A NameError means Python doesn't recognise a name. Here it's a typo: prnt instead of print. Python never guesses what you meant, so spelling and capital letters must be exact. Print("Hi") with a capital P fails too.

Watch out

When a line has an error, Python stops there. Anything after it doesn't run, so fix the first error first, then run again.

Why this matters for clean code

Even a two-line program has a reader. Using clear text, one instruction per line and comments that explain your reasons makes code easy to follow, and code that's easy to follow is easy to fix. These habits feel small now, but they're the same ones that keep a 5,000-line project readable.

Recap

Practise this module

The questions for this lesson run in your browser and check your answer straight away.

Next: Variables and basic data types →