Week 1 · Python and your developer toolkit
The terminal
What the terminal is, the commands you'll use every day, and how to run a Python file.
The terminal is a text window where you control your computer by typing commands instead of clicking. Engineers use it because it's fast, it can be repeated and automated, and most developer tools, including Python, Git and every deployment tool you'll meet, are built to be used from it.
Open it now and follow along: Terminal on macOS, Ubuntu on Windows. Every command on this page works the same on both.
The prompt
When the terminal is ready for a command, it shows a prompt, ending in $ or %. You type a command after it and press Enter. In this course, code blocks show only the command, without the prompt:
whoami
That prints your username. If the terminal prints nothing after a command, that usually means it worked: no news is good news.
Where am I? Folders and paths
Your files live in folders (also called directories), inside other folders. The terminal is always "standing" in one folder, called the current directory.
| Command | What it does |
|---|---|
pwd | Print working directory: shows where you are |
ls | List the files and folders here |
ls -a | List everything, including hidden files that start with a dot |
cd projects | Change directory: move into the projects folder |
cd .. | Move up one level, to the parent folder |
cd ~ | Go to your home folder (~ is short for it) |
A path describes where a file is. ~/projects/fde-course/hello.py means: start in your home folder, go into projects, then fde-course, and find hello.py.
Creating and managing files
| Command | What it does |
|---|---|
mkdir projects | Make a new directory called projects |
mkdir -p projects/fde-course | Make a folder and any missing parent folders in one go |
touch notes.txt | Create an empty file |
cat notes.txt | Print a file's contents |
cp notes.txt backup.txt | Copy a file |
mv notes.txt ideas.txt | Move or rename a file |
rm ideas.txt | Remove (delete) a file |
Running Python from the terminal
There are two ways to run Python:
-
Interactively. Type
python3to start the interpreter, try lines one at a time, and typeexit()to leave. Good for quick experiments. -
As a script. Save your code in a file ending in
.pyand run the whole file:python3 hello.pyThis is how you'll run almost everything in the program.
To open the current folder in VS Code, run:
code .
The . means "this folder".
Time-savers
- Tab completes file and folder names. Type
cd projand press Tab. - ↑ brings back your previous commands, so you don't have to retype them.
- Ctrl C stops a program that's running or stuck. It's not copy in the terminal.
clearclears the screen.
Reading error messages
When something goes wrong, the terminal tells you. Read the message before doing anything else: it's usually telling you exactly what's wrong.
| Message | What it means |
|---|---|
command not found | You mistyped the command, or the tool isn't installed |
No such file or directory | The file or folder isn't where you said. Check with pwd and ls |
Permission denied | You're not allowed to change that file or folder |
Finished this lesson?