Python course · Module 1: Getting started · Lesson 3 of 63
Quick answer: To run a Python script, save your code in a file that ends in .py, open a terminal in the same folder, and type python3 hello.py (on Windows: python hello.py or py hello.py). To try one line at a time, type python3 alone to open the interactive prompt >>>, and type exit() to leave. In VS Code, install the Python extension and click the “Run Python File” play button.
In this lesson you will learn:
- how to use the interactive prompt (the REPL) and leave it
- how to write a
.pyscript and run it from the terminal - why the REPL shows results but a script does not
- how to run Python files in VS Code
- how to fix the most common “can’t open file” and “not recognized” problems
Before you start: finish Lesson 2, How to Install Python on Windows, macOS and Linux, so that python3 --version (or python --version on Windows) works in your terminal.
Table of Contents
Two ways to run Python code
There are two ways to give code to Python:
- Interactive mode (the REPL). You type one line, press Enter, and see the answer at once. Good for testing small ideas.
- Script mode. You write many lines in a
.pyfile and run the whole file. This is how real programs work.
You will use both in this course. All examples below were tested on Python 3.14.6 on Linux, where the command is python3. They work the same on newer 3.x versions such as Python 3.15.2, the newest release in November 2026; only the version line of the REPL welcome message is different. On Windows, type python or py instead of python3; everything else is the same.
The Python REPL: the >>> prompt
REPL means “read–eval–print loop”, as the Python glossary explains: Python reads your line, evaluates (runs) it, prints the result, and loops back to wait for the next line.
Open a terminal and type python3 with nothing after it:
Python 3.14.6 (main, Jul 18 2026, 17:01:31) [Clang 22.1.3 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 + 3
5
>>> print("Hello from the REPL")
Hello from the REPL
>>> name = "Ali"
>>> name
'Ali'
>>> exit()
Line by line:
- The first two lines are the welcome message. They show your Python version.
>>>is the prompt. It means “Python is waiting for you”. Do not type the>>>yourself.2 + 3is an expression, so the REPL prints the answer5withoutprint().name = "Ali"stores a value. It prints nothing, because storing is not an answer.- Typing
namealone shows the stored value. The quotes in'Ali'tell you it is text. exit()closes the REPL and takes you back to the terminal.
On your screen, the prompt and error messages may be in colour. Since Python 3.13 the REPL has colours and also understands exit without brackets (What’s New in Python 3.13). The Python tutorial gives two more ways to leave: Ctrl+D on macOS and Linux, or Ctrl+Z on Windows. quit() also works.
Code you type in the REPL is lost when you close it. To keep your work, use a script.
Write and run your first .py script
A script is a plain text file with Python code. Its name ends in .py.
Step 1: Create a folder and a file
Make a folder for this course, for example python-course in your Documents. In any code editor, create a file named hello.py in that folder with these two lines:
print("Hello, Python!")
print("This is my first script.")
Save the file.
Step 2: Open a terminal in that folder
Use cd (change directory) to move into the folder, for example cd Documents/python-course. Many editors can also open a terminal that starts in the right folder.
Step 3: Run it
$ python3 hello.py
Hello, Python!
This is my first script.
$ stands for your terminal prompt; do not type it. Python ran both lines from top to bottom and then stopped. You are back in the terminal, not in the REPL.
Growing the script
Let’s make the script a little more personal. Save this as greet.py:
name = "Ayesha"
lessons_done = 3
print("Hi", name)
print("Lessons done:", lessons_done)
$ python3 greet.py
Hi Ayesha
Lessons done: 3
print() puts a space between the items you separate with commas. Change the name or the number, save, and run again. This edit, save, run loop is how you will work for the rest of the course.
Why a script does not show results by itself
Here is a difference that confuses many beginners. In the REPL, 2 + 3 shows 5. In a script, it does not. Look at bare_expression.py:
2 + 3
print(2 + 3)
$ python3 bare_expression.py
5
Only one 5 appears. Line 1 calculated 5, but a script only shows what you ask it to show. The automatic echo is a REPL feature. In scripts, always use print() to see a result.
Mix both: run a script, then explore it
The -i option runs a script and then opens the REPL with everything the script created. The tutorial calls this running a script and then entering interactive mode.
$ python3 -i greet.py
Hi Ayesha
Lessons done: 3
>>> name
'Ayesha'
>>> lessons_done + 1
4
>>> exit()
This is useful when you want to check the values inside a script after it runs.
Run Python in VS Code
VS Code is a free code editor that many Python learners use. We could not run VS Code on our test machine, so the steps below follow the official Getting started with Python in VS Code tutorial:
- Install VS Code, then install the Python extension from Microsoft (Extensions view, search “Python”).
- Open your
python-coursefolder with File, then Open Folder. - Open the Command Palette (Ctrl+Shift+P, or Cmd+Shift+P on macOS) and run Python: Select Interpreter. Pick the Python version you installed in Lesson 2.
- Open
hello.pyand click the Run Python File play button in the top-right corner of the editor. The output appears in the terminal panel at the bottom. - To run only some lines, select them and press Shift+Enter (“Run Selection/Line in Python Terminal”).
The play button does the same thing as typing python3 hello.py yourself; VS Code just types the command for you.
If “python is not recognized” or “command not found”
This message means the terminal cannot find Python. Go back to Lesson 2 and check, in this order:
- Did you open a new terminal after installing?
- On Windows, does
py --versionwork? If yes, check “Manage app execution aliases” as described in the Windows guide. - On macOS and Linux, are you typing
python3(with the 3)?
Try it yourself
Exercise 1: Use the REPL as a calculator
Open the REPL and find out how many hours a year has (365 days times 24 hours). Then leave the REPL.
Exercise 2: Your own script
Create about_me.py that prints three lines: your name, your city and your goal for this course. Run it from the terminal.
Exercise 3: Fix the silent script
This script, minutes_broken.py, runs without errors but shows nothing. Fix it so it prints Minutes in 2 hours: 120.
60 * 2
Solutions
Solution 1
>>> 365 * 24
8760
>>> exit()
The REPL prints the answer of an expression straight away, without print().
Solution 2
print("Name: Bilal")
print("City: Lahore")
print("Goal: learn Python for data analysis")
$ python3 about_me.py
Name: Bilal
City: Lahore
Goal: learn Python for data analysis
Each print() call prints one line, in order.
Solution 3
print("Minutes in 2 hours:", 60 * 2)
$ python3 minutes_fixed.py
Minutes in 2 hours: 120
A script only shows what you pass to print(); the original line calculated 120 and threw it away.
Common mistakes
- Typing the run command inside the REPL. At
>>>,python3 hello.pygivesSyntaxError: invalid syntax. Typeexit()first, then run the command in the terminal. - Being in the wrong folder. Running
python3 hello.pyfrom the folder abovepython-coursegivespython3: can't open file '/home/learner/hello.py': [Errno 2] No such file or directory. Read the path in the message, thencdinto the folder that has your file. - Leaving out
.py.python3 hellogivespython3: can't open file '/home/learner/python-course/hello': [Errno 2] No such file or directory, even inside the right folder. Type the full file name. On Windows, also check that the file is not secretly namedhello.py.txt. - Expecting a script to echo results. A line like
60 * 2in a script shows nothing. Useprint(). - Running an old version of the file. If your change does not appear, you probably did not save. Save, then run again.
Frequently asked questions
How do I run a Python file from the command line?
Open a terminal in the folder that holds the file and type python3 filename.py on macOS or Linux, or python filename.py or py filename.py on Windows. Python runs the file from top to bottom and returns you to the terminal.
What is the difference between the Python shell and a script?
The shell (REPL) runs one line at a time and shows each result at once, but forgets everything when you close it. A script is a saved .py file that runs all its lines together and only shows what you print().
How do I exit Python in the terminal?
Type exit() or quit() and press Enter. In Python 3.13 and newer you can also type just exit. Keyboard shortcuts work too: Ctrl+D on macOS and Linux, Ctrl+Z on Windows.
Summary
- Type
python3(Windows:pythonorpy) alone to open the REPL;>>>means Python is waiting;exit()leaves. - Save code in a
.pyfile and run it withpython3 file.pyfrom the file’s folder. - The REPL echoes results automatically; scripts only show what you
print(). python3 -i file.pyruns a script and then opens the REPL with its values.- In VS Code, install the Python extension, select your interpreter, and press the Run Python File button.
Next lesson
Next: Lesson 4, Python Indentation and Syntax: Blocks, Comments and Common Errors.
Sources
- Using the Python Interpreter (Python tutorial)
- Python Glossary: REPL and interactive
- What’s New in Python 3.13: new interactive interpreter
- Getting started with Python in VS Code
- Using Python on Windows: troubleshooting (Python docs)





