Chapter 02

The terminal, folders, and your first project

Move around your folders from the terminal, create a project folder, run files, and learn when Python's interactive shell is the right tool.

25 minPython 3.12
  1. 1Encounter
  2. 2Understand
  3. 3Worked
  4. 4Predict
  5. 5Apply
  6. 6Stretch

The problem we are solving

In the last mission you installed Python and ran a file. But something happened quietly in the middle of it — before running the file, you had to go to its folder in the terminal.

That "going" is what traps beginners more than anything else. The code is fine, Python is fine, but the terminal is sitting in a different folder — and Python reports that the file does not exist.

At work you will do this a few hundred times a day. So today we turn the terminal from an unfamiliar black screen into a tool you know.

By the end of this mission you can

  • Tell which folder the terminal is currently in
  • Move from one folder to another, and step back up one level
  • Create a project folder and run a file inside it
  • Open and close Python's interactive shell (the REPL)
  • Explain how the REPL differs from a .py file — this one confusion wastes a lot of time

Prerequisites: Installing Python and running your first program.


What the terminal actually is

The terminal is a place where you instruct the computer by typing instead of clicking. And every terminal window is always "standing" inside some folder. That folder is called the current directory.

This is the whole idea. When you type python hello.py, what you are really saying is: "run the `hello.py` file in the folder I am standing in right now." If the file is in some other folder, Python will not find it — because it does not look anywhere else.

Open PowerShell and you will usually see something like:

text
PS C:\Users\Rafi>

The C:\Users\Rafi part is telling you where you are.

Four commands that cover the job

pwd — where am I?

text
pwd
text
Path
----
C:\Users\Rafi

dir — what is in here?

text
dir

It lists the folders and files. Running dir before you run a file is a good habit — it confirms the file you are about to run is actually there.

cd — go to another folder

text
cd D:\python-lab

Two dots step one level back up:

text
cd ..

To move to the D: drive in PowerShell, cd D:\ is enough.

Type less. Write the first two or three letters of a folder or file name and press Tab — the terminal fills in the rest. Hand-typing names is the biggest single source of typos, and Tab removes it.

mkdir — make a new folder

text
mkdir lesson-02

Set up a project folder

If you scatter your files across the Desktop while learning, you will not find anything a week later. So pick one place and give each mission its own folder:

text
cd D:\
mkdir python-lab
cd python-lab
mkdir lesson-02
cd lesson-02
pwd

That last command should answer:

text
Path
----
D:\python-lab\lesson-02

Now create a file called notes.py in this folder containing:

python
print("Folder:", "lesson-02")
print("Squares:", 2 * 2, 3 * 3, 4 * 4)

And run it:

text
python notes.py
text
Folder: lesson-02
Squares: 4 9 16

Python's interactive shell

Type just python in the terminal and press Enter, and Python itself opens up:

text
python
text
Python 3.12.0 (tags/v3.12.0) on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

That >>> means Python is waiting for a line from you. Whatever you write here is answered immediately:

text
>>> 2 + 3
5
>>> "Python"
'Python'
>>> len("terminal")
8

This is called the REPL — read, evaluate, print, loop. It is excellent for checking something small: whether a calculation is right, or what a function hands back.

To leave:

text
>>> exit()
The difference that costs people hours: in the REPL, typing 2 + 3 shows you 5 on its own. But put only 2 + 3 in a .py file and run it, and nothing appears — Python does the arithmetic, throws the result away, and stays silent. In a file you have to write print to see anything. That is the real difference between the REPL and a script.

When it breaks

python : The term 'python' is not recognized... Python is not on PATH. See the "When it breaks" section of the previous mission — re-run the installer and tick the box under Modify.

can't open file '...notes.py': [Errno 2] No such file or directory The terminal is in the wrong folder, or the filename has a typo. Run pwd to see where you are, then dir to see whether the file is there.

cd : Cannot find path 'D:\python-lab' because it does not exist The folder has not been created yet, or the name is wrong. Create it with mkdir, and press Tab next time you type the name.

The terminal seems stuck, showing only >>> You are inside the REPL, not outside it. dir and python notes.py will not work here — those are terminal commands, not Python. Type exit() to leave.

The folder name has spaces in it Quote the whole path: cd "D:\my python lab". Naming folders without spaces avoids the problem entirely, which is why python-lab and my_notes are better habits.