<< Main Manual

Tutorial #0.3. Lists and Dictionaries

TIP #1: The following examples are a crash course in Python programming showing the features that are most beneficial with the minimal amount of extraneous information. You can type the example code directly within PhysImage's 'Jython Console' or load the example files (*.py links below) directly from PhysImage as a demonstration, or test the code out with an independent installation of Python 2.7 on your preferred OS platform. These examples are independent of any specific PhysImage features and meant to be a succinct demonstration of very useful general Pythonic features.

TIP #2: The ">>> ", in the below example, is meant to represent PhysImage's 'Jython Console' prompt, while '#' in a line represents a comment (used to annotate Python code) and does not do anything. Therefore,
the text following the '#' is only for your benefit to understand the next bits of code and is not required to be typed in.

For a more expansive tutorial of the general Python programming language, see the Official Python Tutorial.

Example

The following code can be loaded within PhysImage from this file or typed in manually.

>>> # Data structures contain any of the basic types or can
>>> #    contain data structures as well
>>>
>>> ## Lists (similar to arrays in other languages)
>>> list = [23, 3.0, 'blah']
>>> print list
>>> print list[0]
>>> print list[1]
>>>
>>> # calculate the number of elements in the list with 'len(list)'
>>> print len(list)
>>>
>>> # remove and return an element with the index passed to the function 'pop(index)'
>>> list.pop(0)
>>> print len(list)
>>>
>>> list = []
>>> list.append(0.0)
>>> list.append(3.0)
>>> print list
>>> print list[0]
>>> print list[1]
>>> print len(list)
>>>
>>> # splitting strings into a list of tokens
>>> stringValue = "some text to be split"
>>>
>>> # the line below splits stringValue into a list of tokens based on
>>> #    whitespace (i.e., Tab-delimted file formats)
>>> tokens = stringValue.split()
>>> print tokens
>>>
>>> stringValue = "different,text,to,be,split,by,commas"
>>>
>>> # the line below splits stringValue into a list of tokens based on
>>> #    commas (i.e., CSV file formats)
>>> tokens = stringValue.split(',')
>>> print tokens
>>>
>>> # recombining split text
>>> tokens.pop(2)
>>> tokens.pop(2)
>>> print " ".join(tokens)
>>> print ", ".join(tokens)    # making a CSV line
>>> print "\t".join(tokens)    # making a tab-delimited line
>>>
>>> ## Dictionaries (aka associative arrays in other languages)
>>> dictionary = dict()
>>> dictionary['key1'] = "Some Value"
>>> dictionary['key2'] = 3.0
>>> print dictionary
>>> print dictionary['key1']
>>> print dictionary.keys()
>>> print dictionary.has_key('key2')
>>> dictionary = {'key2':3.0,'key1':'Some Value'}
>>> print dictionary
>>> print dictionary['key2']


After the data is in the WaveManager it got manipulated or plotted as a chart in the previously demonstrated ways.

<< Tutorial #0.2. Basic Variables Types -- Numbers and Booleans
>> Tutorial #0.4. Loops and If Statements

<< Main Manual