Skip to content

Expressions

Operators

Boolean Operators

LisPy version

(and expr1 expr2 ...)
Python version
expr1 and expr2 and ...

Binary Operators

LisPy version

(+ n1 n2 ...)
Python version
n1 + n2 + ...

Unary Operators

LisPy version

(- n)
Python version
-n

not

LisPy version

(not expr)
Python version
not expr

~

LisPy version

(~ n)
Python version
~n

Comparison Operators

==

LisPy version

(== expr1 expr2 expr3 ...)
Python version
expr1 == expr2 == expr3 == ...

>

LisPy version

(> expr1 expr2 expr3 ...)
Python version
expr1 > expr2 > expr3 > ...

!=

LisPy version

(!= expr1 expr2 expr3 ...)
Python version
expr1 != expr2 != expr3 != ...

<

LisPy version

(< expr1 expr2 expr3 ...)
Python version
expr1 < expr2 < expr3 < ...

<=

LisPy version

(<= expr1 expr2 expr3 ...)
Python version
expr1 <= expr2 <= expr3 <= ...

>=

LisPy version

(>= expr1 expr2 expr3 ...)
Python version
expr1 >= expr2 >= expr3 >= ...

is

LisPy version

(is expr1 expr2 expr3 ...)
Python version
expr1 is expr2 is expr3 is ...

is-not

LisPy version

(is-not expr1 expr2 expr3 ...)
Python version
expr1 is not expr2 is not expr3 is not ...

in

LisPy version

(in expr1 expr2 expr3 ...)
Python version
expr1 in expr2 in expr3 in ...

not-in

LisPy version

(not-in expr1 expr2 expr3 ...)
Python version
expr1 not in expr2 not in expr3 not in ...

:=

LisPy version

(:= name value)
Python version
(name := value)

Statements are not expressions

Statement forms (do, =, +=, if, return, def, ...) cannot appear inside an expression; the compiler reports an error. Use (:= name value) instead of =, (ife test then else) instead of if, and a tuple (sub (, a b c) -1) to sequence expressions.

lambda

LisPy version

(lambda [arg1 arg2 ...] body)
; or the built-in macro fn
(fn [arg1 arg2 ...] body)
Python version
lambda arg1, arg2, ...: body

if expression

LisPy version

(ife test then other)
Python version
then if test else other

Collection Literals

List

LisPy version

[item1 item2 ...]
Python version
[item1, item2, ...]

Dictionary

LisPy version

{key1 value1 key2 value2 ...}
Python version
{key1: value1, key2: value2, ...}

Set

LisPy version

{, item1 item2 ...}
Python version
{item1, item2, ...}

Tuple

LisPy version

(, item1 item2 ...)
Python version
(item1, item2, ...)

Comprehensions

List Comprehension

LisPy version

[(* 2 x) for x in (range 10) if (== (% x 2) 0)]
Python version
[2 * x for x in range(10) if x % 2 == 0]

Dictionary Comprehension

LisPy version

{(* 2 x) (* 3 x) for x in (range 10) if (== (% x 2) 0)}
Python version
{2 * x: 3 * x for x in range(10) if x % 2 == 0}

Set Comprehension

LisPy version

{, (* 2 x) for x in (range 10) if (== (% x 2) 0)}
Python version
{2 * x for x in range(10) if x % 2 == 0}

Generator Expression

LisPy version

((* 2 x) for x in (range 10) if (== (% x 2) 0))
Python version
(2 * x for x in range(10) if x % 2 == 0)

Comprehension Unpacking

Python 3.15+ (PEP 798). On an older Python these forms are a compile error naming the version they need.

LisPy version

[*L for L in lists]
(*L for L in lists)
{, *s for s in sets}
{**d for d in dicts}
[*L async-for L in lists]
Python version
[*L for L in lists]
(*L for L in lists)
{*s for s in sets}
{**d for d in dicts}
[*L async for L in lists]

await

LisPy version

(await expr)
Python version
await expr

yield

LisPy version

(yield expr)
Python version
yield expr

yield from

LisPy version

(yield-from expr)
Python version
yield from expr

Function Call

LisPy version

(func-name arg1 arg2 *args :kwarg1 value1 :kwarg2 value2 **kwargs)
Python version
func_name(arg1, arg2, *args, kwarg1=value1, kwarg2=value2, **kwargs)

Formatted String Literals

LisPy version

(= name "World")
f"Hello, {(* 2 name)}!"
Python version
name = "World"
f"Hello, {2 * name}!"
Replacement fields follow Python's rules: {{/}} are literal braces, !r/!s/!a conversions, format specs with nested fields, and {x=}. A field is ordinary code, so a string in it is written with plain quotes as in Python 3.12+ (f"{(.get d "k")}"), on every supported Python. The old spelling with \" (f"{(.get d \"k\")}") is deprecated: it still compiles, with a SyntaxWarning (shown by default, as for an invalid escape sequence in Python 3.12+), and a string opened with \" must be closed by \". LisPy version
f"{{literal}} {x!r:>{width}} {x=}"
Python version
f"{{literal}} {x!r:>{width}} {x=}"

Template String Literals

Python 3.14+ (PEP 750). The t prefix combines with r like f does (rt"...", Tr"...") and builds a string.templatelib.Template. Each interpolation's expression is the LisPy text inside the braces. On an older Python a t-string is a compile error naming the version it needs.

LisPy version

(= name "World")
t"Hello, {(* 2 name)!r:>12}!"
Python version
name = "World"
t"Hello, {2 * name!r:>12}!"  # interpolations[0].expression == "(* 2 name)"

Attribute

Two forms read an attribute: a dotted name, and (. obj attr).

Dotted Name

A name with dots in it is Python's attribute access. It chains, and it works anywhere a name does: as a value, a call head, an assignment or del target, and in import.

LisPy version

a.b.c
(obj.method arg :kwarg value)
(= obj.attr 1)
(+= obj.count 1)
(del obj.attr)
(import os.path)
Python version
a.b.c
obj.method(arg, kwarg=value)
obj.attr = 1
obj.count += 1
del obj.attr
import os.path
Every part is an ordinary name, so hyphens become underscores (a.b-c is a.b_c). The dotted name must be one token that starts with a name: a. and a..b are errors, a lone .a as a value is an error ((.a obj) is the method-call form below), and a.b[0] is refused (use (sub a.b 0)).

(. obj attr)

The object may be any expression, which a dotted name cannot be: a literal, call, subscription or other form on the left of the dot is not read as attribute access ((f).x and "s".upper are errors). (. obj attr) takes the attribute names after the object, so more than one chains them.

LisPy version

(. obj attr)
(. obj a b)
(. (f) attr)
(. (sub items 0) attr)
(. "text" upper)
Python version
obj.attr
obj.a.b
f().attr
items[0].attr
"text".upper
Each attribute must be a plain name, so (. obj (method)) is an error: to call a method on an expression use the method-call form below.

Method Call

LisPy version

(.method-name obj arg1 arg2 *args :kwarg1 value1 :kwarg2 value2 **kwargs)
Python version
obj.method_name(arg1, arg2, *args, kwarg1=value1, kwarg2=value2, **kwargs)

Subscription

Simple Subscription

LisPy version

(sub obj i1 i2)
Python version
obj[i1][i2]

Tuple Subscription

LisPy version

(sub obj (, i1 i2 ...))
Python version
obj[i1, i2, ...]

Slice

Slice Subscription

LisPy version

(sub obj [: start stop step])
Python version
obj[start:stop:step]

Slice with Emptiness

LisPy version

(sub obj [: stop])
(sub obj [: start _])
(sub obj [: start _ step])
Python version
obj[:stop]
obj[start:]
obj[start::step]