Function Reference

The [] symbol in subsection headings marks optional arguments. You can leave them out if the default value suits you.

The symbol >>> in the examples below marks the user's input, and the line below indicates the result of evaluating this expression.

The # symbol marks comments (explanations) to the code.

General

len(seq)

Returns the length of an array, the number of dictionary keys, or the length of a string.

>>> len([1, 2, 3])
3

>>> len('hello')
6

int(v)

Converts the argument to an integer.

>>> int(10.2)
10

>>> int('10')
10

>>> '10' + 2
102

>>> int('10') + 2
12

float(v)

Converts the argument to a fractional number.

>>> float('10.2')
10.2

str(v)

Converts the argument to a string.

>>> str(10.2)
10.2

pretty(container, [delimiter])

Formats a number, array, or dictionary using delimiter as the element separator.

>>> 12345678 | pretty
12 345 678

>>> 12345678 | pretty('\'')
12'345'678

>>> ['один', 'два', 'три'] | pretty
один, два, три

>>> d = dict()
>>> d['x'] = 10
>>> d['y'] = 20

>>> d | pretty
x: 10
y: 20

Arithmetic

round(v, [nd])

Rounds v to nd decimal places. If the second argument is not specified, it rounds up to an integer.

>>> round(10.2)
10

>>> round(10.992)
11

>>> round(10.992, 2)
10.99

floor(v)

Rounds the argument down to an integer.

>>> floor(10.9)
10

ceil(v)

Rounds the argument up to an integer.

>>> ceil(10.2)
11

abs(v)

Returns the absolute value

>>> abs(-5)
5

min(a, b, ...)

Returns the minimum argument.

>>> min(1, 2, 3)
1

max(a, b, ...)

Returns the maximum argument.

>>> max(1, 2, 3)
3

rand()

Returns a fractional number in the half-interval [0, 1).

>>> rand()
0.43463103446060180612420253964955918490886688232421875

rand(a, b)

Returns an integer in the interval [a, b].

>>> rand(1, 10)
10

rand(arr)

Returns a random array element.

>>> rand([1, 2, 3])
3

Working with strings

startswith(container, s)

Checks that the string container starts with the substring s.

>>> 'hello world'.startswith('hel')
True

endswith(container, s)

Checks that the string container ends with the substring s.

>>> 'hello world'.endswith('rld')
True

lower(s)

Converts a string to lower case. Convenient for later comparison with another string, case insensitive.

>>> 'hello' | lower
Hey

>>> 'hello' == 'hello'
False

>>> 'hello' | lower == 'hello'
True

upper(s)

Converts a string to upper case.

>>> 'HeLlO' | upper
HELLO

strip(s)

Removes extra spaces at the beginning and end of a string.

>>> '  hello     ' | strip
hello

split(s, [[sep=' ', [max_split=-1]])

Converts a string to an array, splitting it at the specified sep character (default is a space), you can also specify max_split - the maximum number of divisions.

>>> 'one two three' | split
['one two Three']

>>> 'one two three' | split(' ', 1)
['one two Three']

replace(s, old, new, [count=-1])

Replaces the substring old in the string s with the string new. By default (if count is not specified) all occurrences of the substring are replaced.

>>> 'Hi, Vasya'.replace('Vasya', 'Peter')
Hi Peter

>>> 'теxт, теxт, теxт'.replace('теxт', 'test')
'test, test, test'

>>> 'теxт, теxт, теxт'.replace('теxт', 'test', 1)
'test, теxт, теxт'

Regular Expressions

Regular expressions allow you to check if a string matches a specified pattern, or to search for a substring by a pattern in another string. This is a rather complicated, but very powerful mechanism.

You can read more here: https://ru.wikipedia.org/wiki/Regular_expressions

You can test your regular expressions here: https://regex101.com

match(s, pattern, [flags])

Finds a substring in the string s that matches the regular expression pattern. If there is no such substring, None is returned.

Supported flags:

  • i - case insensitive

  • m - multiline matching

  • s - . (dot) matches line breaks

>>> 'hello world' | match(r'world')
world

>>> 'hello world' | match(r'test')
None

# search for a number in a string
>>> 'top up, 500' | match(r'\d+')
500

# extract email from string
>>> 'My email: name@test.com' | match(r'\S+@\S+')
name@test.com

# search case sensitive (default)
>>> 'TEST' | match(r'test')
None

# case insensitive
>>> 'TEST' | match(r'test', 'i')
TEST

# ^$ means the entire string must match the filter
>>> 'top up, 500' | match(r'^refill, \d+$')
replenish, 500

# extra text does not match the filter
>>> 'fill up, 500 some extra text' | match(r'^refill, \d+$')
None

match_groups(s, pattern, [flags])

Finds a substring in the string s that matches the regular expression pattern. If there is no such substring, None is returned.

Unlike match() returns not a substring, but an array of groups. Moreover, the first element of the array is the entire found substring.

Supported flags:

  • i - case insensitive

  • m - multiline matching

  • s - . (dot) matches line breaks

# select 2 groups: number and currency
>>> 'top up with 100 rubles' | match_groups(r'(\d+) (\w+)')
['100 rubles', '100', 'rubles']

match_all(s, pattern, [flags])

Finds in the string s all substrings matching the regular expression pattern.

Supported flags:

  • i - case insensitive

  • m - multiline matching

  • s - . (dot) matches line breaks

# find all numbers in a string
>>> '1, 2, 3, 4, 5' | match_all(r'\d+')
['1', '2', '3', '4', '5']

Working with arrays

Operators

>>> arr = [1, 2, 3]

# get value by index
>>> arr[0]
one

# the last element of the array can be obtained like this:
>>> arr[-1]
3

# check if the value is in the array
>>> 3 in arr
True

# check for the absence of a value in the array
>>> 4 not in arr
True

# change value by index
>>> arr[0] = 100
>>> arr
[100, 2, 3]

# delete value by index
>>> del arr[0]
>>> arr
[2, 3]

Slices

>>> arr = [1, 2, 3, 4, 5]

# array elements starting at the specified index
>>> arr[2:]
[3, 4, 5]

# array elements up to the specified index
>>> arr[:2]
[12]

# every second element of the array
>>> arr[::2]
[1, 3, 5]

# flip the array
>>> arr[::-1]
[5, 4, 3, 2, 1]

push(arr, v)

Adds the element v to the end of the arr array.

>>> arr = [1, 2]
>>> arr.push(3)
>>> arr
[1, 2, 3]

pop(arr, [i])

Removes the element at index i. If i is not specified, then the last element of the array is removed.

>>> arr = [1, 2, 3]
>>> arr.pop()
>>> arr
[1, 2]

>>> arr = [1, 2, 3]
>>> arr.pop(0)
>>> arr
[2, 3]

insert(arr, i, v)

Inserts the value v in place of i.

>>> arr = [1, 2, 3]
>>> arr.insert(0, 100)
>>> arr
[100, 1, 2, 3]

remove(arr, v)

Removes the first occurrence of the element with value v.

>>> arr = [1, 2, 100, 3]
>>> arr.remove(100)
>>> arr
[1, 2, 3]

sorted(arr)

Returns a copy of the array, the values ​​in which are sorted in ascending order.

>>> arr = [2, 10, 1]
>>> arr | sorted
[1, 2, 10]

sorted(arr, key, reversed=False)

Returns a copy of the array in ascending order (or descending if reversed is True). key is a function that returns a value by which to sort.

# sort by field 'x'
>>> [{'x': 30}, {'x': 10}, {'x': 20}] | sorted(el => el['x'])
[{'x': 10}, {'x': 20}, {'x': 30}]

# sort by field 'x' backwards
>>> [{'x': 30}, {'x': 10}, {'x': 20}] | sorted(el => el['x'], True)
[{'x': 30}, {'x': 20}, {'x': 10}]

# just sort in descending order
[30, 10, 20] | sorted(None, False)
[30, 20, 10]

reversed(arr)

Returns a copy of an array whose values ​​are in reverse order.

>>> arr = [2, 10, 1]
>>> arr | reversed
[1, 10, 2]

enumerate(arr)

Returns an array of (index, value) pairs.

>>> arr = [2, 10, 1]
>>> arr | enumerate
[(0, 2), (1, 10), (2, 1)]

shuffle(arr)

Returns a copy of the array sorted randomly.

>>> arr = [2, 10, 1]
>>> arr | shuffle
[10, 1, 2]

index_of(arr, value)

Returns the index of the first array element equal to the specified value. If there is no element with the specified value in the array, None is returned.

>>> arr = [2, 10, 1]
>>> arr | index_of('10')
1

join(arr, [sep])

Concatenates the elements of an array into a single string using the sep separator.

>>> arr = [1, 2, 3]
>>> arr | join
1
2
3

>>> arr | join(':')
1:2:3

sum(arr)

Returns the sum of the array elements.

>>> [1, 2, 3] | sum
6

map(arr, l)

Returns a copy of the array resulting from applying the lambda function l to each of its elements.

# multiply each element by 2
>>> [1, 2, 3] | map(v => v * 2)
[2, 4, 6]

filter(arr, l)

Returns a copy of an array whose elements match the filter specified by the l lambda function.

# filter out values ​​greater than 5
>>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | filter(v => v > 5)
[6, 7, 8, 9, 10]

reduce(arr, l)

Combines the elements of an array into a single entity by calling the passed lambda function l on each element. The first argument of the lambda function is the result accumulated at the moment, the second is the next element of the array.

# sum value
>>> [1, 2, 3] | reduce((acc, v) => acc + v)
6

# product value
>>> [1, 2, 3] | reduce((acc, v) => acc * v)
6

# sum of rows
>>> [1, 2, 3] | reduce((acc, v) => str(acc) + v)
123

Working with dictionaries

Operators

# dictionary declaration
>>> cart = {'bread': 3, 'buckwheat': 10}

# get value by key
>>> basket['bread']
3

# check if the key exists in the dictionary
>>> 'bread' in basket
True

# change value by key
>>> basket['bread'] = 2
>>> shopping cart
{'bread': 2, 'buckwheat': 10}

# delete value by index
>>> del basket['buckwheat']
>>> shopping cart
{'bread': 2}

get(k, [default])

Returns the value given by the key k. If there is no such key in the dictionary, default is returned.

>>> cart = {'bread': 3, 'buckwheat': 10}
>>> cart.get('bread')
3
>>> cart.get('sausages', 100)
100

keys(d)

Returns an array of dictionary keys.

>>> cart = {'bread': 3, 'buckwheat': 10}
>>> shopping cart | keys
['bread', 'buckwheat']

values(d)

Returns an array of dictionary values.

>>> cart = {'bread': 3, 'buckwheat': 10}
>>> shopping cart | values
[3, 10]

map(d, l)

Returns the array resulting from applying the lambda function l to each key-value pair (k, v).

>>> shopping cart | map((k, v) => k + ': ' + v + ' pcs') | join
bread: 3 pcs
buckwheat: 10 pcs

sorted(d, key, reversed=False)

Returns a copy of the dictionary in ascending order (or descending if reversed is True). key is a function that returns a value by which to sort.

# sort by value
>>> {'Vasja': 10, 'Tom': 5} | sorted((k, v) => v)
{'Tom': 5, 'Vasja': 10}

# sort by value (desc)
>>> {'Vasja': 10, 'Tom': 5} | sorted((k, v) => v, True)
{'Vasja': 10, 'Tom': 5}

Last updated