Pyth reference and documentation This file is intended to be read with line wrapping. Tokens are given in the order: 1 character tokens, by code point, . tokens, by code point. Types: num = int, float cmp = complex seq = str, list, tuple lst = list, tuple idx = str, list, dict col = str, list, set, dict any = any expression (a for short) blk = a code block (any series of statements) ... = any number of arguments of any type. n:_ = n _ arguments. var = variable. Not evaluated. func = function. Not evaluated. func n = function with arity n. pfn = Preceding function of arity n. *F, for instance. l:_ = Lambda with lambda variable(s) _ pos = positive int. nneg = nonnegative int. neg = negative int. none = missing argument. c/n or col/num = col or num, where num is cast to list using U. s/n or seq/num = seq or num, where num is cast to list using U. = Type1 whose elements are of the for Type2. Shorthands: In the text, A refers to the first argument, B to the second, etc. If two type definitions overlap, more specific takes precedence. No operations modify the input unless specifically specified. x -> y -> z -> ... means on future / nested uses, the successive character is used. If there is an extra -> at the end, it is cyclic. Format: Char, Input types | Effect Tokens: 0 Leading 0 is a separate token, number zero. 0123456789. Numeric literals. Leading 0 ignored, trailing . often parses separately \n Print with trailing newline. Return input. Identity function. Suppresses printing. ! Logical negation. 0, 0.0, "", [], (), {}, set() -> True, else False. " Begin and end string literal. # Loop until error statement. On error, suppress and break out of loop. # Filter A(_, B ...) over C. Uses f underneath, B may refer to f's lambda var. # looks like a strainer. $ Begin and end Python literal. Counts as one token. Disabled in -s (online). % Modulo. Same as Python. % Every Ath element of B. B[::A] in Python. % Format string, B is list of arguments. % in Python. % Format string, B is only argument. % Apply modulo to real and imaginary parts separately. & If not A, return A. Else, return B. Short-circuiting. Same as Python and. ' Open the file or website given by the input string. Return list of lines if normal file or website. Return list of color triples for color images, and list of brightness values for grayscale images. ( <...> Tuple constructor. tuple(A) in Python. ) End function or statement. * Multiplication. * in Python. * Repetition. * in Python. * Repetition. * in Python. * Cartesian product. All pairs of A_elem, B_elem. + Addition. + in Python. + Concatenate. + in Python. + Concatenate. + in Python (But cast to list). + Append B to the end of A. + Prepend A to the beginning of B. + Append str(B) to the end of A. + Prepend str(A) to the beginning of B. + Union of A and B. + Union of A and {B}. , [A, B] - Subtraction. - in Python. - Filter A on absence from b. Uses not in. - Cast A to B's type, then filter on absence. - Cast B to A's type, then filter on absence. / Integer division. // in Python. / Count occurrences of B in A. / Count occurrences of B in A. : Slice of A from B to C. A[B:C] in Python. : Stepped range. Go from A to B with a step size of C. : Multiple assignment. For i in B, A[i] = C[i], If C runs out, it repeats. : Multiple assignment. For i in B, A[i] = C. : Regex substitution. In A, replace B with C. : 0| Regex search boolean. In A, return whether a match for B is found. : 1 Regex search. In A, return every match for B (non overlapping) : 2 In A, replace each match of regex B with its first capturing group. : 3 Split A on matches of regex B. Retain capturing groups. : 4 Return list of match and all capturing groups for each match of B in A. ; End all containing functions, statements, etc., if not inside a lambda function. ; Global value of innermost lambda's first variable. < Less than, numeric. < Less than, lexicographic. < Less than, lexicographic. < First B elements of A. A[:B] in Python. < All but last A elements of B. B[:len(B)-A] in Python. < Less than, absolute value/magnitude. < Less than, absolute value/magnitude. < Proper subset. = Assign A value B. deepcopy value before assignment. Return assigned value. = <...> Assign x value A(B,..), where x is the first variable after the =, or Q if there is none. The Python Y += 3 would be =+Y3. Return assigned value. > Greater than, numeric. > Greater than, lexicographic. > Greater than, lexicographic. > A at and after index B. A[B:] in Python. > Last A elements of B. B[len(B)-A:] in Python. > Greater than, absolute value/magnitude. > Greater than, absolute value/magnitude. > Proper superset. ? Ternary. A ? B : C in C-like languages. B if A else C in Python. @ Index into the sequence. Modular wrapping. A[B % len(A)] in Python. @ Index into the dict. A[B] in Python. @ Intersection. Filter B on presence in A. @ Root. A ** (1/B) in Python. A Assign the first value of the list to G and the second to H. Return the input. B Break. Ends surrounding statement. B Bifurcate. Return two element list [B, A(B, ...)]. C Character with code point A. If A is too large, convert to base 256 and turn each into a character. C C(floor(A)) C Complex conjugate. C Convert A to int using base 256 and code points. C Matrix transpose, truncating to the length of the shortest entry. D Define a function. Same as def A(B...) in Python. D Sort operator. Sort C with key A(_, B ...). Uses o underneath, B may refer to o's lambda var. E Evaluate input. Same as ast.literal_eval(input()) in Python. F Apply repeatedly. A(A(A(A(... B ... )))), C times. F Fold A over B, with first element as initial value. Same as .U bZ . On empty sequence, value is 0, unless A is *, in which case it is 1. F Apply A to B, with splatting. A(*B) in Python. F For A in range(int(B)): C F For A in B: C G Variable. Initialized to "abcdefghijklmnopqrstuvwxyz", alphabet. H Variable. Initialized to {}. (Hash table) I if A: B I Invariant. A(B, ...) == B. J Variable, Autoinitializing. J=A on first use and all uses before an assignment expression finishes. J Variable. After the assignment expression. K Variable, Autoinitializing. K=A on first use and all uses before an assignment expression finishes. K Variable. After the assignment expression. L def y(b): return A. y -> ' -> L Left map. n>=2. Map A(B, ..., _) over C, where _ is the lambda variable. Map uses m underneath, and B may refer to its lambda variable. M def g(G,H): return A. g -> n -> M Map. Map A(_) over B, where _ is the lambda variable. Map uses m underneath. M Map. Map A(*_) over B, where _ is the lambda variable. Map uses m underneath. Each element of B is splatted as the arguments to A. N Variable. Initialized to '"', quote mark. O If A > 0, random int below A. If A == 0, random float 0 to 1. If A < 0, random.seed(-A). O Random float 0 to A. O A multiplied by random float 0 to 1. O Random element of A. P List of prime factors of A with multiplicity in increasing order. Uses sympy if sympy is installed, trial division otherwise. Empty list if A is 0 or 1. P Returns whether -A is prime. P Complex phase of A. P All but the last element of A. A[:-1] in Python. Q Variable, Autoinitialized to eval(input()). R Return, used inside D. R Right map. n >= 2. Map A(_, B, ...) over C, where _ is the lambda variable. Map uses m underneath, and B may refer to its lambda variable. S Sort. sorted(A) in Python. S 1-indexed range. [1, 2, ... A]. S 1-indexed range up to floor of A. T Variable. Initialized to 10. (Ten) U Unary range. [0, 1, ... A-1]. range(A) in Python. U Reversed unary range. [A, A+1, ... -1]. range(A, 0) in Python. U Unary range of length. [0, 1, ... len(A)-1]. range(len(A)) in Python. V Shorthand for 'FNU'. N -> H -> b -> V Shorthand for 'FN'. N -> H -> b -> V Vectorized function. Equivalent to MC, W While loop. while A: B in Python. W Conditional application. If B, A(C, ...). Else, C. X Dictionary assignment. A[tuple(B)] = C in Python. X Dictionary assignment. A[B] = C X String assignment. Replace in A at index B with str(C). Modular indexing. X Sequence assignment. Replace in A at index B with C. Modular indexing. X Translate A from B to C. Modular indexing. X Translate A from B to B[::-1]. X In-place +=. B[A] += C. Modular indexing. Uses Pyth +. X B[tuple(A)] += C, or B[tuple(A)] = C if tuple(A) not in B. Uses Pyth +. X B[A] += C, or B[A] = C if A not in B. Uses Pyth +. X Insert into string. Insert str(C) into B at A. Y Variable. Initialized to [], empty list. Z Variable. Initialized to 0. (zero) [ <...> List constructor, unbounded number of arguments. [A1, A2, A3, A4 ... ], where A1, A2, .. are the arguments. Often used with ")". \ One character string. ] [A]. ^ Exponentiation. A ** B in Python. ^ Repeated Cartesian Product. itertools.product(A, repeat=B) in Python. _ Negation. -A in Python. _ Reversal. A[::-1] in Python. _ Swap keys and values. ` Representation. repr in Python. a Append. A.append(B) in Python. Return A. a Add. A.add(B) in Python. Return A. a Absolute difference. abs(A - B) in Python. b Variable. Initialized to '\n', newline. c Float division. A / B in Python. c Split. A.split(B) in Python. c Split on whitespace. A.split() in Python. c Chop A into pieces of length B. Last shorter if necessary. c Chop A into B pieces of equal sizes, initial pieces one longer if necessary. c Chop A at every location in B. c Chop A at every occurrence of B. d Variable. Initialized to ' ', space. e End. A[-1] in Python. e Imaginary part. A.imag in Python. e Last digit. A % 10 in Python. f Filter A(_) over B. T -> Y -> Z -> f First input where A(_) is truthy over [B, B+1, B+2, ...] f First input where A(_) is truthy over [1, 2, 3, 4...] g Greater than or equal. A >= B in Python. g Greater than or equal. A >= B in Python. g Superset. A.issuperset(B) g Inclusive slice. A[B-1:] in Python. h Head. A[0] in Python. h A + 1. i Convert A from base B to int. if A is '', return 0. int(A, B) in Python. i Convert A from base B to int. i GCD. j Convert A to base B as a list. j Join B on A. Convert elements of B to strings if needed. j Join A on newlines. j Join B on A. Convert A to a string, elements of B to strings. k Variable. Initialized to '', empty string. l len(A). l log base 2 of A. m Map A(_) over B. d -> k -> b -> n A != B o Sort B with key function A. sorted(B, key=A) in Python. N -> Y -> p Print A, with no trailing newline. Return A. q A == B r 0 A.lower() r 1 A.upper() r 2 A.swapcase() r 3 A.title() r 4 A.capitalize() r 5 string.capwords(A) r 6 A.strip() - Remove whitespace on both sides of A. r 7 Split A, eval each part. r 8 Run length encode A. Output format [[3, 'a'], [2, 'b'], [1, 'c'], [1, 'd']]. r 9 Run length decode A. Input format '3a2bcd' -> 'aaabbcd' r 9 Run length decode A. Input format [[3, 'a'], [2, 'b'], [1, 'c'], [1, 'd']]. r Range, half inclusive. range(A, B) in Python, or range(A, B, -1). r String range. r(C(A), C(B)), then convert each int to string using C. r r(B, A) s Concatenate. ''.join(A) s reduce on +, base case []. (Pyth +) s Real part. A.real in Python. s Floor to int. int(A) in Python. s Parse as int. "" parses to 0. int(A) in Python. t A - 1. t Tail. A[1:] in Python. u Reduce B from left to right, with function A(_, _) and C as starting value. G, H -> N, T ->. A takes current value, next element of B as inputs. Note that A can ignore either input. u Apply A(_, _) until a result that has occurred before is found. Starting value B. A takes current value, iteration number as inputs. v Eval. eval(A) without -s, ast.literal_eval(A) with -s (online). literal_eval only allows numeric, string, list, etc. literals, no variables or functions. w Take input. Reads up to newline. input() in Python 3. x Bitwise XOR. A ^ B in Python. x First occurrence. Return the index of the first element of A equal to B, or -1 if none exists. x First occurrence. Return the index of the first substring of A equal to B, or -1 if none exists. x All occurrences. Returns a list of the indexes of elements of B that equal A. x First occurence. Return the index of the first substring of A equal to str(B), or -1 if none exists. y Powerset. All subsets of A, ordered by length. y A * 2. z Variable. Autoinitialized to input(). { Deduplicate. Remove all duplicated elements in A, leaving the first occurrences in original order. | Logical or. If A, return A. Else, return B. Short circuiting. or in Python. } In. Returns a boolean for whether A is an element of B. in in Python. } In. Returns a boolean for whether A is a substring of B. in in Python. } Inclusive range. [A, A+1, ... B] ~ Assign A value B. Return original value of A. ~ <...> Assign x value A(B,..), where x is the first variable after the =. The Python Y += 3 would be =+Y3. Return original value of B. .! Factorial. *F works the same and should be prefered. .! Gamma function of A + 1. This is continuous with the factorial function. *F works the same and should be prefered. ." ." starts a packed string, terminated by " or end of file. Format is: first character is the lowest output character by code point, second is the highest. The rest of the string is the output string encoded as offsets from the lowest output character, treat as an integer in the minimal base, converted to base 256, as bytes in the rest of the string. To convert a desired output string to the format, use the following program: +++\.N++hSzeSzCi-RChSzCMz-hCeSzChSzN Null bytes, backslashes and double quotes will need to be escaped. .& Bitwise and. & in Python. .( Pop at location. A is mutated to remove the element at index B, which is returned. .) Pop last element. A is mutated to remove its final element, which is returned. .* Splat. A is treated as all of the arguments to the surrounding function. .+ Deltas. Difference between each consecutive pair of elements of A. .- Bagwise subtraction. For each element in B, remove its first occurrence in A from A and return. Non-mutating. ./ Partition. Return all divisions of A into disjoint substrings. ./ Integer partition. Return all sorted lists of positive integers that add to A. .: All substrings of A of length B. .: All substrings of A of length floor(len(A) * B) .: All substrings of A of positive length. .< Cyclically rotate A left by B elements. A[B:] + A[:B] in Python. .< Left shift. A shifted left by B, or A * 2 ^ B. A << B in Python. .> Cyclically rotate A right by B elements. A[-B:] + A[:-B] in Python. .> Right shift. A shifted right by B, or A / (2 ^ B). A >> B in Python. .? Else statement. Must come after I, F, V, W or #. else: in Python. .A Tests whether all elements of A are truthy. all in Python. .B Converts A to a binary string. .B Converts A to a binary string, treating the string as a base 256 integer. .C Combinations with replacement. All sorted lists of elements of A, of length B. .D Divmod. Returns [A / B, A % B]. .D Returns A with elements at indexes in B removed. .D Returns A with the element at index B removed. .E Tests whether any elements of A are truthy. any in Python. .E Rounds up. int(math.ceil(A)) in Python. .F String format. A.format(*B) in Python. .F String format. A.format(B) in Python. .H Converts A to a hexadecimal string. .H Converts A to a hexadecimal string, treating the string as a base 256 integer. .I Invert. A is a monotonic function from num to num. .I finds x such that A(x) = B. .M Filter B for elements x for which A(x) takes its maximal value. .N def :(N, T, Y): return _. The function is called ':'. : -> X -> .O Converts A to an octal string. .O Converts A to an octal string, treating the string as a base 256 integer. .O Average of A. If A is empty, 0.0 .P Permutations. All lists of elements of A of length B. .P Ath lexicographical permutation of B. Zero-indexed with modular indexing. .P nPr. Number of permutations of length B from A elements. .Q Evaluate all input. Take all input from STDIN, literal_eval each line, return list of the result. .R Round A to B decimal places. Convert to int if B == 0. .R Round A to the same number of decimal places as B is rounded to. .S Randomly shuffle A. If A is a list, mutate A to the result. .T Justified transpose. List of 0th elements of A's elements, list of 1st elements, ... Ignoring absences. .U Reduce with no starting value. Reduce from left to right, with initial value B[0]. A(_, _) takes inputs current value, next element of B. b, Z -> k, Y ->. .V Infinite for loop. b iterates over an infinite incrementing sequence starting at the input number. .V Infinite for loop. b iterates over an infinite lexicographically sequence of upper and lowercase strings starting at the input string. .W Functional while. While A(value) is truthy, value = B(value). value starts as C. return ending value. .Z Attempt to decompress A using zlip.decompress. If that fails, compress it with zlib.compress(A, 9). .[ Pad A on the right with a prefix of repeated copies of B to a length of the nearest multiple of C. .[ Pad C on the left with a prefix of repeated copies of A to a length of the nearest multiple of B. .[ Pad B on evenly on both sides with a prefix of repeated copies of C to a length of the nearest multiple of A. Ties broken to the right. .[ Pad list(A) on the right with copies of B as elements to a length of the nearest multiple of C. .[ Pad list(C) on the left with copies of A as elements to a length of the nearest multiple of B. .[ Pad list(B) on both sides with copies of C as elements to a length of the nearest multiple of A. Ties broken to the right. .^ Modular exponentiation. A ^ B % C, but faster. pow in Python. ._ All prefixes of A, length 1 to len(A). ._ Sign of A. 1 if A > 0, -1 if A < 0, 0 otherwise. .a Absolute value. abs in Python. .a L2 norm of A, treated as a vector. .a L2 norm of vector difference of A[0] and A[1]. .b Map A over B and C in parallel. .b Same as above, but splat each element of B into N and Y instead. .c nCr. Number of distinct B-element combinations of A distinct items. .c All B-element combinations of A, not necessarily distinct. .d 0 Current system time, in seconds since epoch. time.time() in Python. .d 1 Amount of time the current program has been running. time.process_time() in Python. .d 2 The current [year, month, day, hour, minute, second, microsecond] .d 3 The current year. .d 4 The current month. .d 5 The current day. .d 6 The current hour. .d 7 The current minute. .d 8 The current second. .d 9 The current weekday, zero indexed at Monday. .d Sleep for A seconds. .d Dictionary constructor. dict in Python. .e Enumerated map. Map A over elements of B (as b) and their indexes (as k). b, k -> Z, Y ->. .f First B inputs to A with truthy results, starting at C and counting up by 1. .f First B inputs to A with truthy results, starting at C and proceeding lexicographically throw upper and lowercase strings. .f First B inputs to A with truthy results, of 1, 2, 3, ... .g Group the elements of B by matching outputs of A, sorted by values of A. .i Interleave the elements of A and B. Non-truncating. .j A + B * j. j is imaginary unit. .j A + j. j is imaginary unit. .j j. j is imaginary unit. .l log A base B. .l log A base e. .m Filter B for elements x for which A(x) takes its minimal value. .n Flatten A to a unnested list. .n 0 Pi .n 1 e .n 2 2 ** 0.5 .n 3 Phi .n 4 Infinity .n 5 - Infinity .n 6 NaN .p All permutations of A. .q Exit. Immediately ends the program. .r Translate each element of A to its cyclic successor in B. .s Strip from A maximal prefix and suffix of A consisting of characters in B. .s Strip from A maximal prefix and suffix of A consisting of elements of B. .s Strip from A maximal prefix and suffix of A consisting of copies of B. .t Transpose A, padding lines with copies of B. .t Transpose, padding with spaces. .t 0 sin(A) .t 1 cos(A) .t 2 tan(A) .t 3 arcsin(A) .t 4 arccos(A) .t 5 arctan(A) .t 6 A radians in degrees. A * 180 / pi .t 7 A degrees in radians. A * pi / 180 .t 8 sinh(A) .t 9 cosh(A) .t 10 tanh(A) .t 11 arcsinh(A) .t 12 arccosh(A) .t 13 arctanh(A) .u Cumulative reduce. Reduce B from left to right, with function A(_, _) and C as starting value, and return all intermediate values, starting with C. A takes current value, next element of B as inputs. .u Cumulative fixed-point. Apply A(_, _) until a result that has occurred before is found. Starting value B. Return all intermediate results, starting with B. A takes current value, iteration number as inputs. .v Evaluates a pyth expression. Everything after the first expression will be ignored. .w <"http"-str> If B starts with "http", perform a request to the URL B, with contents consisting of A's elements joined on '=', joined on '&'. Return as a list of lines. .w <"http"-str> If B starts with "http", perform a request to the URL B, with contents consisting of A. Return as a list of lines. .w Write an image. Requires PIL. For color images, A must be a matrix of 3+ element lists. For grayscale, A must be a matrix of integers. File name is B, unless B has no '.', in which case it is B + ".png". If B does not exist, defaults to 'o.png'. .w Write/append to a file. Write A joined on newlines with a trailing newline to the file B, unless B has no '.', in which case B + '.txt', unless B does not exist, in which case 'o.txt' .w Write/append to a file. Write A with a trailing newline to the file B, unless B has no '.', in which case B + '.txt', unless B does not exist, in which case 'o.txt' .x Try - catch. A, unless A throws an error, in which case B. B is only evaluated if A throws an error. .z All input. Take all input from STDIN, return as list of lines. .{ Convert A to set. If any of A's elements are not hashable, convert all elements to tuples and convert to set. .{ Set containing A. .{ Empty set. .| Bitwise or. | in Python. .| Set union. Does not preserve input order.