This is documentation for Mathematica 4, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
Wolfram Research, Inc.

Using Previous ResultsMaking Lists of Objects

1.2.2 Defining Variables

When you do long calculations, it is often convenient to give names to your intermediate results. Just as in standard mathematics, or in other computer languages, you can do this by introducing named variables.

This sets the value of the variable x to be 5.

In[1]:= x = 5

Out[1]=

Whenever x appears, Mathematica now replaces it with the value 5.

In[2]:= x ^ 2

Out[2]=

This assigns a new value to x.

In[3]:= x = 7 + 4

Out[3]=

pi is set to be the numerical value of to 40-digit accuracy.

In[4]:= pi = N[Pi, 40]

Out[4]=

Here is the value you defined for pi.

In[5]:= pi

Out[5]=

This gives the numerical value of , to the same accuracy as pi.

In[6]:= pi ^ 2

Out[6]=

Assigning values to variables.

It is very important to realize that values you assign to variables are permanent. Once you have assigned a value to a particular variable, the value will be kept until you explicitly remove it. The value will, of course, disappear if you start a whole new Mathematica session.

Forgetting about definitions you made earlier is the single most common cause of mistakes when using Mathematica. If you set x = 5, Mathematica assumes that you always want x to have the value 5, until or unless you explicitly tell it otherwise. To avoid mistakes, you should remove values you have defined as soon as you have finished using them.

A useful principle in using Mathematica.

The variables you define can have almost any names. There is no limit on the length of their names. One constraint, however, is that variable names can never start with numbers. For example, x2 could be a variable, but 2x means 2*x.

Mathematica uses both upper- and lower-case letters. There is a convention that built-in Mathematica objects always have names starting with upper-case (capital) letters. To avoid confusion, you should always choose names for your own variables that start with lower-case letters.

Naming conventions.

You can type formulas involving variables in Mathematica almost exactly as you would in mathematics. There are a few important points to watch, however.

Some points to watch when using variables in Mathematica.

Using Previous ResultsMaking Lists of Objects