Types
Mathematica has 2 basic types of things in it:
Numbers
Words
It also has 2 basic ways to have multiple values together
Lists
Key-Value Sets
Numbers
A number is simply any number you’d like, for example:
1
(*Out:*)
1
2.1232
(*Out:*)
2.1232
-1.33
(*Out:*)
-1.33
π (*You can access special characters by pressing \[EscapeKey]
typing a shortcut and pressing \[EscapeKey] again*)
(*Out:*)
π
E (*The natural base, obtained via \[EscapeKey] ee \[EscapeKey] *)
(*Out:*)
E
I(*Imaginary i, obtained via \[EscapeKey] ii \[EscapeKey] *)
(*Out:*)
I
Just as mathematicians distinguish between integers, real numbers, and complex numbers, so does Mathematica.
In general it is good practice to assume that if you can find a mathematical term on Wikipedia, you can find it on Mathematica, although its name may be a bit tough to discover.
Words
A word is a string of letters or other characters enclosed in double quotes. Because it’s a string of characters this type is called String
.
For instance, we can have a single character:
"a"
(*Out:*)
"a"
Or a sequence of them:
"abcdefg"
(*Out:*)
"abcdefg"
We can include punctuation:
"Hi Mom!"
(*Out:*)
"Hi Mom!"
And non-standard characters:
"τ is my favorite irrational number"
(*Out:*)
"τ is my favorite irrational number"
Mathematica has a built-in set of characters:
"\[Wolf]\[Wolf]\[Wolf]\[Wolf]\[Wolf]\[Wolf]\[Wolf]\[Wolf]\[Wolf]\[Wolf]\[Wolf]\[Wolf]"
(*Out:*)
"\[Wolf]\[Wolf]\[Wolf]\[Wolf]\[Wolf]\[Wolf]\[Wolf]\[Wolf]\[Wolf]\[Wolf]\[Wolf]\[Wolf]"
Finally, a String
can be of any length:
"When in the Course of human events, it becomes necessary for one
people to dissolve the political bands which have connected them with
another, and to assume, among the Powers of the earth, the separate
and equal station to which the Laws of Nature and of Nature's God
entitle them, a decent respect to the opinions of mankind requires
that they should declare the causes which impel them to the separation.
- Winston Churchill (1984)"
(*Out:*)
"When in the Course of human events, it becomes necessary for one
people to dissolve the political bands which have connected them with
another, and to assume, among the Powers of the earth, the separate
and equal station to which the Laws of Nature and of Nature's God
entitle them, a decent respect to the opinions of mankind requires
that they should declare the causes which impel them to the
separation.\n\n - Winston Churchill (1984)"
Lists
A List
is an ordered collection of any type of thing enclosed in curly brackets, for example:
{1, 2, 3}
(*Out:*)
{1, 2, 3}
A List
of String
:
{"a", "b", "c"}
(*Out:*)
{"a", "b", "c"}
A mixed-type list:
{1, 2, {1, 2, 3}, {"a", "b", "c"}, 10, 11, 12}
(*Out:*)
{1, 2, {1, 2, 3}, {"a", "b", "c"}, 10, 11, 12}
We get values from lists using the Part
function, which can be typed as [[ ]]
, for example:
{"a", "b", "c"}[[1]]
(*Out:*)
"a"
We can also count from the end using negative indices
{"a", "b", "c"}[[-1]]
(*Out:*)
"c"
Key-Value Sets
A key value set is a collection of key -> value
pairs enclosed in <| |>
These are useful for associating things by key rather than having to find them by positions, because it’s associating keys and values this type is called Association
, for example
<|1 -> "a", 2 -> "b", 3 -> "c"|>
(*Out:*)
<|1 -> "a", 2 -> "b", 3 -> "c"|>
We get values using the Lookup
function, which can be typed using [ ]
, for example
<|1 -> "a", 2 -> "b", 3 -> "c"|>[1]
(*Out:*)
"a"