User Guide
Quick Reference
Modding
Development
User Guide
Quick Reference
Modding
Development
$ print 039;Numbers:039;, 1 + 1, -.5 + .5 Numbers: 2 0 $ print 039;Larger than 32-bit:039;, \ 0xffffffffff + 0xffffffffff Larger than 32-bit: 2.19902e+12 $ print 039;Text:039;, 039;Hello039; + 039;World039; Text: HelloWorld $ print 039;Array:039;, [1, 2, 3] + [039;a039;, 039;b039;, 039;c039;] Array: [ 1, 2, 3, a, b, c ] $ print 039;Dictionary:039;, \ {1:2, 3:4} + {039;a039;:039;b039;, 039;c039;:039;d039;} Dictionary: { 1: 2, 3: 4, a: b, c: d } $ print 039;Time:039;, Time() + 3600 Time: 2019-11-19 22:56:53.514
$ print 039;Numbers:039;, 1 - 1, -.5 - .5 Numbers: 0 -1 $ print 039;Larger than 32-bit:039;, 0xffffffffff - 0xffffffffff Larger than 32-bit: 0 $ print 039;Dictionary:039;, {039;a039;:039;A039;, 039;b039;:039;B039;} - 039;a039; Dictionary: { b: B } $ print 039;Time:039;, Time() - 3600, timedelta(Time(), Time() - 3600) Time: 2019-11-19 20:56:53.514 -3600
$ print 039;Numbers:039;, 1 * 2, 2.5 * 4, (2 + 6) * 45 Numbers: 2 10 360 $ print 039;Text:039;, 039;Word039; * 3, 039;Longword039; * 3.5 Text: WordWordWord LongwordLongwordLongwordLong
All numbers are internally floating point, so the result of integer division is not forced to be an integer.
$ print 039;Numbers:039;, 2 / 2, 10 / 4, 1 / 0 Numbers: 1 2.5 inf
The /
operator is also used for concatenating file paths. It automatically removes duplicate slashes at the end of a segment.
$ print 039;Path concatenation:039;, \ 039;folder039; / 039;filename.ext039;, \ 039;folder/039; / 039;filename.ext039; Path concatenation: folder/filename.ext folder/filename.ext
If the right-hand argument begins with a slash (absolute path), the left-hand argument is ignored as the absolute path overrides it.
$ print 039;Absolute:039;, 039;base/path039; / 039;/root/location039; Absolute: /root/location
Modulo converts number arguments to integers.
$ print 039;Numbers:039;, 7 % 3 Numbers: 1
%
is also used for C-like formatted string interpolation. The right-hand argument can be a single value or an array of values.
$ print 039;%s with "%3i" arguments. Hex %x, capital hex %X, float %f, precision %.10f.039; % [ 039;Formatted039;, 2, 240, 240, Pi, Pi] Formatted with " 2" arguments. Hex 0xf0, capital hex 0xF0, float 3.142, precision 3.1415926536. $ print 039;Width and precision: "%10.5f"039; % Pi Width and precision: " 3.14159"
Combined assignment operators work on existing variables only. They are considered expressions, which means they can appear in arguments like in the example below.
$ a = 1 $ print 039;a =039;, a += 3 a = 4 $ print 039;a =039;, a -= 3 a = 1 $ print 039;a =039;, a *= 100 a = 100 $ print 039;a =039;, a /= 10 a = 10 $ print 039;a =039;, a %= 3 a = 1 $ path = 039;/some/path/039; $ print 039;path =039;, path path = /some/path/ $ print 039;path =039;, path /= 039;filename.ext039; path = /some/path/filename.ext
The subscript operator []
works on arrays and strings to extract elements/characters.
$ transports = [039;planes039;, 039;trains039;, 039;automobiles039;, 039;bicycles039;] $ print transports[2] automobiles
Negative indices are counted from the end of the array.
$ print transports[-1] bicycles
The subscript operator is also used to access dictionaries by key.
$ xlat = {039;hi039;: 039;hello039;, 039;globe039;: 039;world039;} $ print xlat[039;hi039;], xlat[039;globe039;] hello world
Arrays can be nested.
$ matrix = [[1, 2], [3, 4]] $ print 039;Two-level indexing:039;, matrix[0][1], matrix[1][1] Two-level indexing: 2 4 $ matrix2 = [matrix, matrix] $ print matrix2[1][1][0], 039;from039;, matrix2 3 from [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 1, 2 ], [ 3, 4 ] ] ]
The slice operator is versatile tool for extracting a subset from an array or a text string. It takes a range of indices as argument. If the start of the slice range is omitted, the slice begins from the start of the array. Omitting the end of the range works similarly.
$ fullArray = [1, 2, 3, 4, 5, 6, 7] $ print 039;Sliced:039;, fullArray[2:4], fullArray[3:], fullArray[:3] Sliced: [ 3, 4 ] [ 4, 5, 6, 7 ] [ 1, 2, 3 ]
Slice arguments can be expressions.
$ print fullArray[0:len(fullArray)] [ 1, 2, 3, 4, 5, 6, 7 ]
The slice range may use negative indices, too.
$ print 039;Nega-sliced:039;, fullArray[-3:-1] Nega-sliced: [ 5, 6 ]
An optional third parameter can be provided to specify the step size within the slice range.
$ print 039;Stepping:039;, fullArray[::2], fullArray[1:6:2] Stepping: [ 1, 3, 5, 7 ] [ 2, 4, 6 ]
The step value can be negative to reverse the resulting slice.
$ print 039;Reverse slice:039;, fullArray[4:2:-1] Reverse slice: [ 5, 4 ]
The slice operator can therefore be used to easily reverse an array.
$ print 039;Reversing:039;, fullArray[::-1] [ 7, 6, 5, 4, 3, 2, 1 ]
In addition to arrays, slices work on text strings as well.
$ fullString = "abcdefg" $ print 039;Sliced:039;, fullString[2:4], fullString[3:], fullString[:3] Sliced: cd defg abc $ print 039;Nega-sliced:039;, fullString[-4:-2] Nega-sliced: de $ print 039;Stepping:039;, fullString[::2], fullString[1:6:2] Stepping: aceg bdf $ print 039;Reverse slice:039;, fullString[4:2:-1] Reverse slice: ed $ print 039;Reversing:039;, fullString[::-1] Reversing: gfedcba
The not
operator negates its argument.
$ print not True False $ print not False True
A non-empty text string is considered a boolean truth, so negation yields False.
$ print not 039;Is this true?039; False
and
and or
perform the corresponding logical operations.
$ print True and False False $ print True or False True
and
has higher precedence than or
.
$ print True or True and False True
Early termination applies to both operators.
# Helper function to see what gets evaluated. def showValue(a) print 039;called with039;, a return a end $ showValue(True) and showValue(True) called with True called with True $ showValue(False) and showValue(True) called with False $ showValue(False) or showValue(True) called with False called with True $ showValue(True) or showValue(True) called with True
The usual set of comparison operators is available.
$ print 039;Numbers:039;, 1 > 3, 1 < 3, 2 >= 2, 4 >= 2, \ 2 <= 2, 4 <= 2 Numbers: False True True True True False
Text string comparison is case sensitive.
$ print 039;Text:039;, "hello" > "world", "hello" < "world", \ 039;hello039; == 039;hello039; Text: False True True $ print 039;True/False:039;, True == True, True != False True/False: True True
With arrays, the comparison is done for each element starting from the first one, until a difference is found.
$ print 039;Arrays:039;, [1, 0] == [1, 0], [1, 0] < [1, 1], [1, 0] > [1, 1], [2, 0] > [1, 0], [2, 0] < [1, 0] Arrays: True True False True False
Arrays of different lengths are compared solely based on the lengths.
$ print [100] < [1, 2] True
The in
operator checks whether the left-hand operand can be found within the right-hand operand.
$ transports = [039;planes039;, 039;trains039;, 039;automobiles039;, 039;bicycles039;] $ print 039;bicycles039; in transports True $ print 039;jet039; in transports False
in
also works for text strings and dictionary keys.
$ print 039;x039; in 039;abc039; False $ print 039;b039; in {039;a039;: 1, 039;b039;: 2} True