<< Main Manual

Tutorial #0.4. Loops and If Statements

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.

>>> # control structures influence the order in which commands are executed.
>>> #   Note: unlike most mainstream programming languages (i.e., C/C++, Java,
>>> #   Perl) Python is whitespace-sensitive. Specifically with regards to
>>> #   indentation. Usually this forces the programmer to write legible code
>>> #   and the interpreter is rather lax in terms of how much to indent as long
>>> #   as we're consistent. It's best to use 2-4 spaces to indicate that a set
>>> #   of commands is nested under and command (see below).
>>>
>>> # Loops
>>> i = 0
>>> while i < 10:
...    print i
...    i += 1     # adds 1 to the current value of i
...
>>> # the previous example is equivalent to
>>> for i in range(10):
...    print i
...
>>> # if/elif/else statements
>>> isTrue = False
>>> if isTrue:
...    # do something if a condition is true
...    pass    # pass is a special command and just used as a
...            # place-holder to maintain proper indentation
...
>>> if isTrue:
...    # do something if a condition is true
...    pass
... else:
...    # do something else if the original condition is NOT true
...    pass
...
>>> if isTrue:
...    # do something if a condition is true
...    pass
... elif isTrue == False:
...    # do something 'else if' the first condition is not true but
...    #  the second IS true
...    pass
... else:
...    # do something else if none of the above conditions are true
...    pass
...
>>> # 'break' commands pop you out of the inner most control structure
>>> #    usually when some condition is true as below
>>> for i in range(10):
...    print "breaking i1: " + str(i)
...    if i > 5:
...       break
...    print "breaking i2: " + str(i)
...
>>> # 'continue' commands skip the rest of the inner block and continue
>>> #   at the top most control structure (the difference between 'continue'
>>> #   and 'break' is subtle but important)
>>> for i in range(10):
...    print "continuing i1: " + str(i)
...    if i > 5:
...       continue
...    print "continuing i2: " + str(i)
...

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

<< Tutorial #0.3. Lists and Dictionaries
>> Tutorial #0.5. Functions

<< Main Manual