Differences between revisions 2 and 10 (spanning 8 versions)
Revision 2 as of 2009-10-25 13:05:51
Size: 13049
Editor: anonymous
Comment:
Revision 10 as of 2010-10-25 11:09:59
Size: 15130
Editor: anonymous
Comment:
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:
== Basic Python ==
This lesson includes the basic Python language. No modules have to be imported
= Basic Python =
This lesson includes the basic Python language. No modules have to be imported, except for the extended data types where a {{{numpy array}}} is used.
Line 375: Line 375:
NumPy/SciPy provides a multidimensional '''array''' data type. An array can hold arbitrary Python objects but usually they are used for N-dimensional numeric data types. NumPy/SciPy provides a multidimensional '''array''' data type. An array can hold arbitrary Python objects but usually they are used for N-dimensional numeric data types. We will learn more about arrays in the next chapter.
Line 396: Line 396:

In this section we first get to know about the built-in functions and after this we will learn how to define our own functions.

== Built-in Functions ==

The Python interpreter has a number of functions built into it that are always available.
* [[http://docs.python.org/library/functions.html|Built-in Functions]]

Important builtin-functions that are needed for the exercise are highlighted in the next sections.

=== File ===

The [[http://docs.python.org/lib/bltin-file-objects.html|file object]] can be used for reading and writing plain text as well as unformatted binary data. The following code writes a message in the file with the name ''out.txt'', reads and print the data

{{{
#!python

file('out.txt','w').write('Hello File') # Write a string to a file
print file('out.txt').read() # Read from file and print the results
}}}

 * {{{write()}}} writes a string to the file
 * {{{read()}}} reads complete file
 * {{{read(N)}}} reads N bytes
 * {{{readlines()}}} reads the file with linebreaks
 * {{{readline()}}} reads only the next line

=== Range ===

Range is a versatile function to create lists containing arithmetic progressions.

{{{
range([start], stop[, step])
}}}

=== Type ===

{{{type(object)}}} returns the type of an object.
Line 555: Line 593:
== Data types ==

{{{
D=dict([('b',1),('c',2)])
}}}

Please specify the data types:

 * D is a ...
 * 'b' is a ...
 * 1 is a ...
 * ('b',1) is a ...
 * [('b',1),('c',2)] is a ...


== Data structures ==

{{{#!python
D={'A':array(range(10)),'B':array(range(10))+1}
}}}

 * Which keys contains the dictionary {{{D}}}
 * Get the keys with the method {{{D.keys()}}}
 * Display all values of {{{D}}} using the method {{{.values()}}}
 * Display array {{{A}}}
 * Display the elements {{{3:6}}} of arrays {{{B}}}
 * Set this elements to 0
 * Insert a new key-values pair {{{C:array(range(0,10,-1))}}} into {{{D}}}
Line 556: Line 623:
Write a program that reads in a textfile. For every word calculate the frequency of occurence. You can use the functions {{{file}}}, {{{read}}}, {{{split}}}, {{{count}}}.
R
ead  in this [[attachment:Python_in_the_Scientific_World.txt|textfile]] and count the number of occurrence of the string ''Python''.
U
se the functions/methods {{{open}}}, {{{read}}}, {{{count}}}.

Basic Python

This lesson includes the basic Python language. No modules have to be imported, except for the extended data types where a numpy array is used.

Built-in keywords

Python built-in keywords

help> keywords

and                 else                import              raise
assert              except              in                  return
break               exec                is                  try
class               finally             lambda              while
continue            for                 not                 yield
def                 from                or
del                 global              pass
elif                if                  print

Built-in types

Python built-in numeric types

There are four distinct numeric types:

  • plain integers int

  • long integers long

  • floating point numbers float

  • complex numbers complex

In addition, Booleans are a subtype of plain integers

Boolean

Boolean (logical) types

  • True
  • False

Operations

x + y           sum of x and y
x - y           difference of x and y   
x * y           product of x and y      
x / y           quotient of x and y
x // y          (floored) quotient of x and y
x % y           remainder of x / y
-x              x negated       
+x              x unchanged     
abs(x)          absolute value or magnitude of x        
int(x)          x converted to integer
long(x)         x converted to long integer 
float(x)        x converted to floating point   
complex(re,im)  a complex number with real part re, 
                imaginary part im. im defaults to zero
c.conjugate()   conjugate of the complex number c
x ** y          x to the power y

Boolean Operations: and, or, not

x or y          if x is false, then y, else x 
x and y         if x is false, then x, else y 
not x           if x is false, then True, else False    

Comparisons

<               strictly less than      
<=              less than or equal      
>               strictly greater than   
>=              greater than or equal   
==              equal   
!=              not equal       

is              object identity         

Sequence types

  • String str

  • List list

  • Tuple tuple

Operations

x in s  True if an item of s is equal to x, else False  
x not in s      False if an item of s is equal to x, 
        else True       
s + t   the concatenation of s and t    
s[i]    i'th item of s
s[i:j]  slice of s from i to j  
s[i:j:k] slice of s from i to j with step k
len(s)  length of s     
min(s)  smallest item of s      
max(s)  largest item of s       

Variables, assignment

The assignment statement = creates new variables and gives them values

a=2
l=9**1000L
S='Hello'
f=1e5

Lists and tuples

  • Lists [] are mutable sequences of values

  • Tuples () can not be changed (immutable)

In [5]: a=[1,2,3]
In [6]: b=(1,2,3)

In [7]: a[0]
Out[7]: 1

In [8]: b[0]
Out[8]: 1

In [9]: a[0]=4

In [10]: a
Out[10]: [4, 2, 3]

In [11]: b[0]=4
---------------------------------------------------------------------------
exceptions.TypeError 

Indexing and slicing

  • Indices [] select elements of variables

  • Indices start from zero
  • x[a:b] selects a slice of elements from variable x

In [1]: a=range(10)

In [2]: a
Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [3]: a[0:4]
Out[3]: [0, 1, 2, 3]

In [4]: a[4:]
Out[4]: [4, 5, 6, 7, 8, 9]

In [5]: a[:4]
Out[5]: [0, 1, 2, 3]

In [6]: a[:-1]
Out[6]: [0, 1, 2, 3, 4, 5, 6, 7, 8]

In [7]: s='Hello world'
In [8]: s[:5]
Out[8]: 'Hello'

In [9]: a=tuple(range(10))
In [10]: a[0:4]
Out[10]: (0, 1, 2, 3)

List built-in functions (methods)

  • L.append(object) -- append object to end

  • L.count(value) -> integer -- return number of occurrences of value

  • L.extend(iterable) -- extend list by appending elements from the iterable

  • L.index(value, [start, [stop]]) -> integer -- return first index of value

  • L.insert(index, object) -- insert object before index

  • L.pop([index]) -> item -- remove and return item at index (default last)

  • L.reverse() -- reverse *IN PLACE*

  • L.sort() -- sort *IN PLACE*

In [72]: L=['C','A','B']
In [73]: L.sort()

In [74]: L
Out[74]: ['A', 'B', 'C']

In [75]: L.pop()
Out[75]: 'C'

In [76]: L
Out[76]: ['A', 'B']

In [77]: L.insert(1,'C')

In [78]: L
Out[78]: ['A', 'C', 'B']

Nested data structures

In [84]: a=range(10)
In [85]: b=range(0,10,2)
In [86]: b
Out[86]: [0, 2, 4, 6, 8]

In [87]: c=[a,b]

In [88]: c
Out[88]: [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 2, 4, 6, 8]]

In [91]: c[0]
Out[91]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [92]: c[1]
Out[92]: [0, 2, 4, 6, 8]

In [93]: c[0][0:4]
Out[93]: [0, 1, 2, 3]

In [94]: c[1][0:4]
Out[94]: [0, 2, 4, 6]

Dictionary

  • A dictionary is like a list, but more general.
  • In a list, the indices have to be integers; in a dictionary they can be (almost) any type.
  • d={}  creates empty dictionary

  •  d[key]=value  add a key, value pair to dictionary

  •  d.keys()  returns list of keys

In [95]: D={}
In [96]: D['a']=range(10)
In [97]: D['b']=range(0,10,2)

In [98]: D
Out[98]: {'a': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'b': [0, 2, 4, 6, 8]}

In [99]: D['a']
Out[99]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [100]: D.keys()
Out[100]: ['a', 'b']

Loops

  • Looping over a list is done for the  for  statement

  •  enumerate()  returns an enumerated object

In [106]: a=['Hund','Katze','Maus']
In [108]: for i in a:
   .....:     print i
   .....:
Hund
Katze
Maus

In [111]: for i,j in enumerate(a):
   .....:     print i,j
   .....:
0 Hund
1 Katze
2 Maus

Control statements

  • Conditions can be checked with if-else statement

  •  if  condition: indented block

  •  else: 

In [116]: if a==1:
   .....:     print 'a=1'
   .....: else:
   .....:     print 'a != 1'
   .....:
a=1

Modules

There are many ways to import modules. The resulting namespaces are different

  • Import module SciPy: import scipy

  • List SciPy subpackages help(scipy) or scipy.info(scipy)

  • Import SciPy subpackage n-dimensional image package import scipy.ndimage

  • List ndimage functions help(scipy.ndimage)

  • Help on ndimage function laplace help(scipy.ndimage.laplace)

  • Import SciPy module into local namespace from scipy import *

  • Import SciPy subpackage ndimage in namespace ndi import scipy.ndimage as ndi

  • Write source for this object to output scipy.source(ndi.laplace)

  • Reload a (changed) module dreload(module name)

Variable names

Allowed names for identifier (name of variable or function):

  • Any sequence of alphabetical character, number, or underline character _

  • First symbol has to be a character
  • Case sensitive
  • Not a python built-in keyword

Name space

The Python interpreter maps the name of an identifier in the active name space. The active name space depends on the active code block (module, function). The identifier can be local or global with respect to the active code block.

   1 def func(a):
   2     a=a+1 # a is visibly only within func()
   3     print a
   4 
   5 b=func(1)
   6 print b
   7 print a # This will throw a NameError exception

   1 import scipy
   2 scipy.pi # pi is in the name space of scipy
   3 
   4 from scipy import pi
   5 pi # pi is in the active name space
   6 
   7 from scipy import *
   8 pi # all identifiers of scipy are in the active name space
   9 
  10 del(pi) # Deletes pi from the active name space
  11 reset   # ipython function to delete all identifiers

Built-in datatypes

Scalar

  • Plain integer: 9

  • Long integer: 9**99, 1L

  • Hex integer: 0x10

  • Floating point: 0.1

  • Exponential floating point: 1e-3

  • Complex: 3+2j

Casting

  • int()
  • long()
  • float()
  • complex()
  • str()

Sequences

String

   1 s1='Hello world'
   2 s2="Hello world"
   3 s1==s2
   4 
   5 s='"'
   6 s="'"

Dictionary

   1 D={1:'one',2:'two','one':1,'two':2}
   2 D['one']
   3 D[1]

Extended data types

Array

NumPy/SciPy provides a multidimensional array data type. An array can hold arbitrary Python objects but usually they are used for N-dimensional numeric data types. We will learn more about arrays in the next chapter.

Array creation

  • empty((d1,d2),dtype) returns uninitialized array of shape d1,d2.

  • zeros((d1,d2),dtype) returns array of shape d1,d2 filled with zeros

  • ones((d1,d2),dtype) returns array of shape d1,d2 filled with ones

  • array(object,dtype) returns an array from an object, e.g. a list

  • dtype fundamental C data type e.g. uint8, int16, int64, float32, float64

Array indexing

  • A[y,x] returns (y,x) element of the array

  • A[:,x] returns all elements of the y-dimension at x

  • A[y1:y2,x]

  • A[:,:] returns a copy of two dimensional array A

  • A[:,:,0] returns the first sub-image of a 3-dimensional array

Functions

In this section we first get to know about the built-in functions and after this we will learn how to define our own functions.

Built-in Functions

The Python interpreter has a number of functions built into it that are always available. * Built-in Functions

Important builtin-functions that are needed for the exercise are highlighted in the next sections.

File

The file object can be used for reading and writing plain text as well as unformatted binary data. The following code writes a message in the file with the name out.txt, reads and print the data

   1 file('out.txt','w').write('Hello File') # Write a string to a file
   2 print file('out.txt').read() # Read from file and print the results
  • write() writes a string to the file

  • read() reads complete file

  • read(N) reads N bytes

  • readlines() reads the file with linebreaks

  • readline() reads only the next line

Range

Range is a versatile function to create lists containing arithmetic progressions.

range([start], stop[, step])

Type

type(object) returns the type of an object.

Function with one argument

In [124]: def quadrat(x):
   .....:     return x**2
   .....:

In [125]: quadrat(2)
Out[125]: 4

Function with variable number of arguments

In [126]: def printargs(*args):
   .....:     for arg in args:
   .....:         print arg
   .....:

In [127]: printargs(a)
[1, 2, 3]

In [128]: printargs(a,range(4))
[1, 2, 3]
[0, 1, 2, 3]

Function with variable number of keywords

In [131]: def printkeys(**kw):
   .....:     for k in kw.keys():
   .....:         print k,kw[k]
   .....:

In [132]: printkeys(key1=10,key2='Hallo')
key2 Hallo
key1 10

Function with functions as arguments (expert)

Functions can be passed to functions as arguments.

   1 from scipy import sin,pi
   2 
   3 def func(x,f):
   4         return f(x)
   5 
   6 print func(pi/2,sin)

eval(), apply()

In Matlab or IDL the passing of functions to functions works only with a detour using feval() or Call_function()

Of course, this way is possible in Python as well using the eval() or apply() function.

   1 apply(sin,(pi/2,))
   2 
   3 f='sin'
   4 x=pi/2.0
   5 eval(f+'('+str(x)+')')

In Fortran it is also possible to pass functions as arguments but with quite some overhead:

Lambda expressions (expert)

Lambda expressions are (anonymous) one line functions

   1 l=lambda x: x**2-1
   2 l(4)

Mapping (expert)

Similar as apply() works with one argument one can call map() with a sequence

   1 map(len,['Always','look','on','the','bright','side','of','life'])
   2 [6, 4, 2, 3, 6, 4, 2, 4]

The following example shows the combined usage of a lambda expression

   1 map(lambda s:len(s)+1,['Always','look','on','the','bright','side','of','life'])
   2 [7, 5, 3, 4, 7, 5, 3, 5]

Variable Scope (expert)

The scope of an identifier refers to the variable visibility. Variables inside a function have a local scope. Global variables can be declared and used but you should be very careful.

   1 def bad_style(x):
   2         # Name space of bad_style
   3         global y
   4         y=x 
   5         z=x
   6         return x
   7 
   8 # Name space of __main__
   9 x,y,z=2,2,2
  10 x=bad_style(1)
  11 print 'x=',x,'y=',y,'z=',z
  12 # y is changed!

A shell inside the shell (expert)

For debugging purposes it is sometimes useful to investigate local variables at some points. The following example demonstrates a feature of ipython:

   1 from IPython.Shell import IPShellEmbed
   2 ips=IPShellEmbed()
   3 
   4 def spam(a):
   5     b=a+' Spam!'
   6     ips()
   7     print b

In [1]: from spam import spam
In [2]: whos
Interactive namespace is empty.
In [3]: spam('Hello')

# Now we are in the ips() call

In [1]: whos
Variable   Type    Data/Info
----------------------------
a          str     Hello
b          str     Hello Spam!

In [2]: exit()
Do you really want to exit ([y]/n)? y
Hello Spam!

# We are now again in top level
In [4]: whos
Interactive namespace is empty.

Exercises

Range

What is the corresponding range() expression for the following lists?

[1, 3, 5, 7, 9]
[5, 8, 11, 14, 17]
[19, 18, 17, 16, 15, 14, 13]

Data types

D=dict([('b',1),('c',2)])

Please specify the data types:

  • D is a ...
  • 'b' is a ...
  • 1 is a ...
  • ('b',1) is a ...
  • [('b',1),('c',2)] is a ...

Data structures

   1 D={'A':array(range(10)),'B':array(range(10))+1}
  • Which keys contains the dictionary D

  • Get the keys with the method D.keys()

  • Display all values of D using the method .values()

  • Display array A

  • Display the elements 3:6 of arrays B

  • Set this elements to 0
  • Insert a new key-values pair C:array(range(0,10,-1)) into D

Files and Strings

Read in this textfile and count the number of occurrence of the string Python. Use the functions/methods open, read, count.

LehreWiki: OpenSource2010/Lesson2 (last edited 2010-10-25 11:09:59 by anonymous)