Differences between revisions 16 and 17
Revision 16 as of 2012-10-11 09:55:49
Size: 4845
Editor: hassan
Comment:
Revision 17 as of 2012-10-11 10:17:13
Size: 5122
Editor: hassan
Comment:
Deletions are marked like this. Additions are marked like this.
Line 131: Line 131:
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 objet that is not a list or a tuple can be used as a key. Here are some examples of 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 objet that is not a list or a tuple can be used as a key. Here are some examples of dictionaries in action:
Line 144: Line 144:
>>> dict_1.keys() #print keys of dict_1
['alfa','beta']
>>> dict_1.values() #print values
[1,2]
Line 147: Line 150:
== Modules and functions ==
Line 148: Line 152:
== Modules == {{{Python}}} can be used in interactive mode (as we have seen above), as well as in batch mode.
Line 152: Line 156:
== Loops and conditionals == == Loops, conditionals, assignment, and evaluation ==

Introduction to Python

The metabolic modelling 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

Numbers

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).

>>> n_int = 135
>>> n_int
135
>>> n_float = 10e-10
>>> n_float
1.0000e-9

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

>>> type(n_int)
<type 'int'>
>>>type(n_float)
<type 'float'>

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

>>> n_int2float=float(n_int)
>>> n_int2float
135.0
>>> type(n_int2float)
<type 'float'>   #n_int is still an integer

The common matematical 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.

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

val is true

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

>>> 

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.

>>> 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'>
>>> str(n_int)
'135'

Lists (and tuples)

Lists and tuples are collections of items. 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 excercises we will only use lists. An empty list can be created by assigning a pair of closed square brackets to a variable.

>>> empty_list=[]

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

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

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

>>> empty_list=['string 1']
>>> empty_list
['string 1']

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

>>> 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.

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

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

Dictionaries

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 objet that is not a list or a tuple can be used as a key. Here are some examples of dictionaries in action:

>>> dict_1 = {'alfa':1,'beta':2}  #create a dictionary
>>> keys = ['alfa','beta']
>>> vals = [1,2]
>>> dict_1 = dict(zip(keys,vals)) #create a dictionary from two lists
>>> dict_1
{'alfa':1,'beta':2}
>>> dict_1['alfa']                #access value '1' y key 'alfa'
1
>>> 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]

Modules and functions

Python can be used in interactive mode (as we have seen above), as well as in batch mode.

Objects

Loops, conditionals, assignment, and evaluation

None: Meetings/Delhi2012/Practicals/Practical_2/PyIntro (last edited 2012-10-12 13:53:26 by hassan)