Python & data · Lesson 02
Set up and run Python
Find a Python interpreter, run a file, and understand the difference between Python and your terminal.
- 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.
python3 --version
# On Windows, try: py --versionWrite 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.
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.
python3 hello.py
# On Windows, try: py hello.pyUse 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.
Where do you enter the command to run hello.py?
Your turn
Create a file that prints your name and run it from the terminal.
Create hello.py containing print("My name is Sam"), then run python3 hello.py or py hello.py.
BUILT TOGETHER
Help make this explanation better.
Have a clearer explanation, a useful example, or a correction? Share it with the community.
Be the first to add your name when you improve this lesson.
How to get credited