02 Python basics

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

In [2]:
a = 10
In [4]:
a
Out[4]:
10
In [5]:
type(a)
Out[5]:
int

Float

In [6]:
z = 10.
z
Out[6]:
10.0
In [7]:
type(z)
Out[7]:
float

String

In [8]:
b = '2'
b
Out[8]:
'2'

Some operations are not allowed on different types:

In [9]:
a+b
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in ()
----> 1 a+b

TypeError: unsupported operand type(s) for +: 'int' and 'str'

But some of them are allowed:

In [10]:
a*b
Out[10]:
'2222222222'

Might be a source of confusion :)

String variables can be combined:

In [11]:
c = ' guys walk into a bar'
c
Out[11]:
' guys walk into a bar'
In [12]:
b+c
Out[12]:
'2 guys walk into a bar'

In order to include variable of another type in to string you have to convert it:

In [13]:
str(a)+c
Out[13]:
'10 guys walk into a bar'

Everything is an object

In IPython you can get the list of object's methods and attributes by typing dot and pressing TAB:

In [14]:
c.
Out[14]:
' guys walk into a bar'

Methods are basically default functions that can be applied to our variable:

In [15]:
c.upper()
Out[15]:
' GUYS WALK INTO A BAR'
In [16]:
c.title()
Out[16]:
' Guys Walk Into A Bar'
In [17]:
c.count('a')
Out[17]:
3
In [18]:
c.find('into')
Out[18]:
11

If you need help on method in IPython type something like:

In [ ]:
c.find?

Or open bracket and press TAB:

In [ ]:
c.find(

Int variable is also an object:

In [19]:
a.bit_length()
Out[19]:
4

Methods can be combined (kind of a pipeline)

In [22]:
c.title().count('a').bit_length()
Out[22]:
2

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:

In [23]:
l = [1,2,3,4,5]
l
Out[23]:
[1, 2, 3, 4, 5]

Sort of similar to Matlab variables, but not exactly.

Values in list can be any type:

In [24]:
l = ['one', 'two', 'three', 'four', 'five']
l
Out[24]:
['one', 'two', 'three', 'four', 'five']

Combined

In [25]:
l = ['one', 2, 'three', 4.0, 3+2]
l
Out[25]:
['one', 2, 'three', 4.0, 5]

Any type means ANY type:

In [1]:
l = ['one', 2, 'three', [1,2,3,4,5], 3+2]
l
Out[1]:
['one', 2, 'three', [1, 2, 3, 4, 5], 5]

You can access list values by index:

In [27]:
l[0]
Out[27]:
'one'

Oh, yes, indexing starts with zero, so for Matlab users the zero is the new one :) See discussion on the matter here.

In [28]:
l[1]
Out[28]:
2

Let's have a look at the 4th element of our list:

In [30]:
l[3]
Out[30]:
[1, 2, 3, 4, 5]

It's also a list, and its values can be accessed by indexes as well:

In [31]:
l[3][4]
Out[31]:
5

You also can acces multiple elements of the list using slices:

In [32]:
l[1:3]
Out[32]:
[2, 'three']

Slice will start with the first slice index and go up to but not including the second slice index.

In [2]:
l[3]
Out[2]:
[1, 2, 3, 4, 5]

Control Structures

For loop:

This loop will print all elements from the list l

In [3]:
l = ['one', 2, 'three', [1,2,3,4,5], 3+2]

for element in l:
    print element
one
2
three
[1, 2, 3, 4, 5]
5

Two interesting thins here. First: indentation, it's in the code, you must use it, otherwise code will not work:

In [34]:
for element in l:
print element
  File "", line 2
    print element
        ^
IndentationError: expected an indented block

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:

In [36]:
for index in range(5):
    print(l[index])
one
2
three
[1, 2, 3, 4, 5]
5

where range is just generating a list with sequence of numbers:

In [35]:
range(5)
Out[35]:
[0, 1, 2, 3, 4]

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:

In [37]:
x = -1
if x > 0:
   print "Melting"
elif x == 0:
   print "Zero"
else:
   print "Freezing"
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.

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

In [8]:
response = urllib2.urlopen('ftp://ftp.zmaw.de/')
html = response.read()
html.splitlines()
Out[8]:
['dr-xr-x---  17 ftp      ftp         20480 Oct 26 03:15 incoming',
 'dr-xr-x---  51 ftp      ftp         12288 Oct 26 03:30 outgoing']

Another option is to import it like this:

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

In [10]:
response = urlopen('ftp://ftp.zmaw.de/')
html = response.read()
html.splitlines()
Out[10]:
['dr-xr-x---  17 ftp      ftp         20480 Oct 26 03:15 incoming',
 'dr-xr-x---  51 ftp      ftp         12288 Oct 26 03:30 outgoing']

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.

In [43]:
whos
Variable                          Type                          Data/Info
-------------------------------------------------------------------------
AbstractBasicAuthHandler          classobj                      urllib2.AbstractBasicAuthHandler
AbstractDigestAuthHandler         classobj                      urllib2.AbstractDigestAuthHandler
AbstractHTTPHandler               classobj                      urllib2.AbstractHTTPHandler
BaseHandler                       classobj                      urllib2.BaseHandler
CacheFTPHandler                   classobj                      urllib2.CacheFTPHandler
FTPHandler                        classobj                      urllib2.FTPHandler
FileHandler                       classobj                      urllib2.FileHandler
HTTPBasicAuthHandler              classobj                      urllib2.HTTPBasicAuthHandler
HTTPCookieProcessor               classobj                      urllib2.HTTPCookieProcessor
HTTPDefaultErrorHandler           classobj                      urllib2.HTTPDefaultErrorHandler
HTTPDigestAuthHandler             classobj                      urllib2.HTTPDigestAuthHandler
HTTPError                         type                          
HTTPErrorProcessor                classobj                      urllib2.HTTPErrorProcessor
HTTPHandler                       classobj                      urllib2.HTTPHandler
HTTPPasswordMgr                   classobj                      urllib2.HTTPPasswordMgr
HTTPPasswordMgrWithDefaultRealm   classobj                      urllib2.HTTPPasswordMgrWithDefaultRealm
HTTPRedirectHandler               classobj                      urllib2.HTTPRedirectHandler
HTTPSHandler                      classobj                      urllib2.HTTPSHandler
OpenerDirector                    classobj                      urllib2.OpenerDirector
ProxyBasicAuthHandler             classobj                      urllib2.ProxyBasicAuthHandler
ProxyDigestAuthHandler            classobj                      urllib2.ProxyDigestAuthHandler
ProxyHandler                      classobj                      urllib2.ProxyHandler
Request                           classobj                      urllib2.Request
StringIO                          builtin_function_or_method    
URLError                          type                          
UnknownHandler                    classobj                      urllib2.UnknownHandler
a                                 int                           10
addinfourl                        classobj                      urllib.addinfourl
b                                 str                           2
base64                            module                        ib/python2.7/base64.pyc'>
bisect                            module                        ib/python2.7/bisect.pyc'>
build_opener                      function                      
c                                 str                            guys walk into a bar
element                           int                           5
ftpwrapper                        classobj                      urllib.ftpwrapper
getproxies                        function                      
hashlib                           module                        b/python2.7/hashlib.pyc'>
\n
httplib                           module                        b/python2.7/httplib.pyc'>
index                             int                           4
install_opener                    function                      
l                                 list                          n=5
localhost                         function                      
mimetools                         module                        python2.7/mimetools.pyc'>
os                                module                        
parse_http_list                   function                      
parse_keqv_list                   function                      
posixpath                         module                        python2.7/posixpath.pyc'>
proxy_bypass                      function                      
quote                             function                      
randombytes                       function                      
re                                module                        
request_host                      function                      
response                          urllib.addinfourl             ct object at 0x33af350>>>
socket                            module                        ib/python2.7/socket.pyc'>
splitattr                         function                      
splithost                         function                      
splitpasswd                       function                      
splitport                         function                      
splittag                          function                      
splittype                         function                      
splituser                         function                      
splitvalue                        function                      
time                              module                        
unquote                           function                      
url2pathname                      function                      
urllib2                           module                        b/python2.7/urllib2.pyc'>
urlopen                           function                      
urlparse                          module                        /python2.7/urlparse.pyc'>
x                                 int                           -1
z                                 float                         10.0

You can import only function that you need:

In [ ]:
from urllib2 import urlopen
In [44]:
response = urlopen('ftp://ftp.zmaw.de/')
html = response.read()
html.splitlines()
Out[44]:
['dr-xr-x---  16 ftp      ftp         20480 Oct 23 15:51 incoming',
 'dr-xr-x---  49 ftp      ftp         12288 Oct 23 03:30 outgoing']

Or import library as alias in order to avoid extensive typing:

In [11]:
import urllib2 as ul
In [12]:
response = ul.urlopen('ftp://ftp.zmaw.de/')
html = response.read()
html.splitlines()
Out[12]:
['dr-xr-x---  17 ftp      ftp         20480 Oct 26 03:15 incoming',
 'dr-xr-x---  51 ftp      ftp         12288 Oct 26 03:30 outgoing']

Links:

Comments !

links

social