Python
The material here is based on the Chapter 3-5 in “A Hands-On Introduction to Using Python in the Atmospheric and Oceanic Sciences” by Johnny Wei-Bing Lin.
Python, like any other programming language, has variables and all the standard control structures. We will go though Python’s basic data and control structures that support procedural programming. Then we will explore how to use arrays, followed by file input and output.
A few list of changes in Python 3.x that differs from Python 2.x
- The
print
function syntax now looks like any other function, e.g.,print("Hello")
. - The division operator uses floating point division, even when both the numerator and denominator are integers.
- Using
raise
to raise an exception has a new syntax. - Dictionary methods
keys
andvalues
no longer return lists. Neither does range. To make lists out of the return values, use thelist
function. - Dictionaries no longer have the
has_key
method. Test instead forkey
indict
(wherekey
is the key of interest anddict
is your dictionary object). Note that if a value in your dictionary is the same askey
, the membership testing will not returnTrue
. - Relative imports (i.e., importing a module in the current directory) require the use of . in front of the module name.
- The version of NumPy generally installed with Python 3.x has a function
ndim
that gives the number of dimensions of an array, rather thanrank
.