In this video I'm going to show you how you can run Python scripts in JupyterLab
using the text editor and the terminal.
First make sure you have set up JupyterLab as I showed in my last video
(linked in the description).
We have three panes on the screen here.
On the right is the terminal on the left is a text editor and on the far left is the file browser.
We will use the text editor to write our Python code and we will use the terminal to run our code.
The first thing to do is to give your Python script a name.
Commonly this text file is called "untitled.txt".
The easiest way to rename a file is to right-click
on it in the file browser on the left hand side and select rename.
We will call our script "script.py".
Make sure you have removed the "txt" extension. Python
scripts should always have the "py" extension.
If I press Enter you should see the name change in
both the file browser as well as on the title of the tab.
Now we can write our first Python script. In the text editor write the following:
The word "print" (all lower-case) followed by an opening and a closing round bracket.
Between the round brackets type a pair of double quotes.
Between the double quotes type the word "hello".
After the closing double quote type a comma and then another pair of double quotes.
Inside this second pair type "Python".
We've just written our first Python script.
Take a moment now to check that what you have typed looks exactly the same as what I have here.
The colouring of the text doesn't matter but the exact characters and their order does.
Also make sure that you have upper and lower-case letters in the same places as I do.
Before we can run the code we need to manually and explicitly save the file.
We can see that the file is currently not saved,
as there was a small black circle next to the name in the tab at the top.
To save the file you can either go to the file menu and choose "Save Python File"
or use the keyboard shortcut which is "ctrl-s" on Linux and Windows and "⌘-s" on macOS.
When the file is saved you will see that the black circle has turned into a cross.
Always make sure you save the file every time before running the code.
That is now a valid Python script so we can go ahead and run it.
Go to the terminal pane on the right hand side, this is where we will run our code.
Depending for example on which operating system you're on
you may see something slightly different in this pane but it should work in much the same way.
The first thing we need to work out is which command your computer uses for running Python 3.
The first thing to try is type the word "python", followed by a space,
followed by the name of the Python script we wrote, which in this case is a "script.py".
Press Enter and it should run the script.
You will see the output printed just below where you are typing
and it should say "Hello Python" and nothing else on that line.
If that works you're ready to go.
If that either errors or if it printed brackets around the output,
then you will need to try the next method.
This time type "python3" all as one word without any spaces inside that,
followed by a space and again followed by "script.py".
Press Enter and this time you should see the correct message printed.
You'll see on my computer both of those commands work in the same way as each other.
On your computer one may work or the other one may work.
Throughout this course you should use whichever
method you found here that worked for your computer.
If both of these methods either give you an error or an incorrect output
then make sure you turn up to the class a little early and ask for help.
Hopefully you're all now ready to get started on the course.