2.3. Python Syntax - Basics #3#

In this section we’ll look at a couple of different Python objects that are commonly used. We’ll cover a lists, tuples, and dictionaries.

Lists#

There are times when we want to be able to group together items and store them together. Python’s most versitile construct for holding multiple items together is known as a list. Lists are mutable, meaning you can change the value of a item within the list. Lists can contain a single type of item (e.g., integers, floats, strings) or a mixed grouping.

There are two ways to define a list, 1) using square brackets or 2) using the list function list(). For example the following two are equivalent,

a = [1, 2, 3, 4, 5]

b = list(1, 2.5, 'Kevin', 4, 5)

Just like strings, we can subset a list to get a particular element using index values (addresses) of the desired elements.

print(a[2])
print(b[:3])

Output

3
1, 2.5, Kevin

Lists vs. Tuple#

Python doesn’t have a true array object. The closest thing to an array is a Python List (which are sometimes called array-like objects). Many of their properties are the same, but there are some notable differences depending on what you are trying to do. There are two ways to define a list, 1) using square brackets or 2) using the list function list(). For example the following two are equivalent,

a = [1, 2, 3, 4, 5]

a = list(1, 2, 3, 4, 5)

Lists are mutable objects, meaning that you can redefine individual elements.

Another array-like object in Python is the Tuple. Tuples are different than lists in that they are immutable objects, meaning they can’t be changed once assigned. Tuples can also be defined in two different ways, 1) using parentheses or 2) using the tuple function tuple(). For example, the following two tuple assignments are equivalent,

b = (10, 20, 30, 40, 50)

b = tuple(10, 20, 30, 40, 50)
# A list
a = [1, 2, 3, 4, 5]
print(a)
[1, 2, 3, 4, 5]
# Try a reassignment of a list object
a[1] = 10
# A tuple
b = ('Kevin', 'Emily', 'Adam')
# Try a reassignment of a tuple object
b[1] = 'Matt'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[4], line 2
      1 # Try a reassignment of a tuple object
----> 2 b[1] = 'Matt'

TypeError: 'tuple' object does not support item assignment

Both lists and tuples can contain different data types in the same variable. This can be adventageous at times.

List and Tuple Methods#

List Object#

There are a number of methods that are available for list objects including: append(), copy(), count(), index().

Python Docs: More on Lists

# List append method
a.append(100)
print(a)
[1, 10, 3, 4, 5, 100]

Tuple Object#

There are only two methods available for the tuple object: count(), index()

Python Docs: Tuples and Sequences

# Tuple index method
indx = a.index(5)
print(indx)
4

Beware of Math with Lists and Tuples#

Math works VERY differently! A multiplication creates that number of copies within the list or tuple and concatenates them to the object.

# Multiple a list or tuple by 2
print(a*3)
[1, 10, 3, 4, 5, 100, 1, 10, 3, 4, 5, 100, 1, 10, 3, 4, 5, 100]

Dictionaries#

Python has the ability to create a mappable data structure, which is called a dictionary. The main way you’ll encounter them is through many different data read methods. The great thing about a dictionary is that you are able to reference data with a string (or key) as opposed to just an index value. This is very powerful for keeping track of a number of different arrays within a “dataset”.

A dictionary is defined in one of two ways, 1) with curly brackets or 2) the dict() function where you link a value (or list of values) to a key (name).

hours = list(range(24))
stations = ['VPZ', 'GYY', 'ORD', 'MDW', 'RFD']
data = {'times': hours, 'stn': stations}
print(data['stn'])

Output

['VPZ', 'GYY', 'ORD', 'MDW', 'RFD']

There are a number of important methods attached to a dictionary object including: clear(), copy(), keys(), and values()

Python Docs: Dictionaries

units = {'tmpf': 'F', 'dwpf': 'F', 'pressure': 'hPa'}
long_names = {'tmpf': 'Temperature', 'dwpf': 'Dewpoint', 'pressure': 'Pressure'}
print(long_names['tmpf'])
Temperature

Numpy Arrays#

A data object that is very import to scientists using computers is to have a true array, meaning one that we can do math on and have it automatically calculate element by element. Unfortunately, lists won’t work as we have seen, so we need to bring in a third-party package (module) that contains this type of data object. Through the course of the semester we will interact with many more data objects that will be useful for our work in Earth Systems Science.

The Numpy module is a powerful package that includes not only a true array object, but a number of important calculations that can operate on these arrays in a time efficient manner. We’ll dig deeper into all that Numpy as to offer in the next unit, but for now we want to introduce importing a third-party package and the development of a Numpy array.

Importing a Module#

There are many, many, many packages that have been developed to work off of the main Python syntax core, which is relatively small in and of itself. To gain access to those packages, they need to be imported to attach the methods contained within to your notebook/script. There are some common ways to import many packages and we’ll use the most common methods over the course of this semester.

The main function for bringing in a package is import. There are variations on how to bring in a module or just a particular function, which we’ll highlight as we go through the semester.

To import Numpy we’ll use the import function and then give it an alias, or shorthand name, that we’ll use to reference the particular components of that module. By doing so we’ll protect ourselves against having two functions with the same name with one silently “overwriting” the other.

import numpy as np

This is the most common way you’ll see the Numpy module imported into a notebook or script.

import numpy as np

Create simple array#

The function array can take a Python list that contains all of the same type (e.g., integers, floats, strings). To access the function we’ll need to use the alias that we created when importing the module np.

a_array = np.array([1, 2, 3, 4, 5])
print(a_array[4])
5

Now with this “true” array, we can do math and get the expected output with it operating on each element and not doing some concatenation, which is what we had with native Python lists.

print(a_array*3)
[ 3  6  9 12 15]