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 .
In[1]:=
Click for copyable input
Out[1]=
You can use the same method to get the second element in the sum .
In[2]:=
Click for copyable input
Out[2]=
This gives the last element in the sum.
In[3]:=
Click for copyable input
Out[3]=
Part is the head.
In[4]:=
Click for copyable input
Out[4]=

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

This is part .
In[5]:=
Click for copyable input
Out[5]=
This is part .
In[6]:=
Click for copyable input
Out[6]=
This extracts part of the expression .
In[7]:=
Click for copyable input
Out[7]=
To see what part is , you can look at the full form of the expression.
In[8]:=
Click for copyable input
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 .
In[9]:=
Click for copyable input
Out[9]//FullForm=
It is the internal form that is used in specifying parts.
In[10]:=
Click for copyable input
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.
In[11]:=
Click for copyable input
Out[11]=
Here is an expression.
In[12]:=
Click for copyable input
Out[12]=
This is the full form of .
In[13]:=
Click for copyable input
Out[13]//FullForm=
This resets a part of the expression .
In[14]:=
Click for copyable input
Out[14]=
Now the form of has been changed.
In[15]:=
Click for copyable input
Out[15]=
Part[expr,n] or expr[[n]]the ^(th) part of expr
Part[expr,{n1,n2,}] or expr[[{n1,n2,}]]
a combination of parts of an expression
Part[expr,n1;;n2]parts through of an expression
ReplacePart[expr,n->elem]replace the ^(th) 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.
In[16]:=
Click for copyable input
Out[16]=
This picks out parts 2 and 4 of the sum, and gives a sum of these elements.
In[17]:=
Click for copyable input
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.
In[18]:=
Click for copyable input
Out[18]=