In [ ]:
%matplotlib inline
In Python, you have "things" and "actions".
Now, these are my own made-up terms that I'll use for now just to make things simple. In reality, Python uses the term "Objects" for what I call "things", and... well, I don't think Python has a term for what I call "actions", but for now none of this matters... lets start:
In [ ]:
# this is a comment
In [372]:
2 + 4
Out[372]:
In [373]:
'2' + '4'
Out[373]:
In [374]:
2.5 + 4.1
Out[374]:
There are more "Data types", but we can do a lot just with these ones above.
Lets "name" some floats
In [375]:
a = 1.2
b = 3.0
c = a + b
print c
Now lets "name" some strings
In [376]:
a = '1.2'
b = '3.0'
c = a + b
print c
Now lets introduce one more Data Type (Boolean), which is used to represent "True" or "False". Lets "name" some booleans
In [377]:
a = True
b = False
print a
In [378]:
mylist = ['a','b','c','d']
mylist
Out[378]:
In [379]:
mylist[0]
Out[379]:
In [380]:
mylist[-1]
Out[380]:
In [381]:
mylist[1:4]
Out[381]:
A "Dictionary" is a container specifically disigned to store "things", in a way that resembles a dictionary. You look up the "key" word (e.g. "apple") and your immediatly get the contents (i.e. "Red round fruit that tastes good").
The idea of having a "key" word, is so that you don't have to scall the whole list to find what you want.
In [ ]:
myd = {'name':'Joe','age':25,'gender':'male'}
myd
In [ ]:
myd['name']
In [ ]:
myd['age']
In [382]:
myComplexThing = [{'name':'Joe','tickets':[2,5,7]},{'name':'Pepe','tickets':[1,6,9]},{'name':'Maria','tickets':[0,4,8]}]
In [383]:
myComplexThing[0]
Out[383]:
In [384]:
myComplexThing[1]['name']
Out[384]:
In [385]:
myComplexThing[2]['tickets'][-1]
Out[385]:
In [386]:
mylist = [1,8,5,9]
print mylist
In [387]:
type(mylist)
Out[387]:
In [388]:
min(mylist)
Out[388]:
In [389]:
a = max(mylist)
a
Out[389]:
In [390]:
sum(mylist)
Out[390]:
In [391]:
len(mylist)
Out[391]:
Here is a list of the basic Functions built-in within Python:
https://docs.python.org/2/library/functions.html
However, there are a lot more. You can import functions from additional "modules" (or "packages")... therefore you have thousands of functions at your disposal. Here is an example about how to import a Module and use one of its functions.
In [392]:
# First import the module "math"
import math
# Then use the function "sin" within the module "math"
math.sin(mylist[0])
Out[392]:
Finally, you can also make your own function:
In [393]:
def my_cool_function(inputs):
'''
Here you add some instructions and examples
'''
output = sum(inputs)/2
return output
Now that we made our own function... we can use it!
In [394]:
answer = my_cool_function(mylist)
print answer
In [395]:
mylist.append(2)
print mylist
In [396]:
mylist.remove(1)
print mylist
In [397]:
mylist.sort()
print mylist
In [398]:
mylist.index(5)
Out[398]:
I bet, up to here, everything sound good and easy, but not really that useful.
In [400]:
# Import Pandas module
import pandas as pd
# Define URL of LOBO buoy
URL = 'http://lobo.satlantic.com/cgi-data/nph-data.cgi?min_date=20090610&max_date=20090620&y=temperature'
# Read data from LOBO buoy
mycoolvar = pd.read_csv(URL,sep='\t')
# Do quick plot
mycoolvar.plot();
In [ ]: