Velamuvi AIAcademy

Lesson 2 of 30 · Getting Started

Your First Python Program

Write a real Python program of several lines. Learn how Python reads code from top to bottom, how to add comments and how to read your first error message.

  • 6 min read
  • Beginner
  • Reviewed
In this lesson you will learn
  • How Python runs a program, line by line
  • How to print several things at once
  • What comments are and why programmers write them
  • How to read an error message without panic

Python reads from top to bottom

A program is usually more than one line. Python starts at the first line, runs it, then moves to the next, until it reaches the end.

Each line that does something is called a statement. Run this program and check that the output appears in the same order as the code.

Try it yourselfPython
print("Step 1: Wake up")
print("Step 2: Learn Python")
print("Step 3: Build something amazing")

Try swapping two lines and run it again. The output changes order, because Python always follows your order.

Printing more than one thing

You can give print() several items, separated by commas. Python prints them on the same line with a space between them.

Try it yourselfPython
print("My favourite number is", 7)
print("Python", "is", "fun")
print("2 + 2 =", 2 + 2)

To print an empty line, call print() with nothing inside.

Try it yourselfPython
print("First line")
print()
print("Third line")

Comments: notes for humans

Any text after a # is a comment. Python ignores it completely. Comments are notes that explain your code to other people, and to you when you come back next month.

Try it yourselfPython
# This program greets the user
print("Welcome!")  # A comment can also go at the end of a line

# print("This line will not run")
print("Goodbye!")

Notice the third print did not run. Putting a # in front of code is called commenting it out. It is a quick way to switch a line off without deleting it.

Spaces at the start of a line matter

In Python, the space at the beginning of a line is called indentation, and it has meaning. For now, the rule is simple: start every line at the left edge. You will learn when to indent in the lesson on if statements.

Reading an error message

When Python cannot understand your code, it stops and shows an error message. This is not a failure. It is Python telling you exactly where to look.

Run this broken program.

Try it yourselfPython
print("This line is fine")
print("This line is missing a bracket"

Python shows something like this:

Output
line 2
    print("This line is missing a bracket"
         ^
SyntaxError: '(' was never closed

Read it from the bottom up:

  1. The last line names the problem: a bracket was opened but never closed.
  2. The line number tells you where: line 2.
  3. The ^ arrow points at the exact spot.

Now add the missing ) and run it again.

Practice exercise

Write a program that prints three lines: Python, then is, then easy.

Your solutionPython
# Print these three lines, in this order:
# Python
# is
# easy
Knowledge check

What does Python do with the text after a # symbol?

Summary

  • Python runs statements from top to bottom, one at a time.
  • print() accepts several items separated by commas.
  • # starts a comment, which Python ignores.
  • Every line starts at the left edge unless Python needs indentation.
  • Error messages tell you the type of problem and the line it is on.

Next, you will learn how to store information so your programs can remember things.

Finished reading? Tick it off to track your progress.