Getting Started With Python - eecse4750/e4750_2024Fall_students_repo GitHub Wiki

Getting Started With Python

Introduction

Python is a modern object-oriented programming language used in many applications. Apart from being relatively easy to learn, it can be used to "glue together" software modules written in other programming languages such as C and C++. We will be using Python together with OpenCL to reduce the amount of C code you will need to write and enable you to take advantage of the many powerful libraries that have been written for Python.

Note that there are currently two major versions of the Python language; we will be using Python 2.

Getting Started

Since Python is an interpreted rather than a compiled language, you need to invoke the Python interpreter to run a Python program. For example, if I save the following text in a file called myprogram.py.

for i in range(3):
    print 'an integer: %i' % i

I can run it from the command line on tesseract using the following:

python myprogram.py

This will print

an integer: 0
an integer: 1
an integer: 2

You can also start the Python interpreter and run code directly. Here is what an interactive Python session looks like:

Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> for i in range(3):
...     print 'an integer: %i' % i
...
an integer: 0
an integer: 1
an integer: 2
>>>

An important point to note is that indentation in Python serves to delineate blocks (other languages such as C use symbols such as braces).

There are a wide range of Python packages available for scientific computing, graphics, parallel programming, etc. We have installed those packages that you need in /opt/PYTHON; they will be automatically available when you run the interpreter via the path /opt/PYTHON/bin/python (which is the default if you just type python at the command line).

There are an enormous number of online resources available for Python programming. A good first place to look is the Python documentation site. A great place to search for answers to programming questions in Python (and other languages) is Stackoverflow.

Useful Tools

You can either write your programs on your PC and upload them to tesseract, or write them directly on tesseract using one of the various text editors available. Two relatively easy text editors you may wish to try on tesseract are Nano and Jed. Emacs and Vim both require a bit more effort to learn how to use, but they have lots of powerful features.

Although one can use the Python interpreter directly while testing out your code, you are encouraged to try IPython a powerful wrapper for Python that provides command history, debugging features, and the ability to access embedded Python library documentation.

Recommended Resource:

https://learnpythonthehardway.org/book/