Skip to content
wannacode

Your first program

Problem

The computer does exactly what it says

The computer does not guess what you want. It runs commands, one after another, exactly as they are written. A sequence of such commands is a program.

Here is a whole program. It prints two lines on the screen:

hello.py
print("Welcome!")
print("This is your first program.")
Output
Welcome!
This is your first program.

This is the whole program: two lines, one command per line. There is no wrapper around them. You save them in a file called hello.py and run it.

The idea

What is in the program

  • The program is the file itself. Python runs its lines from top to bottom, from the first to the last.
  • print(...) prints the text in the parentheses and moves to a new line.
  • The text is in double quotes: "Welcome!". The quotes are not printed.
  • Each command is on its own line. You do not need a character at the end of the line.
  • The line starts at the very beginning, with no spaces in front. In Python, spaces at the start of a line matter. We will use them in the lesson on conditions.

You run the program from the terminal, in the file's folder:

Terminal
python hello.py

On macOS and Linux the command is often python3. Python reads the file and runs the commands in it. There is no separate step where the code is translated ahead of time: you write and you run.

More precisely, if you are curious
  • Uppercase and lowercase letters matter. Print is not the same as print, and the program stops with an error when it reaches that line.
  • Text can also be in single quotes: print('Welcome!') prints the same thing. In this course we write double quotes.
  • In other people's code you will see a line if __name__ == "__main__": at the bottom. It is for files that are also used as part of other programs. You do not need it for now.
  • Python is not only in the terminal. There are also editors that run the file with a button, but underneath they do the same thing: python hello.py.

The idea

Printing with and without a new line

Sometimes you want several pieces of text on one line. Then you tell print what to end with: end="" means "with nothing". The text is printed and the cursor stays on the same line.

print("Order ", end="")
print("#1024", end="")
print(" is confirmed.")
print("Delivery: tomorrow")
Output
Order #1024 is confirmed.
Delivery: tomorrow

The first two lines of the code use end="", so "Order", "#1024" and "is confirmed." come out together. After print without end="" the next text starts on a new line.

Pay attention to the spaces. They are inside the quotes, because the computer does not add a space by itself.

Follow the cursor: the text always comes out where it is.

print("Order ", end="")
print("#1024", end="")
print(" is confirmed.")
print("Delivery: tomorrow")
Step by step, in words
  1. print(…, end="") prints "Order " and the cursor stays on the same line.
  2. print(…, end="") prints "#1024" and the cursor stays on the same line.
  3. print prints " is confirmed." and the cursor moves to a new line.
  4. print prints "Delivery: tomorrow" and the cursor moves to a new line.
  5. The spaces are inside the quotes: the computer does not add a space by itself.

Try it

On how many lines?

What will the program print?

print("Train ", end="")
print("8:15")
print("Platform ", end="")
print("3", end="")
print()
Answer

The idea

Comments: notes for people

Sometimes you want to leave a note in the code: why it is like this, or what comes next. You write it as a comment. The computer skips comments.

  • # starts a comment that runs to the end of the line.
  • A comment over several lines is several lines, each with #.
# The coffee shop receipt.
# Prices are in euros.
print("Coffee €2.50")
# print("Croissant €1.80")
print("Total €2.50") # out of croissants
Output
Coffee €2.50
Total €2.50

The croissant line is code, but it is behind #, so it does not run. This is how you switch off a line for a while without deleting it.

A good comment says why, not what. The code itself shows what it does.

More precisely, if you are curious

In other people's code you will see text in triple quotes, """…""", over several lines. It is not a comment. It is text that the program does nothing with. It is used to describe functions. For notes, write #.

Try it

What will come out?

What will the program print?

print("9:00 Math")
# print("10:00 Physics")
print("11:00 History") # lunch is next
Answer

Problem

The program does not start

You wrote the program, you run it, and it does not even begin. Not a single line comes out. Instead of output you get a message:

welcome.py
print("Welcome to the course!"
print("Here we go.")
Message from Python
File "/home/student/welcome.py", line 1
print("Welcome to the course!"
^
SyntaxError: '(' was never closed

Here the closing parenthesis is missing on the first line. Before it runs the first line, Python reads the whole file and checks that it is written by the rules of the language. If it is not, it stops and runs nothing, not even the lines before the error. This is called a syntax error (SyntaxError).

The idea

How to read the message

The message tells you three things: the file, the line and what is wrong.

File "/home/student/welcome.py", line 1 means the file welcome.py (with the full path to it), line 1. Under it is the line itself, and the ^ points to the place where Python noticed the problem. The last line is the most important one: the kind of error and what is wrong. '(' was never closed means "the ( was not closed".

There is a second kind of error too. The program starts, runs the lines one after another and stops only at the line it cannot run:

welcome.py
print("Welcome to the course!")
Print("Here we go.")
Output and message
Welcome to the course!
Traceback (most recent call last):
File "/home/student/welcome.py", line 2, in <module>
Print("Here we go.")
^^^^^
NameError: name 'Print' is not defined. Did you mean: 'print'?
Print with a capital letter is a name Python does not know (NameError). It finds out only when it reaches that line, so the first line has already been printed. Again it gives the file and the line, and at the bottom is the kind of error. Did you mean: 'print'? is a hint.

The most common errors at the start:

  • a quote or a parenthesis is opened but not closed (SyntaxError);
  • spaces at the start of a line (IndentationError: unexpected indent);
  • an uppercase letter instead of a lowercase one: Print (NameError, when the program reaches the line).

Python shows only one syntax error at a time. You fix it, run again, and only then you see the next one, if there is one.

More precisely, if you are curious
  • Python tells you where it noticed the problem. Sometimes the error is a little higher up.
  • There are also errors that nobody catches: the program starts, but it does something other than what you want. We will hunt those in almost every lesson.

Try it

Find the bug

The program should print the exam schedule. It does not start: not even the first line comes out. Which line is the bug on?

Pick the line with the bug.

Exercise

Your first programs

Solve the tasks in your editor. Each one is a separate program. Compare your output with the sample, character by character.

1. Hello World. The traditional first program. Print:

Output
Hello, World!

2. Business card. Print a business card of three lines: name, job and email. You can use your own.

Sample output
Maria Ivanova
web developer
maria@example.com

3. Frame. Print today's cafeteria menu in a frame of asterisks. Watch the spaces, so the right side comes out straight.

Output
***************
* Today: soup *
* €5.40 *
***************

More tasks

  • One sentence in three parts. With three print(..., end="") commands and one print, print on one line "The shop is open from 9:00 to 19:00."
  • A schedule with a comment. Print a schedule of four classes. Then switch off the third line with #, without deleting it, and add a comment saying why.
  • An error on purpose. Remove the closing parenthesis from a line in task 1, then remove a quote, then write Print with a capital letter. Read what Python says each time.

Test

Check yourself

1. Where does the program start running?
2. What is the difference between print("a", end="") and print("a")?
3. What does the computer do with a comment?
4. Python reports a SyntaxError on line 3. What does that mean?
5. Which line will print Hello?

Made by Ivo Gergov as part of Evo Tech's campaign for accessible programming education ·