Lesson 3
18 min
Interactive mode, .py scripts, print() and comments: run your first code.
Definition
A Python script is a text file with the.pyextension, containing a series of instructions that Python runs from top to bottom, one line after another. Yourfirst program is the smallest possible script: it uses theprint()function to display a message on the screen.
There are two ways to run Python code: in interactive mode (you type an instruction and it runs immediately) or as ascript (you write your code in a.pyfile, then run it). Interactive mode is for quick testing; scripts are for building real programs.
A good habit is to use interactive mode to check an idea in a few seconds (“what does this calculation return?”), then copy what works into a script that you can keep, edit and rerun as many times as you like.
Simply typepythonin a terminal. The>>>symbols indicate that Python is waiting for an instruction. Typeexit()to quit.
>>> 2 + 3
5
>>> print("Hello")
HelloNotice the difference: in interactive mode, Python automatically shows the result of an expression like2 + 3, whereasprint()is what you use to deliberately display text. In a script, nothing is displayed on its own: you will always have to go throughprint().
Create a file namedhello.pyand write the following code in it. Theprint()function displays text on the screen.
print("Hello, world!")
print("I'm discovering Python")To run it, open a terminal in the file’s folder and type:
python hello.pyYou should see both messages appear, one below the other. In VS Code, you can also click the “Run Python File” button at the top right of the editor: it runs essentially the same command for you. On macOS and Linux, remember to writepython3 hello.pyifpythonisn’t recognized.

The text to display goes between quotation marks, double or single: both work, as long as you use the same type at the start and at the end. Numbers, on the other hand, are written without quotation marks. You can also giveprint()several values by separating them with commas: Python displays them on the same line, separated by a space.
print("Double quotes")
print('Single quotes')
print(42)
print("I'm", 20, "years old")
print("First line\nSecond line")The \nsymbol placed inside a text creates a line break. Each call toprint()also ends with a line break of its own, which is why the messages appear one below the other.
A comment is a note meant for humans: Python ignores it completely. It starts with the#symbol. Comments are used to explain why you’re doing something, to find your way around your code a few weeks later, or to temporarily disable a line.
# This is a comment, Python ignores it
print("Hi") # A comment can also follow an instruction
# print("This line is disabled")Printandprintare two different things.print("text"), neverprint "text".You will inevitably run into errors, and that’s perfectly normal. Here is what is displayed if you ask Python to display something it doesn’t know yet:
print(first_name)
# Traceback (most recent call last):
# File "error.py", line 1, in <module>
# print(first_name)
# NameError: name 'first_name' is not definedThe last line gives the type of error (NameError) and its cause: the namefirst_namedoesn’t exist. The lines above indicate the file and the number of the faulty line. The most common errors among beginners are a forgotten quotation mark, an unclosed parenthesis, or a typo in a function name.

Good to know
When Python runs into an error, it displays a message called a “traceback”. Don’t panic: read the last line, which indicates the type of error, and the line number tells you where to look. An experienced developer reads error messages every day: they are the best tool for making progress.
Exercise
introduction.pythat displays three lines: your first name, your city, and the reason you want to learn Python. Add a comment above each line. Then deliberately delete a closing quotation mark, rerun the script, read the error message you get, and fix it.