Expression Reference

How to represent the different kinds of Python expressions as Expression Trees

Operations

Boolean Operation - and

A 'and' boolean operation between True and False
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Boolean Operation - or

An 'or' boolean operation between True and False
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Boolean Operation - sequence of and

A sequence of 'and' boolean operations
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Boolean Operation - mixing and & or

A boolean operations mixing 'and', 'or' in its expression. Operator 'and' has the precedence
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Named Expression aka Walrus Operator

An assignment expression, aka Walrus Operator, from PEP 572.
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - sum

A binary operation performing the sum between 1 and 2
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - difference

A binary operation performing the difference between 1 and 2
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - multiplication

A binary operation performing the multiplication between 1 and 2
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - division

A binary operation performing the division between 2 and 1
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - floor division

A binary operation performing floor division between 4 and 2
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - modulo

A binary operation performing 10 modulo 2
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - exponentiation

A binary operation performing 2 to the power of 10
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - bitwise shift left

A binary operation performing 1 bit shift to left on number 2
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - bitwise shift right

A binary operation performing 1 bit shift to right on number 4
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - bitwise or

A binary operation performing a bitwise 'or' between 6 and 1
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - bitwise xor

A binary operation performing the bitwise exclusive or between 1 and 1
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - bitwise and

A binary operation performing the bitwise and between 15 and 15
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - 2 consecutive operations

2 consecutive sum binary operation
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Unary Operation - unary positive

A unary positive operation
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Unary Operation - unary negation

A negative unary operation
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Unary Operation - bitwise negation

The bitwise negation, part of unary operations
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Functions

Lambda - one parameter

Lambda, an anonymous function containing only one parameter and one expression
Specification
lambda

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Call expression

Function call expression with every possible type of parameter
Specification
function

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Lambda

Lambda - no parameters

Lambda, an anonymous function containing only one expression and no parameters
Specification
Lambda

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Lambda - full parameters kind

A Lambda function containing every single type of possible parameter
Specification
Lambda

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Generics

If Expression

If expression with 1 as the result if the condition is True, else 0
Specification
conditionals

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Collections

List - one element

List expression with only one single element
Specification
collections

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

List - no elements

List expression containing no elements
Specification
collections

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

List - multiple elements

List expression containing multiple elements
Specification
collections

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Tuple - one element

Tuple expression with only one single element
Specification
collections

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Tuple - no elements

Tuple expression containing no elements
Specification
collections

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Tuple - multiple elements

Tuple expression containing multiple elements
Specification
collections

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Dictionary - one element

Dictionary expression with only one single element
Specification
collections

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Dictionary - no elements

Dictionary expression with no elements
Specification
collections

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Dictionary - one kwarg argument

Dictionary expression with only one keyword argument
Specification
collections

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Dictionary - multiple arguments

Dictionary expression with multiple arguments and kwargs
Specification
collections

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Set - one element

Set expression with only one single element
Specification
collections

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Set - multiple elements

Set expression containing multiple elements
Specification
collections

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Comprehension

List Comprehension - one generator

A list comprehension with only one generator
Specification
comprehension

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

List Comprehension - generators and filters

A list comprehension with 2 generators and a filter
Specification
comprehension

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Set Comprehension - one generator

A set comprehension with only one generator
Specification
comprehension

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Dictionary Comprehension - one generator

A dictionary comprehension giving for each number in numbers the number as key and its power of 2 as value
Specification
comprehension

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Generator Expression - one generator

A generator expression with only one generator
Specification
comprehension

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Keyword

Await expression

Await expression returning number 404
Specification
keyword

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Yield expression

Yield expression returning number 404
Specification
keyword

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Yield From expression

Yield From expression yielding from variable mylist
Specification
keyword

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Comparison

Compare - equality

Compare expression using equality operator
Specification
compare

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Compare - difference

Compare expression using difference comparison operator
Specification
compare

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Compare - less than

Compare expression using less than comparison operator
Specification
compare

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Compare - less than or equal to

Compare expression using less than or equal to comparison operator
Specification
compare

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Compare - greater than

Compare expression using greater than comparison operator
Specification
compare

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Compare - greater than or equal to

Compare expression using greater than or equal to comparison operator
Specification
compare

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Compare - identity equality operator

Compare expression using the identity equality operator stating if the objects compared are the same
Specification
compare

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Compare - identity difference operator

Compare expression using the identity difference operator stating if the objects compared are not the same
Specification
compare

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Compare - 'in' membership operator

Compare expression using the 'in' membership operator to test if a sequence is present in an object
Specification
compare

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Compare expression using the 'not in' membership operator to test if a sequence is not present in an object

Compare expression
Specification
compare

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Constant

Constant expression

Constant literal expression of a integer number
Specification
constant

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Constant literal expression of a string
Specification
constant

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Attribute

Attribute

An object attribute expression
Specification
attribute

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Subscript

Subscript expression

A subscript expression of a sequence with the zero position being accessed
Specification
subscript

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Sliced Subscription

A subscription giving a slice of the sequence
Specification
slice

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Name

Name expression

A variable name being accessed
Specification
name

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

f-string

Formatted String - no formatted value

A simple formatted string used without inner formatted values
Specification
formatted string

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Formatted String - single formatted value

A formatted string with an inner formatted value
Specification
formatted string

Given the following snippet of code:

The Expression Tree for the highlighted expression is: