Python: Creating a Script

Tutorial Video

Console

So far all the commands we have been written in the console and executed as soon as they were typed. The results of some of the commands have been printed to the console using the print command.

Variable Editor

Items that were assigned to a Variable Name are displayed in the Variable Editor.

Restarting the Kernal

We can restart the Kernal by going to, Consoles and then Restart Kernal:

We will be asked for a confirmation.

Restarting the Kernal Clears the Console and removes all the variables in variable explorer. It also closes any external Python Libraries (which will be discussed later). Closing Spyder and relaunching it or restarting your computer gives the same result. In essence doing so will remove all your previous work if you have typed in all your code using the console exclusively. To get around this we use a script file.

Script File

In the script file you can type in a block of code. By default the blank script looks like the following.

Comments

Any line beginning with

Python

Is treated as a comment. These are here for the user to read and not executed.

The lines which have:

Python

Correspond to a block of comments. Anything within these two lines e.g. line 2 is a comment. These are once again here for the user to read and not executed. Note also that the lines are numbered.

The shortcut key [Ctrl] + [ 1 ] can be used to toggle the selected line your mouse cursor is on to a comment and vice versa.

Saving a Script

A script file can be saved using file ↓ save as…

You can then select your folder to save the script file to. By default it will take you

HTML

It is however advised to save your script file somewhere in your Documents Folder.

For instance in this subfolder Python Scripts:

You can now name your file:

When naming the file, it is recommended to follow the conventions of variable naming, i.e. use lower case characters with a _ instead of a space and do not start the file name with a number:

Select save.

Running a Script File

You can press the Run Button to run the currently selected script file. It has the shortcut key [ F5 ]:

Note if you have not saved your script file already it will open up the save as… dialogue box when running your script for the first time. Once the script has a file name, it will automatically save when running it.

If this is your first time using the Run button, the following dialogue box will show. Select run:

In this case the script exists entirely of comments so as far as Python is concerned it is just a blank file. It runs this blank file but nothing happens as there is no code for it to execute.

Creating Variables

Let’s add the following variables.

Note after modifying this script file, the tab with the filename of the script ends with a * this denotes that the changes haven’t been saved.

We can now run the code:

Note that the file no longer has a * meaning it was saved and has no new changes. The three variables have been created and show in the variable editor but do not display in the console.

The Print Command

If you want variables to be displayed on the console, you must use the print command.

When this file is ran, the variables are printed as specified:

One should be careful when using other functions, to ensure that they set the output to a variable name or they print them. For example we can use the function type and look at the type of a. When this code is executed the type of a is calculated but is neither stored as a variable nor printed to the console.

Enclosing this in the parenthesis of the print command displays the result in the console when the script is ran:

This can be done for all the variables

Deleting Code vs Commenting Code Out

Code can be selected and deleted.

However in many cases you may want to keep the specific code to wither modify it later or to not run it at a time. For instance if your script doesn’t work at a certain point you may want to comment out a block of code at the bottom and check if the top part works.

This can be done using the menu by selecting Edit and then Comment/Uncomment or by using the shortcut key [Ctrl] + [ 1 ]

You’ll note each line begins with a:

Python

As you see the text is now grey when unselected denoting it is commented out.

Restarting the Kernal

Note that when this script is ran, the variables b and c are still present from the last instances.

To get around this, you’ll want to Restart the Kernal. By going to console ↓ restart kernal or using the shortcut key [Ctrl] + [ . ]

Select Yes:

The variable editor is now blank and when the script is ran only the variable a will be created.

It is often worthwhile restarting the kernal and then launching your script to make sure it launches. This prevents you from accidentally typing only a command in the console and not incorporating it into your script which may have dependencies on that command. Assuming your script contains print(a) and you type a=1 into the console and then run your script. Then the script will run because a has already been assigned in the console but if you close down Spyder and open it up again just to run your script with the single print command, it will fail because a will no longer be assigned.

Variable Reassignment

You will only see the end result of a variable unless you print it. For instance if we have the following code:

Python

We will only see the variable a=3 the final value. We will not see the interim values a=1 and a=2.

We can however see these if we use the print command multiple times.

Python

We can also look at a and the type of its a after multiple reassignments.

Python

Here a starts of as a boolian value of True, when this is added to a number it is taken as an integer equal in value to 1. In this case 1 plus 1 is the integer 2. Finally it is added to 1.0 which has a decimal point and is thus a float. Addition of a float 1.0 to an integer 2 gives a float 3.0.

Advertisements