x_int = 20 #this is an integer x_float = 20.0 #this is a floating point number x = 5 y = 3 z = x/y print z z = 10/3. print z from __future__ import division print 5/2 string1 = 'datatypes' string2 = "Don't stop believin'" print string1 print string2 print string1[0] #first character print string1[0:2] #first two characters print string1[2:len(string1)] #everything after second character print string1[2:5] print string1[-2:] #last two characters print string1[0:len(string1)] print string1[:] def my_function(): """ This a simple function to demonstrate the use of triple quotes. You would normally put some a brief description of your function in this space.""" pass #Tells interpreter: nothing to see here, carry on. a = 'asasa' #a[2] = 'd' #throws error num_list = [2, 3, 210, 4] str_list = ['a', 'sdff', 'asd'] rand_list = [[12, 10], 'a', 1, [10, 'x', 12]] print "num_list: %s" %num_list print "str_list: %s" %str_list print "rand_list: %s" %rand_list print rand_list[0][1] #generate a list that contains the square of each number in num_list: num_list_sq = [num**2 for num in num_list ] print num_list_sq #generate a list that contains the square of each number in num_list #if the number is exactly divisible by 3 num_list_sq2 = [num**2 if num%3==0 else num for num in num_list ] print num_list_sq2 num_list_sq3 = [] for num in num_list: if num%3==0: num_list_sq3.append(num**2) else: num_list_sq3.append(num) print num_list_sq3 yr_strs = ('year1', 'year2', 'year3') yr_nums = 2009, 2010, 2011 tupl = ('a',) tup_num = tuple(num_list) print yr_nums print tup_num print "%s: %s" %(yr_strs[0], yr_nums[1]) contacts = {'John': 6462100, 'Adam': 6461870} print contacts mooring1 = {'Date': '2010-09-13', 'Lat': 10.1,'Lon': 78.5, 'salinity': [28.8, 31.3, 34.5, 35.1]} print mooring1['Lon'] dict_ex = dict(zip(['a','b', 'c', 'd', 'e'], range(5))) print dict_ex print dict_ex.keys() print dict_ex.values() dict_ex['a'] import numpy as np x = np.array([12, 10, 3]) print x a = np.arange(5,10,0.5) #note: endpoint is excluded print a print np.linspace(0,20,6) #note: endpoint is included b = np.zeros((3,3)) print b ones3d = np.ones((2,5,2)) print ones3d rand_array = np.random.randint(3,10, (4,5)) print rand_array print rand_array.shape a = np.arange(10) b = np.ones((10,1)) print a*b.T x = np.arange(10) print x x_ma = np.ma.masked_where(x>5, x) print x_ma print x_ma.mask x_ma_mean = np.mean(x_ma) print x_ma_mean y = np.array([1, 2, 3, np.nan, 5]) print np.mean(y) print np.nanmean(y) y_ma = np.ma.masked_where(np.isnan(y), y, copy=True) y_ma2 = np.ma.masked_invalid(y) print y_ma.mean() y[2] = 10 #if copy is set to False, then this modifies y_ma as well print y print y_ma dtype_list1 = [('x','f4'), ('y','f4'), ('z','f4')] coords = np.zeros((5,1), dtype=dtype_list1) print coords dtype_list2 = [('temp','f4'), ('sal','f4'), ('lon','f4'), ('lat','f4')] gridded_data = np.zeros((4,5), dtype=dtype_list2) print gridded_data gridded_data['temp'] = np.random.randint(20,30,(4,5)) gridded_data['sal'] = np.random.randint(30,35,(4,5)) lon = np.linspace(0,20,4) lat = np.linspace(10,30,5) lon,lat = np.meshgrid(lat,lon) gridded_data['lon'] = np.round(lon) gridded_data['lat'] = np.round(lat) print gridded_data print gridded_data['sal'] print gridded_data['lon'] print gridded_data[['temp', 'lat']] gridded_data.shape np.mean(gridded_data['temp'], axis=0)