WOLFRAM LANGUAGE TUTORIAL

# Parts of Expressions

Since lists are just a particular kind of expression, it will come as no surprise that you can refer to parts of any expression much as you refer to parts of a list.

This gets the second element in the list

.

Out[1]= | |

You can use the same method to get the second element in the sum

.

Out[2]= | |

This gives the last element in the sum.

Out[3]= | |

Part

is the head.

Out[4]= | |

You can refer to parts of an expression such as just as you refer to parts of nested lists.

This is part

.

Out[5]= | |

This is part

.

Out[6]= | |

This extracts part

of the expression

.

Out[7]= | |

To see what part is

, you can look at the full form of the expression.

Out[8]//FullForm= |

| |

You should realize that the assignment of indices to parts of expressions is done on the basis of the internal Wolfram Language forms of the expression, as shown by FullForm. These forms do not always correspond directly with what you see printed out. This is particularly true for algebraic expressions, where the Wolfram Language uses a standard internal form, but prints the expressions in special ways.

Here is the internal form of

.

Out[9]//FullForm= |

| |

It is the internal form that is used in specifying parts.

Out[10]= | |

You can manipulate parts of expressions just as you manipulate parts of lists.

This replaces the third part of

by

. Note that the sum is automatically rearranged when the replacement is done.

Out[11]= | |

Out[12]= | |

This is the full form of

.

Out[13]//FullForm= |

| |

This resets a part of the expression

.

Out[14]= | |

Now the form of

has been changed.

Out[15]= | |

Part[expr,n] or expr[[n]] | the part of expr |

Part[expr,{n_{1},n_{2},…}] or expr[[{n_{1},n_{2},…}]] |

| a combination of parts of an expression |

Part[expr,n_{1};;n_{2}] | parts through of an expression |

ReplacePart[expr,n->elem] | replace the part of expr by elem |

Functions for manipulating parts of expressions.

"Manipulating Elements of Lists" discusses how you can use lists of indices to pick out several elements of a list at a time. You can use the same procedure to pick out several parts in an expression at a time.

This picks out elements 2 and 4 in the list, and gives a list of these elements.

Out[16]= | |

This picks out parts 2 and 4 of the sum, and gives a

*sum* of these elements.

Out[17]= | |

Any part in an expression can be viewed as being an argument of some function. When you pick out several parts by giving a list of indices, the parts are combined using the same function as in the expression.

This picks out parts 2 through 4 of the list.

Out[18]= | |