WOLFRAM

Background & Context

    • Code representation format for the Python programming language.
    • PythonExpression supports Python numbers, strings, lists, Booleans and dictionaries.
    • Plain text format.
    • Python was first released as a programming language in 1991.

Import & Export

Import Elements

  • General Import elements:
  • "Elements" list of elements and options available in this file
    "Summary"summary of the file
    "Rules"list of rules for all available elements
  • Details of elements...
  • "Data"the literal Python expression
  • ImportString uses the "Data" element by default.
  • The symbol Null is represented as the Python value "None".
  • Not all Python built-in types are supported. The supported Python types are:
  • tupleListan ordered sequence of values
    dictionaryAssociationan association of keys and values
    intIntegerarbitrary-sized integer
    longIntegerarbitrary-sized integer
    floatRealreal-valued number
    complexComplexa complex number
    listLista sequence of values
    booleanTrue|FalseBoolean values
    stringStringa sequence of character values
    NoneNullnull value

Examples

open allclose all

Basic Examples  (6)Summary of the most common use cases

Import a simple string:

Out[1]=1

Import a list:

Out[1]=1

Export to a string:

Out[1]=1

Import a dictionary as a Wolfram Language list:

Out[1]=1

Export an Association as a Python dictionary:

Out[1]=1

Import the result of evaluating external code in Python:

Out[1]=1

Confirm it is the same as using the "ReturnType" of "Expression":

Out[2]=2

Possible Issues  (1)Common pitfalls and unexpected behavior

"PythonExpression" internally starts and stops a Python session every time ImportString is called, which takes a substantial amount of time:

Out[2]=2

When performance is a concern, manually manage a Python session using ExternalEvaluate:

Out[3]=3