Differences between revisions 5 and 21 (spanning 16 versions)
Revision 5 as of 2013-08-30 14:13:45
Size: 12467
Editor: hassan
Comment:
Revision 21 as of 2015-09-29 14:20:58
Size: 19646
Editor: hassan
Comment:
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:

The metabolic modelling software we will be using, {{{ScrumPy}}}, is written in {{{Python}}}. {{{Python}}} is a high-level, object-oriented, interpreted programming language, it has a large standard library, and supports multiple programming paradigms. It is also syntactically clear and easy to learn. This is a very brief introduction to some of the basic features of the language, for a more complete introduction to the topic, see Lutz & Ascher, "Learning Python" O'Reilly Media inc. (Edition 2 or greater). A good source of {{{Python}}} documentation can be found [[http://docs.python.org/ | here]].
The metabolic modelling software we will be using, {{{ScrumPy}}}, is written in {{{Python}}}. {{{Python}}} is a high-level, object-oriented, interpreted programming language, it has a large standard library, and supports multiple programming paradigms. It is also syntactically clear and easy to learn. This is a very brief introduction to some of the basic features of the language, for a more complete introduction to the topic, see Lutz & Ascher, "Learning Python" O'Reilly Media inc. (Edition 2 or greater). A good source of {{{Python}}} documentation can be found [[http://docs.python.org/|here]].
Line 7: Line 5:

We will be using Python from the !ScrumPy environment. To start a new !ScrumPy session open
a terminal and type "{{{ScrumPy}}}":
We will be using Python from the !ScrumPy environment. To start a new !ScrumPy session open  a terminal and type "{{{ScrumPy}}}":
Line 14: Line 10:
Line 17: Line 12:
Line 19: Line 13:

Any data we may wish to store or manipulate is associated with a specific ''type''. The ways in which the data may be manipulated depends on the type. For example whole numbers are associated with the "{{{int}}}" type and can simply be added together:

{{{
2 + 1 = 3
}}}

Likewise, sequences of characters are associated with the "{{{string}}}" type, and they too, can be added together:
}}}

---- /!\ '''Edit conflict - other version:''' ----
Likewise, sequences of characters are associated with the {{{string}}} type, and they too, can be added together:
Line 31: Line 21:
Line 34: Line 23:
---- /!\ '''Edit conflict - your version:''' ----

---- /!\ '''End of edit conflict''' ----
Line 37: Line 30:

A given data type can be as simple as a single digit, or as complex as a suite of genome databases. Python comes with a number of relativley simple (but still extrememely useful) data-types, and !ScrumPy extends this by providing additional data-types particularly useful for metabolic modelling and related activities. 
A given data type can be as simple as a single digit, or as complex as a suite of genome databases. Python comes with a number of relatively simple (but still extremely useful) data-types, and !ScrumPy extends this by providing additional data-types particularly useful for metabolic modelling and related activities.

---- /!\ '''Edit conflict - other version:''' ----
Line 42: Line 36:
Line 44: Line 37:
Line 48: Line 40:

---- /!\ '''Edit conflict - your version:''' ----

---- /!\ '''End of edit conflict''' ----
Line 58: Line 54:
>>> str(n_int)
'135'
}}}
}}}
Line 63: Line 56:

The numerical types we will be dealing with are integers, {{{int}}}, and floating-point numbers, {{{float}}}. Integers are written as a sequence of digits. Floats are written as digits with a decimal point in the sequence, and an optional exponent ({{{e}}} or {{{E}}}). 
The numerical types we will be dealing with are integers, {{{int}}}, and floating-point numbers, {{{float}}}. Integers are written as a sequence of digits. Floats are written as digits with a decimal point in the sequence, and an optional exponent ({{{e}}} or {{{E}}}).

---- /!\ '''Edit conflict - other version:''' ----
Line 74: Line 68:
Line 76: Line 69:
{{{#!python
{{{#!python

---- /!\ '''Edit conflict - your version:''' ----

---- /!\ '''End of edit conflict''' ----
Line 82: Line 80:
Line 84: Line 81:
{{{#!python
{{{#!python

---- /!\ '''Edit conflict - other version:''' ----
Line 91: Line 91:
Line 94: Line 93:

Line 97: Line 94:
Line 111: Line 107:
>>>
}}}
== Lists (and tuples) ==

---- /!\ '''Edit conflict - your version:''' ----
>>> val=True
>>> if val:
        print 'val is true'

'val is true'

>>> val=False
>>> if val:
        print 'val is true'
Line 118: Line 129:

---- /!\ '''End of edit conflict''' ----
Line 124: Line 137:
Items can be appended to a list by using the ''append()'' method. ---- /!\ '''Edit conflict - other version:''' ----
Items can be appended to a list by using the {{{append()}}} method.
Line 131: Line 145:
Line 133: Line 146:
Line 137: Line 151:
}}}


Items can be removed from a list using the ''remove()'' method.
{{{#!python
>>> empty_list.remove('string 1')
>>> empty_list
[]
}}}

As with strings, indexing can be used to copy a subset of a list, keep in mind that the indices of items in lists (like characters in strings) are numbered from 0. Membership of an item in a list can be evaluated as described for strings.

---- /!\ '''Edit conflict - your version:''' ----
Line 159: Line 164:
}}}
 

The index of a known item can be retrieved using the ''index()'' method.

---- /!\ '''End of edit conflict''' ----
}}}

---- /!\ '''Edit conflict - other version:''' ----
Items can be removed from a list using the {{{remove()}}} method.

{{{#!python
>>> empty_list.remove('string 1')
>>> empty_list
[]
}}}
As with strings, indexing can be used to copy a subset of a list, keep in mind that the indices of items in lists (like characters in strings) are numbered from 0. Membership of an item in a list can be evaluated as described for strings.

---- /!\ '''Edit conflict - your version:''' ----


Subsets of lists (and strings) can be accessed using ''slicing'':


{{{#!python
>>> empty_list[1:3]
'string 2', 'string 3']
}}}

The index of a known item can be retrieved using the {{{index()}}} method.
Line 169: Line 196:
Lists can contain any objects, including other lists. These lists are referred to as ''nested'':

---- /!\ '''End of edit conflict''' ----

{{{#!python
>>> empty_list = ['string 1','string 2','string 3']
>>> nested = [1,2,3]
>>> empty_list.append(nested)
>>> empty_list
['string 1', 'string 2', 'string 3', [1, 2, 3]]
>>> empty_list[3]
[1, 2, 3]
>>> empty_list[3][0]
1
}}}

---- /!\ '''Edit conflict - other version:''' ----
Subsets of lists (and strings) can be accessed using ''slicing'':

{{{#!python
>>> empty_list[1:3]
'string 2', 'string 3']
}}}
The index of a known item can be retrieved using the {{{index()}}} method.

{{{#!python
>>> empty_list.index('string_1')
0
}}}
Lists can contain any objects, including other lists. These lists are referred to as ''nested'':

{{{#!python
>>> empty_list = ['string 1','string 2','string 3']
>>> nested = [1,2,3]
>>> empty_list.append(nested)
>>> empty_list
['string 1', 'string 2', 'string 3', [1, 2, 3]]
>>> empty_list[3]
[1, 2, 3]
>>> empty_list[3][0]
1
}}}

---- /!\ '''Edit conflict - your version:''' ----

---- /!\ '''End of edit conflict''' ----
Line 172: Line 244:
---- /!\ '''Edit conflict - other version:''' ----
Line 192: Line 265:
As with lists, dictionaries can contain ''nested'' dictionaries:

{{{#!python
>>> dict_1 = {'alfa':1,'beta':2} #define first dictionary
>>> dict_2 = {'gamma':1} #define second dictionary
>>> dict_1['nested'] = dict_2 #add dict_2 as value to key 'nested' in dict_1
>>> dict_1
{'beta': 2, 'alfa': 1, 'nested': {'gamma': 1}}
>>> dict_1['nested']['gamma'] #access key 'gamma' in nested dictionary
1
}}}

---- /!\ '''Edit conflict - your version:''' ----

---- /!\ '''End of edit conflict''' ----
Line 194: Line 281:

{{{Python}}} can be used in interactive mode (as we have seen above), as well as in batch mode. A pice of {{{Python}}} code can be saved in a text file with the file extension {{{.py}}} (in {{{ScrumPy}}} the most convenient option is the build-in IDLE text editor, accessed from the tool-bar under {{{File> New window}}}). The module can be run in the {{{ScrumPy}}} shell by ''importing'' the file. Assuming we have saved a file names ''some_py.py'' in the current directory, this is how it works:

{{{
user@machine:~$ ls *.py #in terminal, list all .py files
some_py.py
user@machine:~$ more some_py.py

#Python 'Hello world!' program
print 'Hello world!'
}}}
user@machine:~$ more some_py_func.py

#print user-specified string
def print_string(string):
    print string
}}}

---- /!\ '''Edit conflict - other version:''' ----
Line 208: Line 291:

---- /!\ '''Edit conflict - your version:''' ----

{{{#!python
>>> reload(some_py_func) #if an imported module is modified the changes are updated by reloading the module
>>> some_py_func.print_string('Hello world!')

---- /!\ '''End of edit conflict''' ----
Line 211: Line 302:
It is usually more convenient to structure the stored code into functions that can be executed from the imported module. Functions are defined using the key-word ''def''.  ---- /!\ '''Edit conflict - other version:''' ----
It is usually more convenient to structure the stored code into functions that can be executed from the imported module. Functions are defined using the key-word ''def''.

---- /!\ '''Edit conflict - your version:''' ----

In many applications functions return objects to the user.

---- /!\ '''End of edit conflict''' ----
Line 216: Line 314:
#Python 'Hello world!' program, accessed by function
def print_hello():
    print 'Hello world!'
}}}
#return list of characters in user-specified string
def print_string(string):
    return list(string)
}}}

---- /!\ '''Edit conflict - other version:''' ----
Line 230: Line 329:
Line 232: Line 330:
Line 238: Line 337:
}}}

{{{#!python
>>> reload(some_py_func) #if an imported module is modified the changes are updated by reloading the module
>>> some_py_func.print_string('Hello world!')
Hello world!
}}}

In many applications functions return objects to the user.

{{{
user@machine:~$ more some_py_func.py

#return list of characters in user-specified string
def print_string(string):
    return list(string)
}}}

---- /!\ '''Edit conflict - your version:''' ----
Line 262: Line 346:

Line 278: Line 360:
        self.str_list.append(item)

    def add2Dict(self,key,val):
        self.str_dict[key]=val

---- /!\ '''End of edit conflict''' ----
}}}

---- /!\ '''Edit conflict - other version:''' ----
{{{#!python
>>> reload(some_py_func) #if an imported module is modified the changes are updated by reloading the module
>>> some_py_func.print_string('Hello world!')
Hello world!
}}}
In many applications functions return objects to the user.

{{{
user@machine:~$ more some_py_func.py

#return list of characters in user-specified string
def print_string(string):
    return list(string)
}}}
{{{#!python
>>> reload(some_py_func)
>>> hello_list = some_py_func.print_string('Hello world!')
>>> hello_list
['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']
}}}
== Objects ==
As mentioned, {{{Python}}} supports multiple programming paradigms, one of those being object-orientation. Object-orientation allows collection of data into ''objects'', or class ''instances''. The data collected in objectes is referred to as ''fields'' or ''attributes'', objects also store functions that usually performs actions on the attributes. Object-specific functions are called ''methods''. Here is a small example of class definition, initialisation, and usage of a class that stores a list and a dictionary.

{{{
user@machine:~$ more StringDL.py
class StringDnLs:
    def __init__(self):
        self.str_list=[]
        self.str_dict={}

    def add2List(self,item):
Line 284: Line 406:
---- /!\ '''Edit conflict - your version:''' ----

---- /!\ '''End of edit conflict''' ----

---- /!\ '''Edit conflict - other version:''' ----
Line 289: Line 416:
>>> strdn.addList('a string')  # add a string to str_list using method addList(item)
>>> strdn.str_list                    
['a string']   
}}}
  == Loops, conditionals, assignment, evaluation, and others ==
>>> strdn.add2List('a string') # add a string to str_list using method addList(item)
>>> strdn.str_list
['a string']
}}}
== Built-in functions, loops, conditionals, assignment, and evaluation ==
Line 297: Line 422:
Line 303: Line 429:
Line 307: Line 434:
You can read more about the built-in functions [[http://docs.python.org/2/library/functions.html|here]].
Line 315: Line 443:
a
b
c

>>> for i in range(len(a_list)): #iterating over indices
        print a_list[i]

a
b
c
---- /!\ '''Edit conflict - your version:''' ----
>>> range(0,10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
}}}

The step size is 1 by default, but can be specified as the third argument:
{{{#!python
>>> range(0,10,2)
[0, 2, 4, 6, 8]
}}}

Note that the step size must be an integer. If floating point steps are needed, the {{{arange()}}} function from the {{{numpy}}} package can be used. It is very similar to the {{{range()}}} function but accepts floating point arguments and returns {{{array}}} objects, which can be converted to lists using the method {{{tolist()}}}

{{{#!python
>>> from numpy import arange
>>> arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> arange(0,5,0.5).tolist()
[0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]
Line 335: Line 474:
---- /!\ '''End of edit conflict''' ----
Line 338: Line 479:
}}}

---- /!\ '''Edit conflict - other version:''' ----

>>> for i in range(len(a_list)): #iterating over indices
        print a_list[i]

a
b
c
}}}
Note that {{{range(integer_1,integer_2)}}} is a built-in function that returns a list of integers ranging from the one integer, {{{integer_1}}}, to (but excluding) the other, {{{integer_2}}}. If the first argument is left blank, {{{Python}}} assumes {{{integer_1}}} is 0. For example:

{{{#!python
>>> range(0,10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
}}}
The step size is 1 by default, but can be specified as the third argument:

{{{#!python
>>> range(0,10,2)
[0, 2, 4, 6, 8]
}}}
Note that the step size must be an integer. If floating point steps are needed, the {{{arange()}}} function from the {{{numpy}}} package can be used. It is very similar to the {{{range()}}} function but accepts floating point arguments and returns {{{array}}} objects, which can be converted to lists using the method {{{tolist()}}}

{{{#!python
>>> from numpy import arange
>>> arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> arange(0,5,0.5).tolist()
[0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]
}}}
{{{while}}} loops iterate until a condition is fulfilled.

{{{#!python
>>> a_list = ['a','b','c']
>>> i = 0 #assign value 0 to variable i
>>> while i<len(a_list): #as long as i is less than 3
        print a_list[i] #print item at index i in a_list
        i += 1 #increment i by 1

a
b
c

---- /!\ '''Edit conflict - your version:''' ----

---- /!\ '''End of edit conflict''' ----
}}}

---- /!\ '''Edit conflict - other version:''' ----

---- /!\ '''Edit conflict - your version:''' ----


---- /!\ '''End of edit conflict''' ----
Line 358: Line 554:
---- /!\ '''Edit conflict - other version:''' ----

---- /!\ '''Edit conflict - your version:''' ----


---- /!\ '''End of edit conflict''' ----
Line 362: Line 564:

---- /!\ '''Edit conflict - other version:''' ----
        if type(item)==type(str()):
            print item+' is string'
        elif type(item)==type(int()):

---- /!\ '''Edit conflict - your version:''' ----
Line 365: Line 574:

---- /!\ '''End of edit conflict''' ----
Line 373: Line 584:

---- /!\ '''Edit conflict - other version:''' ----
}}}
You may have noticed it already, but it is necessary to point out the distinction between assignment and evaluation:


---- /!\ '''Edit conflict - your version:''' ----
Line 376: Line 594:

---- /!\ '''End of edit conflict''' ----

Introduction to Python for Metabolic Modellers

The metabolic modelling software we will be using, ScrumPy, is written in Python. Python is a high-level, object-oriented, interpreted programming language, it has a large standard library, and supports multiple programming paradigms. It is also syntactically clear and easy to learn. This is a very brief introduction to some of the basic features of the language, for a more complete introduction to the topic, see Lutz & Ascher, "Learning Python" O'Reilly Media inc. (Edition 2 or greater). A good source of Python documentation can be found here.

Getting started

We will be using Python from the ScrumPy environment. To start a new ScrumPy session open a terminal and type "ScrumPy":

user@machine:~$ ScrumPy &

which will launch the ScrumPy window.

Data types

}}}


/!\ Edit conflict - other version:


Likewise, sequences of characters are associated with the string type, and they too, can be added together:

"Apple" + "Pie" = "ApplePie"

However a string and an integer, cannot be meaningfully added together, so:


/!\ Edit conflict - your version:



/!\ End of edit conflict


"Apple" + 2 = !! ERROR !!

A given data type can be as simple as a single digit, or as complex as a suite of genome databases. Python comes with a number of relatively simple (but still extremely useful) data-types, and ScrumPy extends this by providing additional data-types particularly useful for metabolic modelling and related activities.


/!\ Edit conflict - other version:


We will start here by examining some of the common built-in types.

Strings

Strings are collections of characters. Characters in a string can be accessed by indexing, and membership of a subset of characters in a string can be evaluated.

   1 ---- /!\ '''Edit conflict - your version:''' ----
   2 
   3 ---- /!\ '''End of edit conflict''' ----
   4 >>> s_1 = 'another string'   #create string
   5 >>> s_2 = s_1[:7]            #create new string of characters 0 to 7 in s_1
   6 >>> s_2
   7 'another'
   8 >>> if s_2 in s_1:          #check for membership of s_2 in s_1
   9         print 'true'
  10 
  11 true
  12 >>> type(s_1)
  13 <type 'str'>

Numerical types

The numerical types we will be dealing with are integers, int, and floating-point numbers, float. Integers are written as a sequence of digits. Floats are written as digits with a decimal point in the sequence, and an optional exponent (e or E).


/!\ Edit conflict - other version:


   1 >>> n_int = 135
   2 >>> n_int
   3 135
   4 >>> n_float = 10e-10
   5 >>> n_float
   6 1.0000e-9

The type of a given data object can be checked using the built-in function type().

   1 ---- /!\ '''Edit conflict - your version:''' ----
   2 
   3 ---- /!\ '''End of edit conflict''' ----
   4 >>> type(n_int)
   5 <type 'int'>
   6 >>>type(n_float)
   7 <type 'float'>

Floats and integers can be interconverted using the constructors int() or float().

   1 ---- /!\ '''Edit conflict - other version:''' ----
   2 >>> n_int2float=float(n_int)
   3 >>> n_int2float
   4 135.0
   5 >>> type(n_int2float)
   6 <type 'float'>   #n_int is still an integer

The common mathematical operators (+,-,/,*) work as expected, note that x**y means xy.

Boolean

Booleans are a subtype of integers. A boolean type is either True or False, and can be very useful when writing conditional statements, i.e. if something is True, do something. Also, the integer 0 is False.

   1 >>> val=True
   2 >>> if val:
   3         print 'val is true'
   4 
   5 'val is true'
   6 
   7 >>> val=False
   8 >>> if val:
   9         print 'val is true'
  10 
  11 >>>

Lists (and tuples)


/!\ Edit conflict - your version:


>>> val=True >>> if val:

  • print 'val is true'

'val is true'

>>> val=False >>> if val:

  • print 'val is true'

>>> }}}

Lists (and tuples)


/!\ End of edit conflict


Lists and tuples are collections of items in which are stored in a specific order and each item is associated with (indexed by) an integer. The main difference between the two is that tuples are immutable - once a tuple is created it cannot be changed, whereas lists can. For these exercises we will mainly use lists. An empty list can be created by assigning a pair of closed square brackets to a variable.

   1 >>> empty_list=[]


/!\ Edit conflict - other version:


Items can be appended to a list by using the append() method.

   1 >>> empty_list.append('string 1')
   2 >>> empty_list
   3 ['string 1']

A list can also be created and populated in one go.

   1 >>> empty_list=['string 1']
   2 >>> empty_list
   3 ['string 1']
   4 
   5 ---- /!\ '''Edit conflict - your version:''' ----
   6 
   7 {{{#!python
   8 >>> empty_list = ['string 1','string 2','string 3']
   9 >>> empty_list[0]
  10 'string 1'
  11 >>> empty_list[1]
  12 'string 2'
  13 >>> empty_list[2]
  14 'string 3'
  15 >>> empty_list[-1]
  16 'string 3'
  17 
  18 ---- /!\ '''End of edit conflict''' ----


/!\ Edit conflict - other version:


Items can be removed from a list using the remove() method.

   1 >>> empty_list.remove('string 1')
   2 >>> empty_list
   3 []

As with strings, indexing can be used to copy a subset of a list, keep in mind that the indices of items in lists (like characters in strings) are numbered from 0. Membership of an item in a list can be evaluated as described for strings.


/!\ Edit conflict - your version:


Subsets of lists (and strings) can be accessed using slicing:

   1 >>> empty_list[1:3]
   2 'string 2', 'string 3']

The index of a known item can be retrieved using the index() method.

   1 >>> empty_list.index('string_1')
   2 0

Lists can contain any objects, including other lists. These lists are referred to as nested:


/!\ End of edit conflict


   1 >>> empty_list = ['string 1','string 2','string 3']
   2 >>> nested = [1,2,3]
   3 >>> empty_list.append(nested)
   4 >>> empty_list
   5 ['string 1', 'string 2', 'string 3', [1, 2, 3]]
   6 >>> empty_list[3]
   7 [1, 2, 3]
   8 >>> empty_list[3][0]
   9 1


/!\ Edit conflict - other version:


Subsets of lists (and strings) can be accessed using slicing:

   1 >>> empty_list[1:3]
   2 'string 2', 'string 3']

The index of a known item can be retrieved using the index() method.

   1 >>> empty_list.index('string_1')
   2 0

Lists can contain any objects, including other lists. These lists are referred to as nested:

   1 >>> empty_list = ['string 1','string 2','string 3']
   2 >>> nested = [1,2,3]
   3 >>> empty_list.append(nested)
   4 >>> empty_list
   5 ['string 1', 'string 2', 'string 3', [1, 2, 3]]
   6 >>> empty_list[3]
   7 [1, 2, 3]
   8 >>> empty_list[3][0]
   9 1


/!\ Edit conflict - your version:



/!\ End of edit conflict


Dictionaries


/!\ Edit conflict - other version:


In other programming languages dictionaries are sometimes called "associative arrays". Unlike lists, dictionaries store collections of items that are ordered by keys, not indices. There is no specific order of the items in a dictionary. The keys of a dictionary must be unique (for a given dictionary) and be hashable, for now this means that any object that is not a list can be used as a key. Here are some examples of dictionaries in action:

   1 >>> dict_1 = {'alfa':1,'beta':2}  #create a dictionary
   2 >>> keys = ['alfa','beta']
   3 >>> vals = [1,2]
   4 >>> dict_1 = dict(zip(keys,vals)) #create a dictionary from two lists
   5 >>> dict_1
   6 {'alfa':1,'beta':2}
   7 >>> dict_1['alfa']                #access value '1' by key 'alfa'
   8 1
   9 >>> dict_1.has_key('beta')        #check that dict_1 has key 'beta'
  10 True
  11 >>> dict_1.keys()                 #print keys of dict_1
  12 ['alfa','beta']
  13 >>> dict_1.values()               #print values
  14 [1,2]
  15 >>> dict_1['gamma'] = 1          #add new key:value pair
  16 >>> dict_1['alfa'] = 'a'         #overwrite key:value pair

As with lists, dictionaries can contain nested dictionaries:

   1 >>> dict_1 = {'alfa':1,'beta':2} #define first dictionary
   2 >>> dict_2 = {'gamma':1}         #define second dictionary
   3 >>> dict_1['nested'] = dict_2    #add dict_2 as value to key 'nested' in dict_1
   4 >>> dict_1
   5 {'beta': 2, 'alfa': 1, 'nested': {'gamma': 1}}
   6 >>> dict_1['nested']['gamma']    #access key 'gamma' in nested dictionary
   7 1


/!\ Edit conflict - your version:



/!\ End of edit conflict


Modules and functions

user@machine:~$ more some_py_func.py

#print user-specified string def print_string(string):

  • print string

}}}


/!\ Edit conflict - other version:


   1 >>> import some_py          #import some_py.py (note absence of extension!)
   2 
   3 ---- /!\ '''Edit conflict - your version:''' ----
   4 
   5 {{{#!python
   6 >>> reload(some_py_func)   #if an imported module is modified the changes are updated by reloading the module
   7 >>> some_py_func.print_string('Hello world!')
   8 
   9 ---- /!\ '''End of edit conflict''' ----
  10 Hello world!


/!\ Edit conflict - other version:


It is usually more convenient to structure the stored code into functions that can be executed from the imported module. Functions are defined using the key-word def.


/!\ Edit conflict - your version:


In many applications functions return objects to the user.


/!\ End of edit conflict


user@machine:~$ more some_py_func.py

#return list of characters in user-specified string
def print_string(string):
    return list(string)


/!\ Edit conflict - other version:


   1 >>> import some_py_func
   2 >>> some_py_func.print_hello()
   3 Hello world!
   4 >>> def print_hello():          #functions can also be defined in the shell
   5         print 'Hello world!'
   6 >>> print_hello()
   7 Hello world!

Functions often require arguments that the user is supposed to provide.

user@machine:~$ more some_py_func.py

#print user-specified string
def print_string(string):
    print string

---- /!\ '''Edit conflict - your version:''' ----

{{{#!python
>>> reload(some_py_func)   
>>> hello_list = some_py_func.print_string('Hello world!')
>>> hello_list
['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']

Objects

As mentioned, Python supports multiple programming paradigms, one of those being object-orientation. Object-orientation allows collection of data into objects, or class instances. The data collected in objectes is referred to as fields or attributes, objects also store functions that usually performs actions on the attributes. Object-specific functions are called methods. Here is a small example of class definition, initialisation, and usage of a class that stores a list and a dictionary.

user@machine:~$ more StringDL.py
class StringDnLs:
    def __init__(self):
        self.str_list=[]
        self.str_dict={}

    def add2List(self,item):
        self.str_list.append(item)

    def add2Dict(self,key,val):
        self.str_dict[key]=val

---- /!\ '''End of edit conflict''' ----


/!\ Edit conflict - other version:


   1 >>> reload(some_py_func)   #if an imported module is modified the changes are updated by reloading the module
   2 >>> some_py_func.print_string('Hello world!')
   3 Hello world!

In many applications functions return objects to the user.

user@machine:~$ more some_py_func.py

#return list of characters in user-specified string
def print_string(string):
    return list(string)

   1 >>> reload(some_py_func)
   2 >>> hello_list = some_py_func.print_string('Hello world!')
   3 >>> hello_list
   4 ['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']

Objects

As mentioned, Python supports multiple programming paradigms, one of those being object-orientation. Object-orientation allows collection of data into objects, or class instances. The data collected in objectes is referred to as fields or attributes, objects also store functions that usually performs actions on the attributes. Object-specific functions are called methods. Here is a small example of class definition, initialisation, and usage of a class that stores a list and a dictionary.

user@machine:~$ more StringDL.py
class StringDnLs:
    def __init__(self):
        self.str_list=[]
        self.str_dict={}

    def add2List(self,item):
        str_list.append(item)

    def add2Dict(self,key,val):
        str_dict[key]=val


/!\ Edit conflict - your version:



/!\ End of edit conflict



/!\ Edit conflict - other version:


   1 >>> import StringDL                  #import module
   2 >>> strdn = StringDL.StringDnLs()    #create instance of class StringDnLs
   3 >>> strdn.str_list                   #print StringDnLs field str_list
   4 []                                   # which is empty
   5 >>> strdn.add2List('a string')       # add a string to str_list using method addList(item)
   6 >>> strdn.str_list
   7 ['a string']

Built-in functions, loops, conditionals, assignment, and evaluation

Some of the conventions for Python syntax we have already seen. Useful built-in functions include len(), which returns the length of an object,

   1 >>> a_list = ['a','b','c']
   2 >>> len(a_list)
   3 3

and dir(), which returns a list of methods and attributes of an object.

   1 >>> dir(strdn)  #strdn - as defined under Objects
   2 ['__doc__', '__init__', '__module__', 'add2Dict', 'add2List', 'str_dict', 'str_list']

You can read more about the built-in functions here.

The for loop is used to iterate over an iterable object, e.g. a list. Depending on how the loop is formulated the loop variable will either be an item in the iterable object or an index.

   1 >>> a_list = ['a','b','c']
   2 >>> for item in a_list:       #iterating over the items
   3         print item
   4 
   5 ---- /!\ '''Edit conflict - your version:''' ----
   6 >>> range(0,10)
   7 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
   8 >>> range(10)
   9 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

The step size is 1 by default, but can be specified as the third argument:

   1 >>> range(0,10,2)
   2 [0, 2, 4, 6, 8]

Note that the step size must be an integer. If floating point steps are needed, the arange() function from the numpy package can be used. It is very similar to the range() function but accepts floating point arguments and returns array objects, which can be converted to lists using the method tolist()

   1 >>> from numpy import arange
   2 >>> arange(10)
   3 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
   4 >>> arange(0,5,0.5).tolist()
   5 [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]

while loops iterate until a condition is fulfilled.

   1 >>> a_list = ['a','b','c']
   2 >>> i = 0                  #assign value 0 to variable i
   3 >>> while i<len(a_list):   #as long as i is less than 3
   4         print a_list[i]    #print item at index i in a_list
   5         i += 1             #increment i by 1
   6 
   7 ---- /!\ '''End of edit conflict''' ----
   8 
   9 a
  10 b
  11 c
  12 
  13 ---- /!\ '''Edit conflict - other version:''' ----
  14 
  15 >>> for i in range(len(a_list)):   #iterating over indices
  16         print a_list[i]
  17 
  18 a
  19 b
  20 c

Note that range(integer_1,integer_2) is a built-in function that returns a list of integers ranging from the one integer, integer_1, to (but excluding) the other, integer_2. If the first argument is left blank, Python assumes integer_1 is 0. For example:

   1 >>> range(0,10)
   2 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
   3 >>> range(10)
   4 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

The step size is 1 by default, but can be specified as the third argument:

   1 >>> range(0,10,2)
   2 [0, 2, 4, 6, 8]

Note that the step size must be an integer. If floating point steps are needed, the arange() function from the numpy package can be used. It is very similar to the range() function but accepts floating point arguments and returns array objects, which can be converted to lists using the method tolist()

   1 >>> from numpy import arange
   2 >>> arange(10)
   3 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
   4 >>> arange(0,5,0.5).tolist()
   5 [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]

while loops iterate until a condition is fulfilled.

   1 >>> a_list = ['a','b','c']
   2 >>> i = 0                  #assign value 0 to variable i
   3 >>> while i<len(a_list):   #as long as i is less than 3
   4         print a_list[i]    #print item at index i in a_list
   5         i += 1             #increment i by 1
   6 
   7 a
   8 b
   9 c
  10 
  11 ---- /!\ '''Edit conflict - your version:''' ----
  12 
  13 ---- /!\ '''End of edit conflict''' ----


/!\ Edit conflict - other version:



/!\ Edit conflict - your version:



/!\ End of edit conflict


This implies that if the condition i<len(a_list) is never fulfilled the loop continues indefinitely, which it will.

Loops can be combined with conditional statements, where a block of code is executed if a statement is true, else another block is executed. The else block is optional, but must be the last option and no statement may follow on the same line.

   1 >>> a_list = ['a',1,'c',2]
   2 >>> for item in a_list:
   3         if type(item)==type(str()):  #check if item is of string type
   4             print item+' is string'
   5         else:                        #if not...
   6             print str(item)+' is not string'
   7 
   8 'a is string'
   9 '1 is not string'
  10 'c is string'
  11 '2 is not string'


/!\ Edit conflict - other version:



/!\ Edit conflict - your version:



/!\ End of edit conflict


If several options are possible the elif statement can be used.

   1 >>> for item in a_list:
   2 
   3 ---- /!\ '''Edit conflict - other version:''' ----
   4         if type(item)==type(str()):
   5             print item+' is string'
   6         elif type(item)==type(int()):
   7 
   8 ---- /!\ '''Edit conflict - your version:''' ----
   9         if type(item)==type(str()):  
  10             print item+' is string'
  11         elif type(item)==type(int()):                        
  12 
  13 ---- /!\ '''End of edit conflict''' ----
  14             print str(item)+' is int'
  15         else:
  16             print str(item)+' is unkown'
  17 
  18 'a is string'
  19 '1 is int'
  20 'c is string'
  21 '2 is int'
  22 
  23 ---- /!\ '''Edit conflict - other version:''' ----

You may have noticed it already, but it is necessary to point out the distinction between assignment and evaluation:


/!\ Edit conflict - your version:


}}}

You may have noticed it already, but it is necessary to point out the distinction between assignment and evaluation:


/!\ End of edit conflict


   1 >>> a = 'string' #assignment
   2 >>> a=='string'  #evaluation, is a equal to 'string'?
   3 True
   4 >>> a!='string'  #evaluation, is a not equal to 'string'?
   5 False

None: ScrumPy/Doc/Tutorial/PyIntro (last edited 2016-03-15 06:34:24 by david)