Introduction to Python Programming

This section gives a brief introduction to Python programming in general, without reference to use with Itasca software. There are many online and published resources to find more information:

IPython Console

The best way to get started with Python programming is to experiment with the IPython pane in the program. A tab like the one shown below is accessed using the IPython item on the Python menu. The following examples can be run in this terminal.

  • Use the up and down arrow keys to access previous inputs.
  • Use the tab key for completion of Python names.
  • See for more information about this terminal.

Basic Types

Every value in Python has a specific type. The most common types are int, float and str. These represent an integer, a real number, and a character string, respectively. Below we assign some values to some variables.

size = 0.5
box_count = 10
first_name = "Fred"
last_name = 'Baker'

long_string = """
This is a multi-line string.
Triple quotes are used to define strings like this.

Lists, Tuples and Dictionaries

Three fundamental container types are commonly used in Python. These are the list, tuple and dict types.

Lists are created with the [   ] characters.

id_list = [1, 2, 3, 4]

Typing id_list in the IPython console will show the value of this list.



[1, 2, 3, 4]

Add an item to the end of a list with the append method.



[1, 2, 3, 4, 903]

Iterate over a list (or any sequence) with for and in.

for id_number in id_list:
    print ("id #: ", id_number)


id #:  1
id #:  2
id #:  3
id #:  4
id #:  903

Note that white space is used to indicate what code is included in the loop.

Get the length of a list with len.




Access individual elements of a list by index (using [   ]).




Access a sequence of elements (a slice) of a list using [#:#].



[2, 3]

The list index can be negative, which returns elements from the back of the list.




The value of an element of a list may be changed.

id_list[0] = 10.0

print (id_list)


[10.0, 2, 3, 4, 903]

Lists may store objects of different types.

id_list.append("this is a string")


[10.0, 2, 3, 4, 903, 'this is a string']


Tuples resemble but they cannot be changed after they are created being — in other words tuples are immutable.

parts = (1,2,3,4,5)


(1, 2, 3, 4, 5)


Python dictionaries are used for one-to-one mappings between objects (key/value pairs). The following line shows a dictionary literal that is assigned to the variable telephone_directory.

telephone_directory = {"Jim" : 125, "Steve": 128, "Bob" : 144}


{'Jim': 125, 'Steve': 128, 'Bob': 144}

Look up a value in a dictionary by supplying its key.




Assign new key/value pairs to an existing dictionary using [   ].

telephone_directory["Fred"] = 147
telephone_directory[(1,3)] = 0.22344


{'Jim': 125, 'Steve': 128, 'Bob': 144, 'Fred': 147, (1, 3): 0.22344}

Remove key, value pairs from a dictionary with del.


Iterate over a the key, value pairs of a dictionary as was shown with lists.

for key, value in telephone_directory.items():
    print ("found key: {} with value: {}".format(key, value))


found key: Jim with value: 125
found key: Steve with value: 128
found key: Fred with value: 147
found key: (1, 3) with value: 0.22344

Use the keys method to get a list of the dictionary keys.



dict_keys(['Jim', 'Steve', 'Fred', (1, 3)])

For more about dictionaries see: The dictionary section of the Python tutorial

Defining Functions

Functions are defined in Python with the def keyword.

def my_function(a, b):
    return a + b
my_function(1.0, 5)



Error Handling in Python.

The function call below contains a deliberate mistake to help show how Python handles errors.

my_function(1.0, "This is a Python string")


Traceback (most recent call last):
  File "<string>", line 8, in <module>
  File "", line 1, in <module>
  File "c:/src/ppfc/common/python/src/", line 60, in <module>
    my_function(1.0, "This is a Python string")
  File "c:/src/ppfc/common/python/src/", line 49, in my_function
    return a + b
TypeError: unsupported operand type(s) for +: 'float' and 'str'

The error occurs because float and str objects cannot be added in Python, so an error message occurs when the function is called. Causing an error in Python results in a traceback being printed. The traceback tells you the type of error encountered and the line and file the error occured in.


For advanced troubleshooting use the Python debugger. Typing debug in the IPython console activates the debugger.

Object-Oriented Programming

In Python all values (values are things variables can refer too) are objects. Objects are instances of a class. Objects have methods. For example, the long_string variable defined above (and repeated below) is an object of type str. The str methods may be called on the variable by attaching the method (split(), in this case) to the variable name.

long_string = """
This is a multi-line string.
Triple quotes are used to define strings like this.


['This', 'is', 'a', 'multi-line', 'string.', 'Triple', 'quotes', 'are', 'used', 'to', 'define', 'strings', 'like', 'this.']

The split method of the str object takes the original string, splits it at each space character, and returns the result as a list.

The numerical model objects of PFC, FLAC3D, and 3DEC are represented as objects in Python. For example there is a Ball object to represent PFC balls, or a Zone object to represent FLAC3D zones. As seen here with the built-in Python object type String, Itasca software model objects have properties and methods. Refer to the sections on using Python with each code for further information.

Interactive Documentation

The IPython console displays interactive completion and inline documentation to make programming easier. The following figure uses these features to show some of the available methods of string objects.

  • Pressing the tab key at the end of a partially written name will offer interactive completions. Pressing tab again will cycle through the available names. Pressing enter selects a completion.
  • Interactive documentation is shown when the ( character is typed after a function name.
  • Alternatively, adding a ? character to the end of any Python function, module or object displays inline documentation.

Example File

The source code for this example is