In the last section, we used Python in an interactive mode. While this is useful for some purposes, many times you'll want to write and save a long Python program for running multiple times. (And if you are doing interactive work, you will probably want to use the ipython interpreter instead.) Since Python is interpreted like Perl or the UNIX command shell,, saved programs are often referred to as “scripts”; their format is similar to other kinds of scripts.

In order to write a Python program, you'll need to be able to use a text editor to write and save your code. If you are logged into a resource running UNIX/Linux, and you are not familiar with the use of text editors (or shell scripts), visit the Introduction to UNIX /Linux Cornell Virtual Workshop topic at this site and read those sections before continuing on in this tutorial. Alternatively, if you are working on your own machine, you may want to try out a special-purpose Python editor or perhaps even an Integrated Development Environment (IDE) that bundles together an editor with an interpreter. Good, flexible examples which run on Windows, OSX and Linux are Visual Studio Code (with Python extensions), and PyCharm.

However, let's assume you are already familiar with editing. If you are on the Frontera system at TACC, and you want to run the environment module to access the default version of Python 3.x available on Frontera, execute this code:

Now we're ready to get you started on your first Python program. To allow direct invocation from the command line on any UNIX/Linux system—so you can just type ./scriptname.py and have the script run—the shell needs to be told exactly how to run the script. This is achieved with the shebang line, which the python interpreter will just consider to be a comment and ignore. Here's what that line looks like in your editor:

If the desired executable is simply named "python" instead, the first line would read: . After that initial line, you simply insert the Python statements that you want to be executed. A statement is essentially a single line of code containing a keyword or other indication of what the line is supposed to do. Let's use the same set of statements from the previous section. Open a file, insert the following text, and save it under the name "TryMe.py" (using a ".py" extension is standard for Python scripts).

To run the script without the shebang, we'd first have to invoke the Python interpreter:

In the script, any text appearing on a line after the # symbol will be treated as a comment; it will not be executed. Strictly speaking, Python does not provide a separate mechanism for creating multiline block comments, other than by having multiple lines beginning with the # symbol. (Some programmers create multiline comments by enclosing them in pairs of triple quotes; this essentially creates a multiline string object that never gets assigned to anything and eventually disappears from scope. But depending on where such a string is located, it might be interpreted as a documentation string — or docstring — which could be visible through the help() system.)

Once you have saved this file, if you wish to invoke the script directly in the shell using the shebang mechanism instead of passing it to the interpreter, you'll need to change the permissions on the file to make it executable. On Linux, UNIX, or Mac systems, you do this with the command:

Now, you are ready to run your script. Simply type:

You should see "hello, world" followed on the next line by the number 7. If you did, you have now run your first Python script.

Now, let's get started on some of the elements of Python syntax, so you can compose some more complicated scripts. You can start up the Python interpreter in a terminal window again, to try things out as we go.

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