PythonExpression

Background

    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 and Export

  • ImportString["string","PythonExpression"]imports string into the corresponding Wolfram Language Expression.
  • ExportString[expr,"PythonExpression"] exports expr to a string representation suitable for use with the Python programming language.

Elements

  • General Import elements:
  • "Elements" list of elements and options available in this file
    "Rules"full list of rules for each element and option
    "Options"list of rules for options, properties and settings
  • 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

Basic Examples  (6)

Import a simple string:

In[1]:=
Click for copyable input

Import a list:

In[1]:=
Click for copyable input

Export to a string:

In[1]:=
Click for copyable input

Import a dictionary as a Wolfram Language list:

In[1]:=
Click for copyable input

Export an Association as a Python dictionary:

In[1]:=
Click for copyable input

Import the result of evaluating external code in Python:

In[1]:=
Click for copyable input

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

In[2]:=
Click for copyable input
Introduced in 2017
(11.2)