In [ ]:
%matplotlib inline

Python Basics


Objectives:

  • Overview "things"
    • comments
    • data types
    • variables
    • Containers (i.e. Lists and Dictionaries)
  • Overview how to do "Actions"
    • Functions
    • Methods

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:

"Things"


Comments

Before you to write "code", you need to learn how to write "comments", which are "text for humans" (as opposed to "Text for computers).

In Python, you simply add a # to indicate that the line is a comment:


In [ ]:
# this is a comment

Data types (i.e. raw materials)

Integers

Integers are used to represent "whole numbers" (i.e. no decimal point)


In [372]:
2 + 4


Out[372]:
6

Strings

Strings are used to represent "letters"


In [373]:
'2' + '4'


Out[373]:
'24'

Floats

Floats are used to represent "numbers with decimals"


In [374]:
2.5 + 4.1


Out[374]:
6.6

There are more "Data types", but we can do a lot just with these ones above.


Variables (i.e. "Labels")

Variables are use to "name" or "label" things. Much like sticking a label on a jar and writing on the label: "sugar"

Lets "name" some floats


In [375]:
a = 1.2
b = 3.0
c = a + b

print c


4.2

Now lets "name" some strings


In [376]:
a = '1.2'
b = '3.0'
c = a + b

print c


1.23.0

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


True

Containers

"Containers" are "things" that are used to store other "things". Kinda' like storing your sugar in a jar...

Here are some of the more common "containers".

Lists

A "List" is a container specifically disigned to store... well, lists of "things".


In [378]:
mylist = ['a','b','c','d']

mylist


Out[378]:
['a', 'b', 'c', 'd']

In [379]:
mylist[0]


Out[379]:
'a'

In [380]:
mylist[-1]


Out[380]:
'd'

In [381]:
mylist[1:4]


Out[381]:
['b', 'c', 'd']

Dictionaries

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']

"Things" within "things"...

Lets do a harder example:


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]:
{'name': 'Joe', 'tickets': [2, 5, 7]}

In [384]:
myComplexThing[1]['name']


Out[384]:
'Pepe'

In [385]:
myComplexThing[2]['tickets'][-1]


Out[385]:
8

"Actions"


1. Functions

Functions are used following the "nomenclature" below:

output = FunctionName(inputs)

For example, lets do a list... and then apply some functions to it.


In [386]:
mylist = [1,8,5,9]

print mylist


[1, 8, 5, 9]

In [387]:
type(mylist)


Out[387]:
list

In [388]:
min(mylist)


Out[388]:
1

In [389]:
a = max(mylist)

a


Out[389]:
9

In [390]:
sum(mylist)


Out[390]:
23

In [391]:
len(mylist)


Out[391]:
4

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]:
0.8414709848078965

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


11

2. Methods

Methods are similar to functions, but the follow a different "nomenclature":

output = input.MethodName(arguments)

For example...


In [395]:
mylist.append(2)

print mylist


[1, 8, 5, 9, 2]

In [396]:
mylist.remove(1)

print mylist


[8, 5, 9, 2]

In [397]:
mylist.sort()

print mylist


[2, 5, 8, 9]

In [398]:
mylist.index(5)


Out[398]:
1

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 [ ]: