How to | Use Brackets and Braces Correctly

Mathematica's rich syntax uses different kinds of brackets and braces; familiarity with these aspects lets you read and program efficiently in Mathematica.

Parentheses ( ), braces { }, and square brackets [ ] all have different meanings in Mathematica. The first two are sometimes called round brackets and curly brackets.

You use parentheses in Mathematica for grouping expressions and to determine the precedence of operations:

In[53]:=
Click for copyable input
Out[53]=
In[54]:=
Click for copyable input
Out[54]=
    

A list in Mathematica is represented by braces and is a collection of items referred to as elements.

Create a list of the first five positive integers:

In[5]:=
Click for copyable input
Out[5]=

Anything in Mathematica can be used in lists, including numbers, variables, typeset mathematical expressions, and strings:

In[17]:=
Click for copyable input
Out[17]=

Lists can contain other lists to create nested lists:

In[18]:=
Click for copyable input
Out[18]=

Square brackets are used in Mathematica to enclose the arguments of functions.

The functions Range, Sin, and N are used here with square brackets enclosing their arguments:

In[45]:=
Click for copyable input
Out[45]=
In[2]:=
Click for copyable input
Out[2]=
In[46]:=
Click for copyable input
Out[46]=

Mathematica uses double square brackets as the short form for the Part function, which is used to get parts of lists:

In[3]:=
Click for copyable input
Out[3]=
In[5]:=
Click for copyable input
Out[5]=
    

The various bracketing constructions can be used together.

Plot a function, with the range of the plot specified in a list:

In[48]:=
Click for copyable input
Out[48]=

The ability to use functions and lists together is seamlessly integrated in Mathematica. Plot two functions together—the pair of functions is in a list:

In[49]:=
Click for copyable input
Out[49]=
    

All bracketing characters must be balanced for Mathematica to evaluate an expression. When a bracketing character is unbalanced, the Mathematica front end colors it purple:

Click for copyable input

Attempting to evaluate the expression produces an error:

In[6]:=
Click for copyable input

For more information on balancing brackets and braces, see How to: Balance Brackets and Braces.

New to Mathematica? Find your learning path »
Have a question? Ask support »