This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
script:walkthrough:basic_expressions [2019-11-19 10:58] – skyjake | script:walkthrough:basic_expressions [2019-11-20 05:30] (current) – skyjake | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | <- script: | ||
+ | ====== Basic expressions ====== | ||
+ | |||
+ | ===== Numbers and strings ===== | ||
+ | |||
+ | All numbers are internally stored as 64-bit floats. | ||
+ | |||
+ | $ print 5, 5.5, -3.141592657 | ||
+ | 5 5.5 -3.14159 | ||
+ | |||
+ | Hexadecimal numbers use the " | ||
+ | |||
+ | $ print 0x100, 0X123 | ||
+ | 256 291 | ||
+ | |||
+ | Underscores can be inserted in numbers at any points. This is only to improve readability for humans; the parser ignores them. | ||
+ | |||
+ | $ print ' | ||
+ | Underscores for readability: | ||
+ | | ||
+ | Text strings can use single, double, or triple quotes. Single and double are semantically equal. C-like escape sequences are supported, for example '' | ||
+ | |||
+ | $ print " | ||
+ | Hello World | ||
+ | | ||
+ | $ print """ | ||
+ | newlines.""" | ||
+ | I can " | ||
+ | newlines. | ||
+ | |||
+ | |||
+ | ===== Arrays and dictionaries ===== | ||
+ | |||
+ | Arrays are created using brackets around comma-separated elements. | ||
+ | |||
+ | $ print [1, 2, 3] | ||
+ | [ 1, 2, 3 ] | ||
+ | |||
+ | The elements of arrays can be of any type. | ||
+ | |||
+ | $ print [1, [2, 3], 4] | ||
+ | [ 1, [ 2, 3 ], 4 ] | ||
+ | |||
+ | Dictionaries contain key-value pairs. A dictionary is created with curly brackets. | ||
+ | |||
+ | $ print {' | ||
+ | { a: 10, b: 5 } | ||
+ | |||
+ | Keys and values can be of any type. | ||
+ | | ||
+ | $ print {' | ||
+ | { 1: [ b, { 5: 6, 6: 7 } ], [ array, as, key ]: oh my, a: b } | ||
+ | |||
+ | |||
+ | ===== Other value types ===== | ||
+ | |||
+ | Time values store a specific point in time. The built-in function '' | ||
+ | |||
+ | $ print 'The time is now:', Time() | ||
+ | The time is now: 2019-11-19 21: | ||
+ | |||
+ | |||
+ | ===== Constants ===== | ||
+ | |||
+ | There are reserved keywords for commonly used constants. | ||
+ | |||
+ | $ print True, False, None, Pi | ||
+ | True False (none) 3.14159 | ||
+ | |||
+ | |||
+ | ===== Built-in functions ===== | ||
+ | |||
+ | The built-in '' | ||
+ | |||
+ | len(" | ||
+ | len([1, 2, 3, 4]) ⇒ 4 | ||
+ | len({1:2, 3:5, 8:13}) ⇒ 3 | ||
+ | |||
+ | There are functions for extracting all the keys or all the values of a dictionary into an array. | ||
+ | |||
+ | dictkeys({1: | ||
+ | dictvalues({1: | ||
+ | |||
+ | The functions '' | ||
+ | |||
+ | $ print Text(123), Number(" | ||
+ | 123 123 | ||
+ | |||
+ | One can give a string argument to the '' | ||
+ | |||
+ | $ print " | ||
+ | Beginning of March 2011: 2011-03-01 00: | ||
+ | | ||
+ | $ print Time(" | ||
+ | 2000-01-02 03: | ||
+ | |||
+ | If the parsing fails, the resulting value represents an undefined point in time. A valid time evaluates to True and an undefined time evaluates to False when used as a boolean condition. | ||
+ | | ||
+ | $ print ' | ||
+ | Invalid time: (undefined Time) (undefined Time) | ||
+ | |||
+ | The '' | ||
+ | | ||
+ | $ print ' | ||
+ | Thousand seconds later: 1000 | ||
+ | |||
+ | The '' | ||
+ | |||
+ | $ a = [1, ' | ||
+ | | ||
+ | $ print a | ||
+ | [ 1, One, { 1: One } ] | ||
+ | | ||
+ | $ b = serialize(a) | ||
+ | | ||
+ | $ print b | ||
+ | (Block of 46 bytes) | ||
+ | | ||
+ | $ print deserialize(b) | ||
+ | [ 1, One, { 1: One } ] | ||
+ | |||
+ | The '' | ||
+ | |||
+ | $ a = eval(""" | ||
+ | | ||
+ | $ print a | ||
+ | argument | ||
+ | | ||
+ | $ eval(""" | ||
+ | Printed from eval(): argument |