Two ways to run Python
Python on your computer can run 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 last lesson, and know which one to reach for. Knowing the difference also explains a confusing moment every beginner hits: code that "works" in one place and prints nothing in the other.
The interactive shell
The first way is the . You start it by typing Python's name in the terminal with nothing after it.
Windows
pymacOS
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")
SelamNotice that you didn't need print to see 7. The shell shows the value of whatever
you type. It even shows text with its quotes, so you can tell text from numbers. When
you're done, type exit() and press Enter to get back to the normal terminal.
The shell is perfect 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 you're typing on. The terminal's own prompt usually ends in >, $, or %,
often after the folder you're in, like PS C:\Users\Hana> on Windows or
hana@laptop ~ % on a Mac. Python's prompt is exactly three arrows, >>>, with nothing
before them.
Terminal commands such as python3 --version or cd only work at the terminal
prompt. Python code only works after >>>. If you get an error that makes no sense,
check the prompt first.
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 for yourself.
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 calculated 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.
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 with a single line like
print("Selam") and save it.
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 three lines 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. The code boxes on this site are a halfway point: you can edit and rerun them like a script, without saving anything.
Quick check
Selam wants to check what 1200 * 15 / 100 gives before putting it in a program. Where is the quickest place to try it?
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 interactive shell (REPL) runs one line at a time and shows each result; start it with
pyorpython3, leave withexit(). - 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, or with VS Code's Run button. - Use the shell to try things, and scripts for anything you want to keep.