3.2. Your first Python program¶
Here is some Python code here
. You
should download this code and put it in a file called
hello.py. Throughout these notes you will be given
files to download. You should adopt the following practice.
Create a directory (folder) in which you place these files
and always start up python from the commandline after
connecting to that directory.
The file hello.py is a tiny Python program to check that Python is working. Try running this program from the command line like this:
gawron$ python hello.py
(the gawron$
is my commandline prompt; the rest
is what I typed on my keyboard, followed by <Enter>
).
The program should print:
Hello World
Or if you type:
gawron$ python hello.py Alice
it should print:
Hello Alice
Or if you type:
gawron$ python hello.py stupid
it should print:
Hello stupid
If you have a text editor that you feel comfortable with
(as discussed in Section Python: Workflow and learning strategy), try editing
the hello.py
file,
changing the ‘Hello’ to ‘Howdy’, and running it again.
Once you have that working, you’re ready for class – you can edit a Python file and run Python code; now you just need to learn Python!
3.2.1. A little more discussion¶
The following function is what the file contains:
import sys
def main():
"""
Get the name from the command line, using 'World' as a fallback.
"""
if len(sys.argv) >= 2:
name = sys.argv[1]
else:
name = 'World'
print('Hello', name)
Skipping line 1 for now, the block of Python code beginning on line 3 is a function definition. A function is a program, and the way you define a function in Python is to write:
def <function_name> ([Arguments]):
followed by an number of lines of indented legal Python
code. The arguments are values that are passed
into the function that may affect what it does. In
the case of main
, there are no arguments,
and so the arguments list is simply ()
.
To see how this function works in python, let’s run
the Python interactively, so that we end up
seeing the python prompt after the program is run. This
is done by including a -i
on the commandline as follows:
gawron$ python -i hello.py
We then get:
Hello World
>>>
We can run the function main
again from inside
Python, because the first thing Python did on startup was load
up all the definitions in the file hello.py
.
Then it executed main
.
We can tell it to execute main
again as follows:
>>> main()
Hello World
And it still works. In general in this course, we’ll be executing Python code interactively by typing directly to the Python prompt.
This simple function contains a lot of features of Python which we will be discussing in more detail in the next few sections. Just as a small preview, we discuss some of them now:
Line 1 is an
import
statement. Python comes with a small amount of built in functionality, but most of what you do when you program Python requires importing other files defining new functionality. These helper files are called modules, and any Python distribution, even the bare bones standard distribution, comes with a large number of them. The core set of modules that comes with a standard Python distribution is called the Python standard library. Thesys
module imported in line 1 is just one of the modules in the Python standard library. It provides access to some variables containing information gathered when python started up, including information about the machine Python is running on and how Python was started. One of these is the variablesys.argv
used in line 8.The variable
sys.argv
is a list containing any arguments you provided when you called Python from the commandline, including the name of the program you asked Python to run. So if you typed:python hello_world.py
then
sys.argv
looks like this:>>> sys.argv ['hello.py']
if you typed:
python hello_world.py Alice
then
sys.argv
looks like this:>>> sys.argv ['hello.py', 'Alice']
Line 8 of the program is a test to see if the
sys.argv
list is long enough to contain a name; its length is 1 if no name was supplied on the commandline and its length is 2 only if a name was supplied. If the test is passed (the length is 2), then the variablename
is set to the name supplied on the commandline (line 9). This is becausesys.argv[0]
gives us the first thing on the list andsys.argv[1]
gives us the second. Otherwise (else
), the name is set to be ‘World` (line 11).In line 12, the
print
function is called. This prints something to the screen. In this case, theprint
function prints “Hello” followed by a space (signalled by the comma) followed by the value ofname
.
Each of these points illustrates an important feature of Python. We’ll see them again in the upcoming sections.