This is part of Python for Geosciences notes.
================
Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C. The language provides constructs intended to enable clear programs on both a small and large scale.
-- From Wikipedia, the free encyclopedia
Variables¶
Python uses duck typing
Int¶
a = 10
a
type(a)
Float¶
z = 10.
z
type(z)
String¶
b = '2'
b
Some operations are not allowed on different types:
a+b
But some of them are allowed:
a*b
Might be a source of confusion :)
String variables can be combined:
c = ' guys walk into a bar'
c
b+c
In order to include variable of another type in to string you have to convert it:
str(a)+c
Everything is an object¶
In IPython you can get the list of object's methods and attributes by typing dot and pressing TAB:
c.
Methods are basically default functions that can be applied to our variable:
c.upper()
c.title()
c.count('a')
c.find('into')
If you need help on method in IPython type something like:
c.find?
Or open bracket and press TAB:
c.find(
Int variable is also an object:
a.bit_length()
Methods can be combined (kind of a pipeline)
c.title().count('a').bit_length()
Lists¶
There are several other interesting variable types in Python, but the one we would need the most is the list.
In order to create list put coma separated values in square brackets:
l = [1,2,3,4,5]
l
Sort of similar to Matlab variables, but not exactly.
Values in list can be any type:
l = ['one', 'two', 'three', 'four', 'five']
l
Combined
l = ['one', 2, 'three', 4.0, 3+2]
l
Any type means ANY type:
l = ['one', 2, 'three', [1,2,3,4,5], 3+2]
l
You can access list values by index:
l[0]
Oh, yes, indexing starts with zero, so for Matlab users the zero is the new one :) See discussion on the matter here.
l[1]
Let's have a look at the 4th element of our list:
l[3]
It's also a list, and its values can be accessed by indexes as well:
l[3][4]
You also can acces multiple elements of the list using slices:
l[1:3]
Slice will start with the first slice index and go up to but not including the second slice index.
l[3]
Control Structures¶
For loop:¶
This loop will print all elements from the list l
l = ['one', 2, 'three', [1,2,3,4,5], 3+2]
for element in l:
print element
Two interesting thins here. First: indentation, it's in the code, you must use it, otherwise code will not work:
for element in l:
print element
Second - you can iterate through the elements of the list. There is an option to iterate through a bunch of numbers as we used to in Matlab:
for index in range(5):
print(l[index])
where range is just generating a list with sequence of numbers:
range(5)
Branches¶
We are not going to use branches in this notes, but this is how they look like just as another example of indentation use:
x = -1
if x > 0:
print "Melting"
elif x == 0:
print "Zero"
else:
print "Freezing"
Modules¶
Pure python does not do much. Even IPython with pylab enabled can do only limited number of things. To do some specific tasks you need to import modules. Here I am going to demonstrate several ways to do so.
The most common one is to import complete library. In this example we import urllib2 - a library for opening URLs using a variety of protocols.
import urllib2
Here we get information from some ftp site. Note how function urlopen is called. We have to use name of the library, then dot, then name of the function from the library:
response = urllib2.urlopen('ftp://ftp.zmaw.de/')
html = response.read()
html.splitlines()
Another option is to import it like this:
from urllib2 import *
In this case all functions will be imported in to the name-space and you can use urlopen directly, without typing the name of the library first:
response = urlopen('ftp://ftp.zmaw.de/')
html = response.read()
html.splitlines()
But generally I think it's a bad idea, because your name-space is populated by things that you don't really need and it's hard to tell where the function comes from.
whos
You can import only function that you need:
from urllib2 import urlopen
response = urlopen('ftp://ftp.zmaw.de/')
html = response.read()
html.splitlines()
Or import library as alias in order to avoid extensive typing:
import urllib2 as ul
response = ul.urlopen('ftp://ftp.zmaw.de/')
html = response.read()
html.splitlines()
Comments !