Python is mainly used as a scripting language, but there are two main ways to use Python. You can either execute scripts or use the interactive interpreter.
You should see something like the screenshot above. From this prompt, you may enter any Python command.
Most python scripts begin with the shebang: #!/usr/bin/env python
This is not necessary for running Python scripts, but is good practice, as it is required to run your program as an executable script.
To run a Python script, simply type: python script.py
(If the script is made executable, and the shebang is added to the top, you can simply type script.py)
Some basic syntax
Above: a quick example of how to define a variable, do basic math, create an array, define a string, and some basic if logic within a for loop.
Indentation is very important! All loops and if-statements must be properly indented. This is good programming practice for all languages, but is required for Python. Also, Python is case-sensitive. Python =/= python, so you can have variables, functions and modules named both Foo and foo in the same program.
The most basic functions are loaded by default, but for most of your programs, you will need to use Python modules.
Some of the most common modules are:
numpy - for computation and advanced math
matplotlib - for plotting and data visualization
netcdf4 - for reading / writing netCDF data
datetime - time manipulation
Depending on usage, there are three different ways to import modules.
Import module - allows you to call functions in the module
from module import function - allows you to call this function without addressing the module first
from module import * - this imports every function from this module
Note if you just import the module and not any functions, you must call the function in the format: module.function()
Unlike Matlab and Fortran, Python starts its indexing at 0 rather than 1.
So, in the list a=[1,2,3,4,5] , a is 2 , whereas a is 1.