|
First steps executing Python programs interactively
If you use Spyder (that is the default assumption), then this is a
good time to find and start Spyder.
Get the hello world file into the Spyder editor window by either
- Download hellotest.py and save as
hellotest.py. (You download the file by right-clicking on the
link in your browser, then select Save Target As or Save
Download as), and then
- Open the file hellotest.py via the File menu, then select
Open. We express this as File -> Open in short.
or
- click on hellotest.py to see the source code in
the webbrowser, then copy the whole code
- navigate to the editor window in spyder and paste the code. Then
save the file as hellotest.py.
To execute the program, select Run -> Run (or press F5), and
confirm the Run settings if required.
You should see output like (the paths may differ):
In [1]: runfile('/Users/fangohr/Desktop/hellotest.py', wdir=r'/Users/fangohr/Desktop')
Hello World
In [2]:
If so, then you have just run your first Python program -- well done.
Before we proceed, please
Once you have executed the hellotest.py program the function object hello is defined and known at the Python prompt. We can thus call the function from the Python prompt:
Call the hello() function from the Python prompt, i.e. type
hello() in the Python Shell window (the Python prompt shows as
>>>, or as In [?] if we use the IPython session where the
question mark can be any positive integer number.).
You should find that you the hello() function is executed again,
i.e. Hello World is printed again. Your function call at the
Python prompt together with the output should look like this:
In [3]: hello()
Hello World
Can you see how this differs from executing the whole program again?
When we execute the whole program (by pressing F5), Python goes
through the file, creates the hello function object (overriding
the previous object), reaches the main program and calls the
function.
When we call hello() from the Python prompt, we only call the
(already existing) function objects hello.
This will become clearer over time and also when we work with
slightly larger examples. You may want to return to this tutorial at
a later stage.
Python provides a function that displays all known objects (in the
current name space). It is called dir(): when you type dir()
at the prompt, you get a list of known objects. Ignore everything
starting with an underscore for now. Can you see hello in the
list?
Once an object is visible in the current name space (as is hello
in this example), we can use the help function as follows to
learn about it: Type help(hello) at the Python prompt.
Python should display information about the hello object. Where
does Python take the information from?
The Spyder environment also provides a panel in the top right corner
(by default) which is the Object inspector. If you type
hello into the empty line in the Object inspector window, it
will also provide the help string.
In the Editor window, change the function hello so that it prints Good Bye World rather than Hello World.
Press F5 (to execute the whole program) and check that the output of the program is now:
Good Bye World
What has happened when you pressed F5 is this: Python has gone through the hellotest.py file and created a new function object hello (overriding the function object hello we had defined before) and then executed the function.
We need to start with a clearly defined state. To do this, please change the function hello() back so that it prints Hello World (i.e. use the original hellotest.py file), then press F5 to run the whole program and check that it prints Hello World.
Call the function hello() from the command prompt (as described in Call existing function objects from the command line). You should see Hello World printed.
Now change the function definition so that it would print Laters World, and save the file (but do NOT execute the program, i.e. do NOT press F5 yet).
Call the function hello() from the command prompt again. You
should find that the text printed reads Hello World, like here
In [8]: hello()
Hello
Why is this so? Because the hello function object in the Python interpreter is the old one which prints Hello World. So far, we have changed the file hellotest.py (and replaced Hello World in there with Laters World) but this has not affected the objects that have previously been created in the Python interpreter.
Here are two possibilities to use our modified version of the hello function:
Option 1: execute the whole file hellotest.py again by pressing F5: this creates a new function object hello (and overrides the old one). You should find that if you press F5, and then call hello() at the prompt, the new text Laters World is printed.
Option 2: select the region you have changed (in this case the whole
function hello, starting from the line def hello(): down to
return None, and then select Run -> Run selection).
This will update the hello object in the interpreter without
having to execute the whole hellotest.py file:
In [9]: def hello():
...: """A friendly function printing 'Hello World'
...: Hans Fangohr, for teaching Python, 15 Sept 2013 <fangohr@soton.ac.uk>
...: """
...: print("Laters world")
...: return None
...:
If we now type hello(), we see the update response:
In [10]: hello()
Laters world
The ability to execute parts of the code to update some objects in
the interpreter (in the example above, we updated the function object
hello), is of great use when developing and debugging more complex codes.
|