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
printfunction 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
raiseto raise an exception has a new syntax. - Dictionary methods
keysandvaluesno longer return lists. Neither does range. To make lists out of the return values, use thelistfunction. - Dictionaries no longer have the
has_keymethod. Test instead forkeyindict(wherekeyis the key of interest anddictis 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
ndimthat gives the number of dimensions of an array, rather thanrank.