Two ways to run Python
Python on your computer runs in two ways: as a live conversation, one line at a time, or as a saved program that runs from top to bottom. By the end of this lesson you'll use both, run the file you wrote in lesson 0.2, and know which one to reach for. The first half works in the browser; the second half needs Python installed.
The interactive shell
The first way is the . You start it by typing Python's name in the terminal with nothing after it.
Windows
pythonmacOS
python3Linux
python3The prompt changes to >>>. That's Python waiting for you. Type a line, press Enter,
and it answers straight away:
>>> 3 + 4
7
>>> "Selam"
'Selam'
>>> print("Selam")
SelamYou didn't need print to see 7. The shell shows the value of whatever you type,
and shows text with its quotes so you can tell text from numbers. Python 3.14's shell
also colors your code as you type. When you're done, type exit() and press Enter
to get back to the normal terminal.
The shell is good for quick questions: "what's 15% of 1,200?", "does this line work?". But nothing you type is saved. Close the window and it's gone.
Where am I: the terminal or Python?
The most common beginner mix-up is typing into the wrong place. Look at the start of
the line. The terminal's prompt usually ends in >, $, or %, often after a
folder name, like PS C:\Users\Hana> or hana@MacBook-Air ~ %. Python's prompt is
exactly three arrows, >>>, with nothing before them.
Quick check
Your screen shows >>> with nothing before it. Where are you?
Terminal commands only work at the terminal prompt. Python code only works after
>>>. Here's what happens when a terminal command goes to Python:
Programs saved in files
The second way is a
:
a file ending in .py that holds your whole program. Python runs it from the first
line to the last and then stops. Almost everything you build in this course will be
a script.
Scripts don't echo values the way the shell does. Only what you print appears. The
code boxes on this site run your code as a script, so you can see this here. Decide
first: what does this print?
What will this print?
Decide before you look. Guessing wrong is how this sticks.
3 + 4
print(3 + 4)Python prints
7
In a script, 3 + 4 is worked out and then thrown away, because nothing asked to show it. Only print puts things on the screen. Typed into the shell, the first line would have shown 7 as well.
Now run it, then add print(...) around the first line so both results appear.
3 + 4
print(3 + 4)In the editor, press Escape then Tab to move on.
The shell and the script are the same Python. The same line gives the same result; the only difference is that the shell shows every value, and a script shows only what you print.
Running a file
Open your python-course folder in VS Code. In lesson 0.2 you saved
setup_check.py there; if you didn't, create it now: File, then New File,
type print("Selam"), and save it as setup_check.py.
You can run it in two ways.
From the terminal
In VS Code, choose Terminal, then New Terminal. It opens inside your folder. Type the command for your system:
Windows
python setup_check.pymacOS
python3 setup_check.pyLinux
python3 setup_check.pyWith the Run button
With the file open, click the triangle Run Python File button at the top right of the editor. VS Code types a similar command into its terminal for you.
Either way, the terminal shows your program's output and returns to the prompt.
Which one to use
Use the shell to try one small thing and see the answer immediately. Use a script for anything you want to keep, run again, or build on. Most programmers keep both open: a script they're writing, and a shell for checking ideas.
A good rhythm while you learn: when you're unsure what a line does, try it in the shell or in a code box here. Once it does what you want, copy it into your script.
Exercise
Make a script show its work
This script calculates two things but shows nothing when you run it. Change it so it prints both results, one per line.
84
63Your code
12 * 7
100 - 37In the editor, press Escape then Tab to move on.
Show a solutionHide the solution
This is one way to solve it, not the only one. If yours prints the same thing, it works.
print(12 * 7)
print(100 - 37)Key takeaways
- The shell (REPL) runs one line at a time and shows each value; start it with
pythonorpython3, leave withexit(). >>>means you're in Python; a prompt ending in>,$, or%means you're in the terminal.- A script is a
.pyfile that runs from top to bottom; only what youprintappears. - Run a script with
python file.py(Windows) orpython3 file.py, from the folder it's in, or with VS Code's Run button. - Use the shell to try things, and scripts for anything you want to keep.