Making Tables

We’ve seen a few ways to make lists in the Wolfram Language. You can just type them in. You can use Range. And you can use functions like IntegerDigits. One of the most common and flexible ways to make lists is with the function Table.

In its very simplest form, Table makes a list with a single element repeated some specified number of times.

Table[5,10]

Table[x,10]

Table[{1,2},10]

Table[PieChart[{1,1,1}],3]

But what if we want to make a table where the elements aren’t identical? We can do that by introducing a variable, and then iterating over that variable.

Table[a[n],{n,5}]

Here’s how this works. To make the first element of the list, n is taken to be 1, so a[n] is a[1]. To make the second element, n is taken to be 2, so a[n] is a[2], and so on. n is called a variable because it’s varying as we make the different elements of the list.

Table[n+1,{n,10}]

Table[n^2,{n,10}]

With Table, you can make tables of anything.

Table[Range[n],{n,5}]

Table[Column[Range[n]],{n,8}]

Table[ListPlot[Range[10*n]],{n,3}]

Table[PieChart[Table[1,n]],{n,5}]

So far we’ve always used n as our variable. That’s a pretty common choice. But we can actually use any (lowercase) letter we want, or any combination of letters. All that matters is that wherever the variable appears, its name is the same.

Table[2^expt,{expt,10}]

Table[{x,x+1,x^2},{x,5}]

In Table[f[n],{n,5}], n takes on values 1, 2, 3, 4, 5. Table[f[n],{n,3,5}] says to start at 3 instead: 3, 4, 5.

Table[f[n],{n,10}]

Table[f[n],{n,4,10}]

Table[f[n],{n,4,10,2}]

The Wolfram Language emphasizes consistency, so for example Range is set up to deal with starting points and steps just like Table.

Range[4,10]

Range[4,10,2]

Range[0,1,0.1]

There are usually many ways to do the same thing in the Wolfram Language. For example, here’s how Table and Range can produce identical plots.

ListPlot[Table[x-x^2,{x,0,1,.02}]]

ListPlot[Range[0,1,.02]-Range[0,1,.02]^2]

Table always separately computes each entry in the list it generates—and you can see this if you use RandomInteger in Table.

Table[RandomInteger[10],20]

RandomInteger can actually also generate the list directly.

RandomInteger[10,20]

Make a list in which the number 1000 is repeated 5 times. »

Make a table of the values of n^3 for n from 10 to 20. »

Make a number line plot of the first 20 squares. »

Make a list of the even numbers (2, 4, 6, ...) up to 20. »

Use Table to get the same result as Range[10]. »

Make a bar chart of the first 10 squares. »

Make a table of lists of digits for the first 10 squares. »

Make a list line plot of the length of the sequence of digits for each of the first 100 squares. »

Make a table of the first digit of the first 20 squares. »

Make a list line plot of the first digits of the first 100 squares. »

Make a list of the differences between n^3 and n^2 with n up to 10. »

Make a list of the odd numbers (1, 3, 5, ...) up to 100. »

Make a list of the squares of even numbers up to 100. »

Create the list {-3,-2,-1,0,1,2} using Range. »

Make a list for numbers n up to 20 in which each element is a column of the values of n, n^2 and n^3. »

Make a list line plot of the last digits of the first 100 squares. »

Make a list line plot of the first digit of the first 100 multiples of 3. »

Make a list line plot of the total of the digits for each number up to 200. »

Make a list line plot of the total of the digits for each of the first 100 squares. »

Make a number line plot of the numbers 1/n with n from 1 to 20. »

Make a line plot of a list of random integers where the nth integer is between 0 and n. »

What does the {...} (list) in Table[n^2,{n,5}] mean?

A list is always a way of collecting things together. Here what it’s collecting is the variable n and its range 5. In the Wolfram Language, this kind of use of a list is called an iterator specification.

Why is the {...} (list) in Table[n^2,{n,5}] needed?

So one can easily generalize to multidimensional arrays, like Table[x^2-y^2,{x,5},{y,5}].

What are the constraints on the names of variables?

They can be any sequence of letters or numbers, but they can’t start with a number, and—to avoid possible confusion with built-in Wolfram Language functions—they shouldn’t start with a capital letter.

Why do you have to name a variable if the name never matters?

Good question! In Section 26, we’ll see how to avoid having named variables. It’s very elegant, but it’s a little more abstract than what we’re doing with Table here.

Can Range deal with negative numbers?

Yes. Range[-2,2] gives {-2,-1,0,1,2}. Range[2,-2] gives {}, but Range[2,-2,-1] gives {2,1,0,-1,-2}.

Tech Notes