Python Programming Language
Python is a widely-used programming language in many application areas, including scientific computing.
- High-level, modular
- Interpreted or compiled
- Interactive or scripted
- Free, open source software
- Multi-platform (Linux, Unix, OS X, Windows, etc.)
- Multi-paradigm (procedural, object-oriented, functional, etc.)
- Many specialized libraries available
Python has a lot of similarity with other languages. Areas that differ from some other languages include:
- Aims for clear, simple syntax — generally easy to understand, although indentation to delimit blocks can take some getting used to
- Dynamic typing
- Garbage collection (automatic memory management)
- Expressions and statements are distinct
- Performance: may be slower than similar programs in C, C++ or Fortran, although compiled version will be faster than interpreted version
- Modules written in other languages, such as C or Fortran, can be used in a Python program. Prior to specialized modules for parallelism being available, using C or Fortran modules with parallelism was used for parallel programming in Python. There are now a number of different Python modules for parallel and distributed computing.
- There are significant differences between Python 2 and Python 3.
Python for scientific computing
The ability to use object-oriented design for the controller, together with low-level libraries in familiar HPC languages (such as C and Fortran), using modular design, make Python a powerful scientific computing platform.
- Has data types such as arrays and matrices
- Many useful libraries have been contributed by scientists/engineers/application specialists, and the design of Python makes it easy to use them in other programs
- Particularly useful general-purpose libraries include:
- NumPy: multi-dimensional arrays, fast array/matrix operations (for processing all elements, and for linear algebra)
- SciPy: additional mathematical functionality including linear algebra, calculus, FFT, signal and image processing, etc.
- matplotlib: a highly customizable 2D plotting library which also works well with NumPy and SciPy
The above three packages are all installed and are usable under the gcc/7.3.0 module for Teton (modules py-matplotlib/2.2.2-py27, py-numpy/1.14.3-py27, py-scipy/1.1.0-py27). Together they provide functionality similar to Matlab.
- Python comes pre-installed on many systems
- Can have more than one version of Python on the same system (e.g. Python 2 and Python 3)
- Anaconda is a Python platform recommended for scientific computing:
- includes the package manager conda
- can download from www.anaconda.com/download
- can install the version of Anaconda for Python 3, and use it to make a Python 2 environment if needed (see environments)
- Miniconda version of Anaconda can also be used to start with a minimal set of installed packages, and then install only the required ones (to save space)
- Python scripts usually have a .py extension
- The Python program (interpreter) to run a script can be specified in one of two ways:
- in the script itself (with a line like "#!/usr/bin/env python" or "#!/usr/bin/python" at the top), or
- on the command line when the script is run
For the examples below, the script is saved as the file hello.py (and must be executable for the second column):
|Specify python to run||Specify python in script|
print "Hello world!"
#!/usr/bin/python2 print "Hello world!"
#!/usr/bin/python3 print("Hello world!")
|To run:||/usr/bin/python ./hello.py||./hello.py|
- If there are multiple versions of Python installed on a system, the method of including the python command in the script can be used to ensure that the correct version will be used.