Course contentsLesson 2 of 13← Course overview
  1. 01Meet Python12 min
  2. 02Set up and run Python12 min
  3. 03Variables and data types15 min
  4. 04Strings and input15 min
  5. 05Decisions with conditionals15 min
  6. 06Repeat work with loops15 min
  7. 07Functions and return values18 min
  8. 08Work with lists16 min
  9. 09Dictionaries and tuples16 min
  10. 10Understand errors and exceptions14 min
  11. 11Read and write files16 min
  12. 12Modules and the standard library14 min
  13. 13Build a small data summary20 min

Python & data · Lesson 02

Set up and run Python

Edit lesson

Find a Python interpreter, run a file, and understand the difference between Python and your terminal.

12 minProgress saves on this device
In this lesson
  • Check whether Python is installed
  • Run a Python file from a terminal
  • Identify where Python code should be entered

Helpful background: A computer with a terminal and permission to install Python if needed.

Check your installation

Open a terminal and ask for the installed Python 3 version. On macOS and Linux, the command is commonly python3; on Windows, the Python launcher is commonly py. The exact command depends on your installation.

If neither command works, follow the official Python installation instructions for your operating system, then reopen the terminal.

Check Pythonshell
python3 --version
# On Windows, try: py --version

Write a program

Create a plain-text file named hello.py in a folder you can find. The .py extension identifies a Python source file. Add the following line and save the file.

hello.pypython
print("Hello from Python!")

Run the file

In your terminal, change to the folder containing hello.py, then run it with your Python command. The terminal command launches the interpreter; the contents of the file are Python code.

If the terminal cannot find the file, check your current directory and the file name. On Windows, use py hello.py if that is how your Python installation is configured.

Run the programshell
python3 hello.py
# On Windows, try: py hello.py

Use the interactive prompt

Running python3 without a file opens an interactive Python prompt. Enter a Python expression there and see the result immediately. Use exit() to leave it and return to the terminal.

Do not paste terminal commands such as python3 hello.py into the Python prompt; those commands belong in the terminal.

Knowledge check

Where do you enter the command to run hello.py?

Try first; feedback appears after you check.
Apply it

Your turn

Create a file that prints your name and run it from the terminal.

BUILT TOGETHER

Help make this explanation better.

Have a clearer explanation, a useful example, or a correction? Share it with the community.

Contributors

Be the first to add your name when you improve this lesson.

How to get credited
Previous lessonNext lesson