Differences between revisions 2 and 10 (spanning 8 versions)
Revision 2 as of 2018-06-19 10:46:08
Size: 10665
Editor: mark
Comment:
Revision 10 as of 2018-06-29 03:44:18
Size: 10946
Editor: mark
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Line 3: Line 2:
 . Start !ScrumPy as described in the [[Meetings/C1netWork4/ScrumPyInstall#GettingStarted|installation instructions]]. Read the tutorial material below, and then replicate the python examples in the !ScrumPy command window (don't copy and paste!). Make sure you understand why you get the output you see, and test this by modifying some of these examples - you should be able to correctly predict the effects of these modifications.

=== Data Types ===
 . 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. We will start here by examining some of the common built-in types.

=== Strings ===
 . <<Anchor(String)>>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.

{{{#!python

>>> s_1 = 'another string' #create string
>>> s_2 = s_1[:7] #create new string of characters 0 to 7 in s_1
>>> s_2
'another'
>>> if s_2 in s_1: #check for membership of s_2 in s_1
        print 'true'

true
>>> type(s_1)
<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}}}).

{{{#!python
>>> n_int = 135
>>> n_int
=== Introduction/Aims of this practical ===
The aims of this practical are two-fold: firstly to introduce you to the different ways in which data is represented in python, and, in the second part how to use python to manipulate data. This practical is not about modelling ''per se'', but the material here is an essential pre-requisite for the modelling practicals that follow.

=== Instructions ===
Having started !ScrumPy, read the tutorial material below, and then replicate the python examples in the !ScrumPy command window (don't copy and paste!).

It is essential that you understand what each of these examples is doing, it's not enough to simply reproduce the ouptut shown here on your own laptop screens! You should therefore test this by modifying some of these examples - you should be able to correctly predict the effects of these modifications.

'''Note: '''In the examples that follow, ">>>" represents the python prompt which tells you that python is ready for you to type something, not something that you should type.

*** more here + exercises without answers + refs to L2 slides ***

== Part 1: How data is represented in Python ==
Data 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 data-types, but as we will see, these can be combined to build a python representation of whatever it is that we are studying. The purpose of !ScrumPy is to define such collections of simple data-types in a defined way (a ''data-structure'') particularly useful for metabolic modelling, and routines that act upon them. We will start by examining some of the common built-in types.The complete, official python documentation can be found [[https://docs.python.org/2/library/stdtypes.html|here]], although you might find some of it a bit technical, and it goes beyond what is needed for this course.

=== Viewing data ===
Simply typing a value at the prompt causes it to be printed on the screen:

{{{#!python
>>> "Hello"
'Hello'
}}}
if we assign a value to a variable, then typing the variable name will cause the value of the variable to be printed:

{{{#!python

>>> Greet = "Hello"
>>> Greet
'Hello'
}}}
Finally, if we use the built in "print" command, this will also cause the value to be printed on the screen. For some data the two methods may result in a different represesentation of the same value:

{{{#!python
>>> print Greet
Hello
}}}
'''Question:''' What is the difference between the two ways in which Greet is represented?

=== Numbers ===
Python has four built in ways of defining numbers: integer, long integer, floating point and complex numbers. For our purposes integers can be treated in exactly the same way as long integers, and we will not be considering complex numbers.I

Integers are whole, or counting, numbers and can be either positive or negative, they are represented as a sequence of digits which contains no other characters. Floating-point, or real, numbers contain a fractional component followng a decimal point. They can also be represented using the "e" notation, common to many other languages which represents "times 10 to the power of".

{{{#!python
>>> Count = 135
>>> Count
Line 31: Line 49:
>>> n_float = 10e-10
>>> n_float
1e-09
}}}
 . The type of a given data object can be checked using the built-in function {{{type()}}}.

{{{#!python

>>> type(n_int)
<type 'int'>
>>>type(n_float)
<type 'float'>
}}}
 . Floats and integers can be interconverted using the constructors {{{int()}}} or {{{float()}}}.
>>> Pi = 3.1415926
>>> Pi
3.14
>>> VeryBig = 1.1e16
>>> VeryBig
1.1e+16
>>> VerySmall = 1/VeryBig
9.090909090909091e-17
}}}
The common mathematical operators ({{{+,-,/,*}}}) work as expected, note that {{{x**y}}} means ''x''^y^. Numbers can also be compared using the common == (is equal), != (is not equal), < (is less than), <= (is less than or equal to), > (is greater than) and >= (is greater than or equal to). Some of these operators can also be used with othre types of data.

'''Question: '''What is the difference between 3/2, 3/2.0 and 3.0/2 ?
Line 54: Line 70:
 . The common mathematical operators ({{{+,-,/,*}}}) work as expected, note that {{{x**y}}} means ''x''^y^.  .
Line 57: Line 73:
 . Booleans are a subtype of integers. A boolean value is either {{{True}}} or {{{False}}}, and used for writing conditional statements, i.e. {{{if something is True, do something}}}. Booleans are used to indicate whether or not something is considered true, and can take one of exactly two values, True or False, they can be generated explicitly, or as the result of a comparison:
Line 61: Line 77:
>>> if val:
        print '
val is true'

'
val is true'

>>> val=
False
>>> if val:
        print 'val is true'

>>>

}}}
== Lists (and tuples) ==
 .
<<Anchor(List)>>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.

{{{#!python
>>> empty_list=[]
>>> val
True
>>> 3>=4
False
>>>
val = 3==4
>>>
val
False
}}}
'''Question:''' Explain line 6 in the example above.

=== Strings ===
<<Anchor(String)>>Strings are sequences of characters and used to represent arbitrary text. We saw one example in the first section of this practicatal **insert link**. They have many of the same properties as lists and tuples, described below, so we will return to them later.

=
== Lists (and tuples) ===
<<Anchor(List)>>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.

{{{#!python
>>> SomeList=[]
Line 81: Line 99:
>>> empty_list.append('string 1')
>>> empty_list
>>> SomeList.append('string 1')
>>> SomeList
Line 88: Line 106:
>>> empty_list=['string 1']
>>> empty_list
>>> SomeList=['string 1']
>>> SomeList
Line 92: Line 110:
{{{#!python
>>> empty_list = ['string 1','string 2','string 3']
>>> empty_list[0]
Or:

{{{#!python
>>> SomeList = ['string 1','string 2','string 3']
>>> SomeList[0]
Line 96: Line 116:
>>> empty_list[1] >>> SomeList[1]
Line 98: Line 118:
>>> empty_list[2] >>> SomeList[2]
Line 100: Line 120:
>>> empty_list[-1] >>> SomeList[-1]
Line 103: Line 123:
 . Items can be removed from a list using the {{{remove()}}} method.

{{{#!python
>>> empty_list.remove('string 1')
>>> empty_list
 . Question: What is the item at !SomeList[-2] ? !SomeList[-4] ?
 .

 . Indexing can be used to create a copy of part of a subset of a list, a process known as ''slicing''.

{{{#!python
>>>SomeList[1:3]
Line 110: Line 132:
 . 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.
 Subsets of lists (and strings) can be accessed using ''slicing'':

{{{#!python
>>> empty_list[1:3]
['string 3']
}}}
Line 120: Line 135:
>>> empty_list.index('string 2') >>> SomeList.index('string 2')
Line 123: Line 138:
 .
 . .
  . Items can be removed from a list using the {{{remove()}}} method.
 {{{#!python
>>> SomeList.remove('string 1')
>>> SomeList
['string 2', 'string 3']
}}}

==== Nested lists: ====
Line 126: Line 151:
>>> empty_list = ['string 1','string 2','string 3'] >>> SomeList = ['string 1','string 2','string 3']
Line 128: Line 153:
>>> empty_list.append(nested)
>>> empty_list
>>> SomeList.append(nested)
>>> SomeList
Line 131: Line 156:
>>> empty_list[3] >>> SomeList[3]
Line 133: Line 158:
>>> empty_list[3][0] >>> SomeList[3][0]
Line 136: Line 161:
Note the syntax of line 8 above.

'''Question:''' Explain the syntax of line 8 - how could you access the item with the value of 3 using this notation?

=== Tuples ===
Tuples are very similar to lists, but with one important difference: ince a tuple is defined its contents cannot be changed, they are ''immutable. ''Tuples are created in a in the same way as lists, excet the round brackets "()", rather than square brackets "[ ]" are used.

{{{#!python

>>> SomeTuple = ("Item 1", "Item 2", "Item 3")
}}}
'''Exercise:''' Repeat the previous list examples with the tuple. What works and what doesnt? Explain.

=== Strings ===
Strings are in fact special instance of tuples: they contain only characters, are defined by enclosing them with quote marks, and have some extra methods associated with them specific to text manipulation.
Line 137: Line 178:
 . <<Anchor(Dictionary)>>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:  . <<Anchor(Dictionary)>>. Conceptually, dictionaries are quite similar to lists and tuples in that they contain collections of items, however, what makes them distinct is the fact that instead of using integers to refer to items (almost) anything can be used to refer to what is contained, this alos means that, incontrast to lists and tuples there is no implicit order of the items stored in a dictionary. The hace that we can use (almos)t anything to refer to an item gives great flexibilty; dictionaries and "dictianary-like" classes are used extensively in !ScrumPy. Dictionaries are created by specifying a sequence of key:value pairs inside a pair of braces "{}":
Line 141: Line 182:
>>> keys = ['alfa','beta']
>>> vals = [1,2]
>>> dict_1 = dict(zip(keys,vals)) #create a dictionary from two lists
>>> dict_1
{'alfa':1,'beta':2}
>>> print dict_1
{'beta': 2, 'alfa': 1}
Line 148: Line 186:
>>> dict_1.has_key('beta') #check that dict_1 has key 'beta'
True
>>> dict_1.keys() #print keys of dict_1
['alfa','beta']
>>> dict_1.values() #print values
[1,2]
Line 157: Line 189:
 . As with lists, dictionaries can contain ''nested'' dictionaries:  .
 .
. As with lists, dictionaries can be nested.
Line 168: Line 202:
== 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,

{{{#!python
>>> a_list = ['a','b','c']
>>> len(a_list)
3
}}}
 . and {{{dir()}}}, which returns a list of methods and attributes of an object.

{{{#!python
>>> dir(dict_1) # list attributes of dict_1
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']
}}}
 . You can read more about the built-in functions [[http://docs.python.org/2/library/functions.html|here]]. {{{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]
}}}
 . <<Anchor(Loop)>>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.
== Part 2: Loops and conditionals ==
=== Repetition ===
==== For loops ====
<<Anchor(Loop)>>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.
Line 212: Line 212:
 . {{{while}}} loops iterate until a condition is fulfilled.  . While loops
 .
 . {{{while}}} loops continue repeating as long as some condition (the ''loop invariant'') evaluates as True.
Line 266: Line 268:
 . You may have noticed it already, but it is necessary to point out the distinction between assignment and evaluation:

{{{#!python
>>> a = 'string' #assignment
>>> a=='string' #evaluation, is a equal to 'string'?
True
>>> a!='string' #evaluation, is a not equal to 'string'?
False
}}}

Practical 2: Common Python Data Structures and Functions

Introduction/Aims of this practical

The aims of this practical are two-fold: firstly to introduce you to the different ways in which data is represented in python, and, in the second part how to use python to manipulate data. This practical is not about modelling per se, but the material here is an essential pre-requisite for the modelling practicals that follow.

Instructions

Having started ScrumPy, read the tutorial material below, and then replicate the python examples in the ScrumPy command window (don't copy and paste!).

It is essential that you understand what each of these examples is doing, it's not enough to simply reproduce the ouptut shown here on your own laptop screens! You should therefore test this by modifying some of these examples - you should be able to correctly predict the effects of these modifications.

Note: In the examples that follow, ">>>" represents the python prompt which tells you that python is ready for you to type something, not something that you should type.

*** more here + exercises without answers + refs to L2 slides ***

Part 1: How data is represented in Python

Data 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 data-types, but as we will see, these can be combined to build a python representation of whatever it is that we are studying. The purpose of ScrumPy is to define such collections of simple data-types in a defined way (a data-structure) particularly useful for metabolic modelling, and routines that act upon them. We will start by examining some of the common built-in types.The complete, official python documentation can be found here, although you might find some of it a bit technical, and it goes beyond what is needed for this course.

Viewing data

Simply typing a value at the prompt causes it to be printed on the screen:

   1 >>> "Hello"
   2 'Hello'

if we assign a value to a variable, then typing the variable name will cause the value of the variable to be printed:

   1 >>> Greet = "Hello"
   2 >>> Greet
   3 'Hello'

Finally, if we use the built in "print" command, this will also cause the value to be printed on the screen. For some data the two methods may result in a different represesentation of the same value:

   1 >>> print Greet
   2 Hello

Question: What is the difference between the two ways in which Greet is represented?

Numbers

Python has four built in ways of defining numbers: integer, long integer, floating point and complex numbers. For our purposes integers can be treated in exactly the same way as long integers, and we will not be considering complex numbers.I

Integers are whole, or counting, numbers and can be either positive or negative, they are represented as a sequence of digits which contains no other characters. Floating-point, or real, numbers contain a fractional component followng a decimal point. They can also be represented using the "e" notation, common to many other languages which represents "times 10 to the power of".

   1 >>> Count = 135
   2 >>> Count
   3 135
   4 >>> Pi = 3.1415926
   5 >>> Pi
   6 3.14
   7 >>> VeryBig = 1.1e16
   8 >>> VeryBig
   9 1.1e+16
  10 >>> VerySmall = 1/VeryBig
  11 9.090909090909091e-17

The common mathematical operators (+,-,/,*) work as expected, note that x**y means xy. Numbers can also be compared using the common == (is equal), != (is not equal), < (is less than), <= (is less than or equal to), > (is greater than) and >= (is greater than or equal to). Some of these operators can also be used with othre types of data.

Question: What is the difference between 3/2, 3/2.0 and 3.0/2 ?

   1 >>> n_int2float=float(n_int)
   2 >>> n_int2float
   3 135.0
   4 >>> type(n_int2float)
   5 <type 'float'>   #n_int is still an integer

Boolean

Booleans are used to indicate whether or not something is considered true, and can take one of exactly two values, True or False, they can be generated explicitly, or as the result of a comparison:

   1 >>> val=True
   2 >>> val
   3 True
   4 >>> 3>=4
   5 False
   6 >>> val = 3==4
   7 >>> val
   8 False

Question: Explain line 6 in the example above.

Strings

Strings are sequences of characters and used to represent arbitrary text. We saw one example in the first section of this practicatal **insert link**. They have many of the same properties as lists and tuples, described below, so we will return to them later.

Lists (and tuples)

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 >>> SomeList=[]
  • Items can be appended to a list by using the append() method.

   1 >>> SomeList.append('string 1')
   2 >>> SomeList
   3 ['string 1']
  • A list can also be created and populated in one go.

   1 >>> SomeList=['string 1']
   2 >>> SomeList
   3 ['string 1']

Or:

   1 >>> SomeList = ['string 1','string 2','string 3']
   2 >>> SomeList[0]
   3 'string 1'
   4 >>> SomeList[1]
   5 'string 2'
   6 >>> SomeList[2]
   7 'string 3'
   8 >>> SomeList[-1]
   9 'string 3'
  • Question: What is the item at SomeList[-2] ? SomeList[-4] ?

  • Indexing can be used to create a copy of part of a subset of a list, a process known as slicing.

   1 >>>SomeList[1:3]
   2 ['string 2', 'string 3']
  • The index of a known item can be retrieved using the index() method.

   1 >>> SomeList.index('string 2')
   2 0
  • .
    • Items can be removed from a list using the remove() method.

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

Nested lists:

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

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

Note the syntax of line 8 above.

Question: Explain the syntax of line 8 - how could you access the item with the value of 3 using this notation?

Tuples

Tuples are very similar to lists, but with one important difference: ince a tuple is defined its contents cannot be changed, they are immutable. Tuples are created in a in the same way as lists, excet the round brackets "()", rather than square brackets "[ ]" are used.

   1 >>> SomeTuple = ("Item 1", "Item 2", "Item 3")

Exercise: Repeat the previous list examples with the tuple. What works and what doesnt? Explain.

Strings

Strings are in fact special instance of tuples: they contain only characters, are defined by enclosing them with quote marks, and have some extra methods associated with them specific to text manipulation.

Dictionaries

  • . Conceptually, dictionaries are quite similar to lists and tuples in that they contain collections of items, however, what makes them distinct is the fact that instead of using integers to refer to items (almost) anything can be used to refer to what is contained, this alos means that, incontrast to lists and tuples there is no implicit order of the items stored in a dictionary. The hace that we can use (almos)t anything to refer to an item gives great flexibilty; dictionaries and "dictianary-like" classes are used extensively in ScrumPy. Dictionaries are created by specifying a sequence of key:value pairs inside a pair of braces "{}":

   1 >>> dict_1 = {'alfa':1,'beta':2}  #create a dictionary
   2 >>>  print dict_1
   3 {'beta': 2, 'alfa': 1}
   4 >>> dict_1['alfa']                #access value '1' by key 'alfa'
   5 1
   6 >>> dict_1['gamma'] = 1          #add new key:value pair
   7 >>> dict_1['alfa'] = 'a'         #overwrite key:value pair
  • As with lists, dictionaries can be nested.

   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

Part 2: Loops and conditionals

Repetition

For loops

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
  • While loops
  • while loops continue repeating as long as some condition (the loop invariant) evaluates as True.

   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 
  12 
  13 >>> for i in range(len(a_list)):   #iterating over indices
  14         print a_list[i]
  15 
  16 a
  17 b
  18 c
  • 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'
  • If several options are possible the elif statement can be used.

   1 >>> for item in a_list:
   2 
   3         if type(item)==type(str()):
   4             print item+' is string'
   5         elif type(item)==type(int()):
   6             print str(item)+' is int'
   7         else:
   8             print str(item)+' is unkown'
   9 
  10 'a is string'
  11 '1 is int'
  12 'c is string'
  13 '2 is int'

None: Meetings/Nepal2018/Prac2 (last edited 2018-06-29 03:44:18 by mark)