Adams Basic Package > Adams View > Adams Python Interface > Overview of the interface

Overview of the interface

This section provides an overview of the Python interface and the object model in place.

Introduction to Python

Python is an object oriented interpreted scripting language. This section contains a brief description of the language. Detailed reference can be obtained at https://www.python.org/

Python data types

The following are some of the built-in data types used in the Adams Python Interface. See here for more information.

Integer

Python's built-in integer type is based on the C long. The following creates a variable that refers to an integer object:
x = 42
Variables of other types (such as floats or strings) can be converted to integers using int(n).

Float

The built-in float type is based on the C double.
x = 3.14159
y = 0.0
Floats can also be declared using exponential notation:
exp = 1.122e-5

Sequences

Sequences are objects containing a series of objects. Python comes with a number of built-in sequence types, most importantly list, tuple, and string.

List

A list is a sequence that is mutable and heterogeneous. A mutable data type is one that can be modified. This means that it's possible to add, remove, or change the elements of a list. When we say that lists are heterogeneous, we mean that the elements of a list don't all need to be of the same type. A list could contain all integers, or all strings, but it could also contain some combination of floats, Booleans, strings, or whatever type of object you choose. Lists can contain any type of object.
intList = [1, 2, 3]
floatList = [1.2, 3.4, 5.6, 7.8]
stringList = ["thing_1", "thing_2"]
listList = [[1, 2, 3], ['a', 'b', 'c'], [4, 5, 6]]
You can refer to an individual item inside of a list using the item's index. Indices start at zero, and negative values count backwards from the end of the list.
>>> floatList[0]
1.2
>>> intList[-1]
3
>>> listList[2]
[4, 5, 6]
>>> listList[2][1]
5
You can modify a list by simply referring to a location in the list, and assigning it a value.
>>> myList = [0, 1, 2, 3]
>>> myList[1] = "foo"
>>> myList
[0, "foo", 2, 3]
The following example shows some useful methods that can be used on lists:
>>> myList = [8, 6, 7, 5, 3, 0, 9]
>>> myList.append(55)
>>> myList
[8, 6, 7, 5, 3, 0, 9, 55]
>>> myList.insert(2, 66)
>>> myList
[8, 6, 66, 7, 5, 3, 0, 9, 55]
>>> myList.reverse()
>>> myList
[55, 9, 0, 3, 5, 7, 66, 6, 8]
>>> myList.sort()
>>> myList
[0, 3, 5, 6, 7, 8, 9, 55, 66]

Tuple

Tuples are similar to lists in that they are heterogeneous. However, unlike lists, tuples are immutable, meaning they cannot be modified after they are created. This means that the methods shown above used to modify lists cannot be used on tuples. Attempting to modify a tuple will result in an AttributeError being raised. Tuples can be created as follows:
emptyTuple = ()
fullTuple = (0.1, 2.3, 4.5, 6.7)
As with any sequence type, individual elements can be accessed with the elements index.
>>> myTuple = (5, 4, 3, 2, 1)
>>> myTuple[0]
5
>>> myTuple[-2]
2

String

A string is an immutable sequence of characters. Strings can be defined using either single or double quotes.
str1 = "foo"
str2 = 'bar'

Common Operations on Sequences

The following operations can be used on any sequence.

Concatenation

You can concatenate sequences using the "+" operator.
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> x + y
[1, 2, 3, 4, 5, 6]
>>> a = "foo"
>>> b = "bar"
>>> a + " " + b
'foo bar'

Slicing

A slice, or subsequence, from m to n of a sequence can be obtained using the expression mySequence[m:n].
>>> myList = [0, 1, 2, 3, 4, 5, 6]
>>> myList[2:6]
[2, 3, 4, 5]
The first index has a default value of zero. The second index has a default value of the length of the sequence.
>>> myString = "This is a string."
>>> myString[:5]
'This '
>>> myString[8:]
'a string.'

Dictionary

Dictionaries map hashable values, which we call the dictionary's keys, to any other object type, which we call the dictionary's values. The keys of a dictionary can be of any immutable type, such as strings, integers, or tuples. Mutable types like lists cannot be used as keys. To create a dictionary, place a comma-separates list of key-value pairs inside of braces.
phone_numbers = {"Jack": "555-5555", "Jill": "444-4444"}
Accessing the values of a dictionary is similar to accessing the values of a sequence. The difference is that instead of using an index, we use a key.
>>> phone_numbers["Jack"]
"555-5555"

Control structures

Python does not provide any special character to denote bocks of code. Instead, control blocks are denoted by indentation. The number of tabs and spaces can vary from block to block, but code in the same block must have the same indentation.
# This is fine
if True:
    print("True")
else:
    print("False")
 
# This will generate an error
if True:
    x = "True"
    print(x)
else:
    x = "False"
  print(x)
 
Note:  
Any text written after a '#' is considered a comment, and is ignored by the interpreter.

Conditional Statements

temp = 72.3
if temp >= 82.2:
    print("Too hot!")
elif temp < 62.7:
    print("Too cold!")
else:
    print("Just right")

Loops

n = 12
d = 3
while n >= d:
    n = n - d
For loops can be used to iterate over any iterable object, such as a list. The built in range function returns a list of integers, and is commonly used in for loops.
for i in range(10):
    print(str(i) + " squared is " + str(i*i))
The break statement breaks out of the smallest containing loop.
for n in range(2, 20):
    for x in range(2, n):
        if x * x == n:
            print(str(n) + " is a perfect square")
            break
        elif x * x > n:
            print(str(n) + " is not a perfect square")
            break
The continue statement skips the rest of the loop and moves to the next iteration.
for x in range(25):
    if 24 % x != 0:
        continue
    print("%d divides 24" % x)

Functions

A function is a reusable block of code that can accept arguments and return values. A function definition starts with the def keyword. The following function accepts two arguments, subtracts the second value from the first, and then returns the difference:
def difference(a, b):
    c = a - b
    return c
When you call a function, write the arguments you wish to pass in parenthesis, separated by commas:
>>> difference(13.7, 9.5)
4.2
In the above example, we used the order of the arguments to dictate which value gets assigned to which variable in the function. These are called positional arguments. Python also lets you use keyword arguments. Keyword arguments must come after any positional arguments, and allow you to specify the variable the value should be assigned to:
>>> difference(a=12, b=7)
5
>>> difference(b=12, a=7)
-5
Python functions also allow for optional arguments.
def difference(a, b, abs=False):
    c = a - b
    if abs and c < 0:
        c *= -1
    return c
The above function can be called with either two or three arguments. If no value is provided for abs, it will take on the default value of False. When multiple optional values are provided, it is usually useful, and sometimes necessary, to pass the optional values via keyword arguments.