How to | Use Brackets and Braces Correctly

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

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

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

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=
    

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

Create a list of the first five positive integers:

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

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

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

Lists can contain other lists to create nested lists:

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

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

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

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

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

In[7]:=
Click for copyable input
Out[7]=
In[8]:=
Click for copyable input
Out[8]=
    

The various bracketing constructions can be used together.

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

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

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

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

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

In[1]:=
Click for copyable input

Attempting to evaluate the expression produces an error:

In[2]:=
Click for copyable input

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