As an interpreted language, Python can be used in a couple different modes. You can type statements directly into a python interpreter, an ipython interpreter, or a Jupyter notebook, and they will be executed immediately. Alternatively, you can store a set of statements (a program) in a file and run it all at once. If you have used MATLAB or R (or you are old enough to remember BASIC interpreters), this will feel familiar. One of the advantages of an interpreted language is that you can experiment with the language by simply firing up the interpreter, typing statements or expressions, and viewing the output.

Give this a try now. First, get to a machine with Python. Either log in to your favorite UNIX/Linux based machine, or run Python on your local system. On ACCESS resources, you can typically configure access to different software packages through the module shell command. Practically every Linux or Mac system will already have Python installed, although the pre-installed version may be running an older version like 2.7.5 or even a 2.6.x variety. On ACCESS resources, you could try executing the command module load python3 to gain access to a 3.x version. If you are running on Frontera at TACC, look here for additional information about Python installations on Frontera. If you either do not have Python installed on your system, or would like a more up-to-date and fully configured version that you can use for your research, see this material on Python Installations and Distributions. There are several "batteries included" Python distributions freely available for download; these provide a rich suite of installed libraries as well as convenient tools for installation of additional packages.

Start the interpreter by typing python on the command line of your Linux, UNIX or Mac based system, or by running the python executable in Windows. (Or, if you either installed or loaded a Python 3.x distribution, you might try typing python3.) When you start up the interpreter, it should tell you what version of the language you have, as in this example which is running version 2.7.10.

Or perhaps it looks something like this if you started up python3:

The interpreter has an input prompt (usually >>>, which we will to use indicate console input you can enter for yourself); this indicates it is waiting to process your instructions. Try typing a few statements, such as the example text below appearing to the right of the input prompt >>> . If there is an output associated with that input statement, it will be printed on the line below without a prompt:

>> x = 2+3
>>> print(x)
5
>>> print('hello, world')
hello, world-->

Congratulations, you have now become a Python programmer! You have accessed some built-in data types (integers such as 2 and 3, and a string, conveying the message 'hello, world'); you have also called the built-in function print. (In Python 3, print is a function, which is called with arguments to be printed. In Python 2.6 and 2.7, print is a keyword that is followed by objects to be printed, without the need for parentheses to indicate a function call, although if enclosing parentheses are present, they are treated just the same as whitespace.)

In some versions (as in the examples above), when you start up the python interpreter, it suggests you might try the help function. This function is a great way to explore the options in the language. You can explore interactively what is available in help by simply typing help().

The interactive help will point you to the official online tutorial; the drop down menu near the top of that page allows selection of different Python versions for the tutorial, so check which version is targeted. The tutorial is an excellent reference, providing comprehensive syntax documentation that goes well beyond what is available in this online workshop.

The help system will also provide you with a list of available modules. Much of the utility of the Python language comes from building on the extensive library of existing modules. Do you want to calculate some trigonometric functions? These are provided by the math module — run help() and type math to read up on the available functions provided by this module.

But before we get into more complicated things, let's spend a little more time on the basics. For example, how do you get out of your interactive python session? You can do it at any time by typing quit() or exit(), or by holding down the control key and hitting "d", ctrl-d. Now would be a good time to try this, because next we want to try running a Python script.

 
©  |   Cornell University    |   Center for Advanced Computing    |   Copyright Statement    |   Inclusivity Statement