CoCalc Public FilesATMS-305-Content / Week-6 / Week 6 Handouts / Basics.htmlOpen with one click!
Authors: Anna Nesbitt, Steve Nesbitt
Views : 47
Description: Jupyter html version of ATMS-305-Content/Week-6/Week 6 Handouts/Basics.ipynb
Compute Environment: Ubuntu 18.04 (Deprecated)
Basics

Names first

In [3]:
import numpy as np
import pandas as pd

What's a variable?

In [7]:
x = 'hello'
print('hello')
hello
In [13]:
x
Out[13]:
'hello'

How to print in Jupyter?

In [25]:
print(x)
x
hello
Out[25]:
'hello'

Basic types: String? Integer? Float?

In [2]:
astr = 'yellow'
another_str = "yellow"
aint = 4
another2_str = str(aint)
multiple_line_str = """
asgashdsj
sdajfdfsjfdjjsfjdsjfsdj
fsjfdsjsdfj
"""

afloat = 10.2
In [ ]:
a = 10.
b = '45'
c = 1337

Higher level types: Tuple? List? Dictionary?

In [19]:
atup = (1, 2)
alist = [5, 5, 6, 7]
adict = {'key1': [6, 3], 'key2': [5, 4]}
In [4]:
atup_of_lists = ()
alist_of_tups = []
adict_of_lists_and_tups = {}

How to check the type?

In [9]:
type(atup)
Out[9]:
tuple

Subscript/slice/subselect/reverse

In [28]:
atup_sub = atup[0]
alist_sub = alist[2:3]
adict_sub = adict['key1']

Other types: np.array?, pd.DataFrame?

In [20]:
nparr = np.array(alist)
pddf = pd.DataFrame(adict)

One big difference between np.array and lists

In [5]:
alist_aint = alist * aint
nparr_aint = nparr * aint
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-7a75f7858083> in <module>()
----> 1 alist_aint = alist * aint
      2 nparr_aint = nparr * aint

NameError: name 'alist' is not defined

Format a string!

In [4]:
afloat_fmt = '{0:0.2f}'.format(afloat)
aint_fmt = '{0:5d}'.format(aint)
astr_fmt = '{0}'.format(astr)
In [1]:
afloat_aint_astr_fmt = ''.format()

1. Sorting a dictionary, printing formatted values.

Write a python script to sort the annual-average global temperature anomaly values (contained in aravg.ann.land_ocean.90S.90N.v4.0.1.201612.asc in into a new dictionary in descending order.

The script should print the top 10 warmest years to a file called 'top10warmest.txt', including all of the following information:

The output should look like this:

Ranking      Year     Temperature Anomaly (degrees C)      Temperature Anomaly (degrees F)
1.           1745     10.0                                 28.0
2. (etc.)
  1. How many reports of snow (including the report SN, -SN, +SN) were there last year?
  2. What fraction of the reports had snow reported?
  3. What was the highest temperature last year, and when was it reported?
  4. What was the mean temperature of all of the reports last year?
In [ ]: