print("This is a notebook cell speaking!")This is a notebook cell speaking!
Before you write any Python code, it helps to know that there are several different ways of actually running it. In this lesson, you will meet three of them. Each one suits a different situation — a bit like how you might send a text message for something quick, write a letter for something you want to keep, or put together a scrapbook when you want words and pictures together. By the end of this lesson, you will have tried all three for yourself.
Before you run any code, VS Code needs to know which folder on your computer you are working in. This is called your working directory.
(These instructions use Linux. If you are using Windows or a Mac, everything will look almost the same.)
scripts folder that came with your course materials.This matters because, by default, Python will look for any other files it needs inside this folder, and will save anything it creates back into this folder too. We will go into much more detail about exactly how this works later in the course — for now, just make sure this is the folder you have open.
In the last section, you pressed a play button to run a script, and a box appeared at the bottom of your screen asking you to type something in. That box has a name: it is called the terminal.
A terminal is simply a plain text window where you type instructions for your computer, instead of clicking on menus and icons with your mouse. It looks a little old-fashioned, but it is one of the most useful tools you will use as a programmer in particular because it is much quicker than using the mouse.
You can open a terminal in VS Code at any time by clicking Terminal → New Terminal at the top of the screen (or by pressing Ctrl +). Because you already opened the scripts folder, your terminal will start there too.
A script is simply a text file with your Python code saved inside it, ending in .py. Think of it as a recipe you have written down: you can follow it once, put it away, and come back to run it again whenever you like. This makes scripts the best choice when you want to save your work properly.
There are two equally valid ways to run a script:
Whichever way you choose, the output appears in the same place: the terminal window at the bottom of your screen. That is the default behaviour of a script — everything it produces shows up there.
Depending on how Python is set up on your computer, you might need to type python3 instead of python. If one does not work, try the other.
Note that on your own machines, you do not want to be running things on the default python that comes pre-installed. This is because you do not want to mess with the setup that many other software including your operating system might use. We will cover more about this later in the environments section.
If running your script is not working from the terminal check that you are in the correct working directory. You need to be in the correct directory to run a python script from the terminal. As we opened the scripts foldr in VSCode, it should automatically that as the working directory - e.g. where the expense_calculator.py is located
Example: Imagine you saved a file called hello.py containing one line of code:
If you ran this file — either by pressing Play, or by typing python hello.py into the terminal — this is what your terminal window might actually look like:
Notice that the message appears right below the command you typed. That is what “running” a script actually means: Python reads your file from top to bottom, carries out each instruction, and the terminal shows you the result.
Sometimes you do not want to save anything at all — you just want to try a quick idea and see what happens. That is exactly what the Python Console is for.
The console is sometimes called a REPL, which is a simple way of describing four things it does, one after another:
This instant back-and-forth makes the console brilliant for quick tests. The catch is that nothing you type is saved — as soon as you close it, it is gone, a bit like a chat message that disappears the moment you close the app.
Opening the console
python (or python3) and press Enter.>>>, waiting for you.exit() and press Enter to leave the console and return to your ordinary terminal.Example: Here is what it would actually look like if you typed two lines into the console, one after another, and then exited:
Notice how each answer appears immediately after you press Enter, and Python is straight back with >>>, ready for your next line. Once you type exit(), the >>> disappears and you are back in your ordinary terminal.
If you’re interested in using the terminal more, for example to navigate file systems and automate tasks - attend the training facilities unix/command line course
When you run a piece of code inside a Jupyter Notebook, the result appears directly underneath it, and both stay together, permanently, in the same file — a record of exactly what you wrote and what it produced. A Jupyter Notebook is a file ending in .ipynb that mixes code and writing together, split up into separate blocks called cells — a bit like a scrapbook, where some pages hold your working and other pages hold your notes.
Don’t worry about installing anything — the software and extensions you need have already been set up for you.
Two kinds of cell
Example: If you typed the following into a code cell and ran it:
This is exactly what you would see, with the code and its result kept together in one place:
Unlike the console, this stays exactly where it is when you save the file, so you (or anyone else) can come back later and see both the code and its result side by side.
Opening a notebook
Option A — Inside VS Code
notebook.ipynb).Option B — In your web browser
What is a kernel?
When you open a notebook, you will be asked to choose a kernel. In simple terms, the kernel loads the specific setup you need — including the correct version of Python — and keeps track of the current state of your work: it remembers every variable and every result you have created so far in that notebook. In the top-right corner of your notebook in VS Code, click Select Kernel and choose the course option provided.
Now it is your turn to try each way for yourself.
Create a new file in your scripts folder named hello.py. Inside it, write the line print("hello world"), save the file, then run it from the terminal by typing python hello.py.
What happens?
When you run hello.py, Python reads through the file from top to bottom and carries out the print("hello world") command. This displays the text:
hello world
directly in your terminal window, underneath the command you typed.
Open the Python console in VS Code. Type print("hello world").
What happens?
When the command print("hello world") is entered into the Python console and executed, Python immediately evaluates the statement and displays the text:
hello world
right away, with no file being saved anywhere.
Open a new Jupyter Notebook in VS Code and save it as practice.ipynb in your scripts folder. In the first code cell, type print("hello world"), then run it by clicking the Play button next to the cell (or pressing Shift + Enter).
What happens?
When you run the cell, Python evaluates print("hello world") and displays the text:
hello world
directly beneath the cell. Because it is a notebook, this result is saved alongside your code when you save the file, so it will still be there the next time you open it.
For each scenario below, decide whether Way 1 (Script), Way 2 (Console), or Way 3 (Notebook) would suit it best.
12 * 7 equals before moving on with your work..py files): Saved text files, run either by pressing Play in VS Code or by typing python script_name.py in the terminal. Best for programs you want to keep, reuse, or run automatically..ipynb files): Interactive pages mixing code cells and text cells, keeping code and results together permanently. Best for step-by-step work, projects, and notes.