28 | Tests and Conditionals |
Is 2+2 equal to 4? Let’s ask the Wolfram Language.
Test whether 2+2 is equal to 4:
In[1]:=
Out[1]=
Not surprisingly, testing whether 2+2 is equal to 4 gives True.
We can also test whether 2×2 is greater than 5. We do that using >.
Test whether 2×2 is greater than 5:
In[2]:=
Out[2]=
In[3]:=
Out[3]=
If an element is less than 4, make it x, otherwise make it y:
In[4]:=
Out[4]=
You can also test for less than or equal using ≤, which is typed as <=.
If an element is less than or equal to 4, make it x; otherwise, make it y:
In[5]:=
Out[5]=
This makes an element x only if it is equal to 4:
In[6]:=
Out[6]=
You can test whether two things are not equal using ≠, which is typed as !=.
If an element is not equal to 4, make it x; otherwise, make it y:
In[7]:=
Out[7]=
It’s often useful to select elements in a list that satisfy a test. You can do this by using Select, and giving your test as a pure function.
Select elements in the list that are greater than 3:
In[8]:=
Out[8]=
Select elements that are between 2 and 5:
In[9]:=
Out[9]=
Beyond size comparisons like <, > and ==, the Wolfram Language includes many other kinds of tests. Examples are EvenQ and OddQ, which test whether numbers are even or odd. (The “Q” indicates that the functions are asking a question.)
4 is an even number:
In[10]:=
Out[10]=
Select even numbers from the list:
In[11]:=
Out[11]=
In this case, we don’t need the explicit pure function:
In[12]:=
Out[12]=
Select prime numbers:
In[13]:=
Out[13]=
Select elements of the list that are both even and greater than 2:
In[14]:=
Out[14]=
In[15]:=
Out[15]=
Select elements that are not either even or greater than 4:
In[16]:=
Out[16]=
There are many other “Q functions” that ask various kinds of questions. LetterQ tests whether a string consists of letters.
The space between letters isn’t a letter; nor is “!”:
In[17]:=
Out[17]=
Turn a string into a list of characters, then test which are letters:
In[18]:=
Out[18]=
Select the characters that are letters:
In[19]:=
Out[19]=
Select letters that appear after position 10 in the alphabet:
In[20]:=
Out[20]=
You can use Select to find words in English that are palindromes, meaning that they are the same if you reverse them.
In[21]:=
Out[21]=
MemberQ tests whether something appears as an element, or member, of a list.
5 appears in the list {1, 3, 5, 7}:
In[22]:=
Out[22]=
In[23]:=
Out[23]=
ImageInstanceQ is a machine-learning-based function that tests whether an image is an instance of a particular kind of thing, like a cat.
Test if an image is of a cat:
In[24]:=
Out[24]=
Select images of cats:
In[25]:=
Out[25]=
Here’s a geographic example of Select: find which cities in a list are less than 3000 miles from San Francisco.
Select cities whose distance from San Francisco is less than 3000 miles:
In[26]:=
Out[26]=
ab | test for equality | |
a<b | test whether less | |
a>b | test whether greater | |
a≤b | test whether less or equal | |
a≥b | test whether greater or equal | |
If[test,u,v] | give u if test is True and v if False | |
Select[list,f] | select elements that pass a test | |
EvenQ[x] | test whether even | |
OddQ[x] | test whether odd | |
IntegerQ[x] | test whether an integer | |
PrimeQ[x] | test whether a prime number | |
LetterQ[string] | test whether there are only letters | |
MemberQ[list,x] | test whether x is a member of list | |
ImageInstanceQ[image,category] | test whether image is an instance of category |
28.1Test whether 123^321 is greater than 456^123. »
28.2Get a list of numbers up to 100 whose digits add up to less than 5. »
28.3Make a list of the first 20 integers, with prime numbers styled red. »
28.5Make a list of the first 100 primes, keeping only ones whose last digit is less than 3. »
28.6Find Roman numerals up to 100 that do not contain “I”. »
28.7Get a list of Roman numerals up to 1000 that are palindromes. »
28.8Find names of integers up to 100 that begin and end with the same letter. »
28.9Get a list of words longer than 15 characters from the Wikipedia article on words. »
28.10Starting from 1000, divide by 2 if the number is even, and compute 3#+1& if the number is odd; do this repeatedly 200 times (Collatz problem). »
28.11Make a word cloud of 5-letter words in the Wikipedia article on computers. »
28.12Find words in WordList[ ] whose first 3 letters are the same as their last 3 read backward, but where the whole string is not a palindrome. »
+28.1Make a table of integers up to 25 where every integer ending in 3 is replaced with 0. »
+28.3Get a list of numbers up to 1000 that are equal to 1 mod both 7 and 8. »
+28.4Make a list of numbers up to 100, where multiples of 3 are replaced by Black, multiples of 5 by White and multiples of 3 and 5 by Red. »
+28.7Make a 100×100 array plot in which a square is black if the values of both its x and y positions do not contain a 5. »
Because = means something else in the Wolfram Language. You’ll get very strange results if you use = instead of == by mistake. (= is for assigning values of variables.) To avoid possible confusion, == is often read as “double equals”.
Because & means other things in the Wolfram Language. For example it’s what ends a pure function.
== is Equal, ≠ (!=) is Unequal, > is Greater, ≥ is GreaterEqual, < is Less, && is And, || is Or and ! is Not.
There’s an order of operations that’s a direct analog of arithmetic. && is like ×, || is like +, and ! is like −. So !p&&q means “(not p) and q”; !(p&&q) means “not (p and q)”.
What’s special about “Q” functions?
What are some other “Q” functions?
Is there a better way to find real-world entities with certain properties than using Select?
Yes. You can do things like Entity["Country", "Population"GreaterThan[]] to find “implicit entities”, then use EntityList to get explicit lists of entities.
- True and False are typically called Booleans in computer science, after George Boole from the mid-1800s. Expressions with &&, ||, etc. are often called Boolean expressions.
- In the Wolfram Language, True and False are symbols, and are not represented by 1 and 0 as in many other computer languages.
- If is often called a conditional. In If[test, then, else], the then and else aren’t computed unless the test says their condition is met.
- PalindromeQ directly tests if a string is a palindrome.
- In the Wolfram Language, x is a symbol (see Section 33) that could represent anything, so x==1 is just an equation, that isn’t immediately True or False. x===1 (“triple equals”) tests whether x is the same as 1, and since it isn’t, it gives False.