Data Types
Python has integer, long integer, floating-point, imaginary number, and sequence and
dictionary data types. Sequences are divided into mutable (or changeable) sequences
called lists, immutable sequences called tuples. Strings are special tuples of
characters that have their own syntax. Dictionaries are like sequences but are
indexed by non-numeric values. In addition, Python also has a special type called
None, which is used to indicate the absence of any value.
Python does not have a specific type for boolean (logical, or “true / false”) data. Tests, such as conditional expressions, which must evaluate to true or false, are conducted such that the result is false if the expression evaluates to None, integer or floating-point zero, or an empty sequence. Any other result is true. Python statements that generate Boolean information (such as the if statement) generate integer 0 for false and integer 1 for true. This becomes an issue in Jython which allows calling Java functions and methods which expect a Java boolean for input or generate a Java boolean for output. Jython maps these boolean values to integer 0 or 1. Documentation for the HEC-DSSVue API uses the term Constants.TRUE (1), or Constants.FALSE (0), or sometimes the shorthand “0/1”, for arguments (these are constants defined to 1 and 0, respectively, in hec.script), and “0/1” to specify that the return type is a Python integer, but its value is restricted to 0 or 1, corresponding to a Java boolean. The hec.script module supplies constants to use in these situations.
There are also situations regarding the HEC-DSSVue API where it is necessary or desirable to set a time-series value to “missing” or to test whether a time-series value is missing. The hec.script module also supplies a constant to use in these situations.
The currently-defined constants in the hec.script module are:
|
Constant |
Type |
Represents |
|
Constants.TRUE |
integer |
true |
|
Constants.FALSE |
integer |
false |
|
Constants.UNDEFINED |
floating-point |
missing data value |
It is recommended that these defined constants be used where applicable for portability and clarity.
|
Example: Variable Types
# set some integer values i = 0 j = 1 k = -10998 m = Constants.TRUE
# set a long integer n = 79228162514264337593543950336L
# set some floating-point values x = 9.375 y = 6.023e23 z = -7.2e-3 t = Constants.UNDEFINED
# set some strings string_1 = “abc” string_2 = ‘xyz’ string_3 = “he said \“I won’t!\”” string_4 = ‘he said “I will not!”‘ string_5 = “””this is a multi-line string”””
# set a tuple – tuples are contained within () tuple_1 = (1, 2, “abc”, x, None)
# set a list – lists are contained within [] list_1 = [1, 2, “abc”, x, tuple_1]
# set a dictionary, using key : value syntax # dictionaries are contained within {} dict_1 = {“color” : “red”, “size” : 10, “list” : [1, 5, 8]}
|
Indexing into sequence types is done using [i] where i starts at 0 for the first element. Subsets of sequence types (called slices) are accessed using [i:j] where i is the first element in the subset and j is the element after the last element in the subset. If negative numbers are used to specify and index or slice, the index is applied to the end of the sequence, where [-1] specifies the last element, [-2] the next-to last and so on. If i is omitted in slice syntax it defaults to 0. If j is omitted in slice syntax it defaults to the length of the sequence, so list_1 [0:len(list_1)] is the same as list_1[:]. Indexing into dictionaries is done using [x] where x is the key.
The number of elements in a sequence type or dictionary is returned by the len() function.
|
Example: Sequence Indexing and Slicing
string_4[3:5] # 4th & 5th elements list_1[-1] # last element list_1[2:-1] # 3rd through next-to-last element list_1[2:len(list_1)] # 3rd through last element (also list_1[2:]) dict_1[“size”] # value associated with “size” key i = len(list_1) # length of list_1
|