Is 2+2 equal to 4? Let’s ask the Wolfram Language.

2+2==4

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 >.

2*2>5

The function If lets you choose to give one result if a test is True, and another if it’s False.

If[2+2==4,x,y]

By using a pure function with /@, we can apply an If to every element of a list.

If[#<4,x,y]&/@{1,2,3,4,5,6,7}

You can also test for less than or equal using ≤, which is typed as <=.

If[#≤ 4,x,y]&/@{1,2,3,4,5,6,7}

If[#== 4,x,y]&/@{1,2,3,4,5,6,7}

You can test whether two things are not equal using ≠, which is typed as !=.

If[#≠ 4,x,y]&/@{1,2,3,4,5,6,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[{1,2,3,4,5,6,7},#>3&]

Select[{1,2,3,4,5,6,7},2≤#≤5&]

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.)

EvenQ[4]

Select[{1,2,3,4,5,6,7,8,9},EvenQ[#]&]

Select[{1,2,3,4,5,6,7,8,9},EvenQ]

IntegerQ tests whether something is an integer; PrimeQ tests whether a number is prime.

Select[{1,2,3,4,5,6,7,8,9,10},PrimeQ]

Sometimes we need to combine tests. && represents “and”, || represents “or” and ! represents “not”.

Select[{1,2,3,4,5,6,7},EvenQ[#]&&#>2&]

Select[{1,2,3,4,5,6,7},EvenQ[#]||#>4&]

Select[{1,2,3,4,5,6,7},!(EvenQ[#]||#>4)&]

There are many other “Q functions” that ask various kinds of questions. LetterQ tests whether a string consists of letters.

{LetterQ["a"],LetterQ["bc"],LetterQ["a b"],LetterQ["!"]}

LetterQ/@Characters["30 is the best!"]

Select[Characters["30 is the best!"],LetterQ]

Select[Characters["30 is the best!"],LetterQ[#]&&LetterNumber[#]>10&]

You can use Select to find words in English that are palindromes, meaning that they are the same if you reverse them.

Select[WordList[ ],StringReverse[#]==#&]

MemberQ tests whether something appears as an element, or member, of a list.

MemberQ[{1,3,5,7},5]

Select[Range[100],MemberQ[IntegerDigits[#],2]&]

ImageInstanceQ is a machine-learning-based function that tests whether an image is an instance of a particular kind of thing, like a cat.

ImageInstanceQ[,LinguisticAssistant]

Select[{,,,,,},ImageInstanceQ[#,LinguisticAssistant]&]

Here’s a geographic example of Select: find which cities in a list are less than 3000 miles from San Francisco.

Select[{LinguisticAssistant,LinguisticAssistant,LinguisticAssistant,LinguisticAssistant},GeoDistance[#,LinguisticAssistant]<LinguisticAssistant&]

Test whether 123^321 is greater than 456^123. »

Get a list of numbers up to 100 whose digits add up to less than 5. »

Make a list of the first 20 integers, with prime numbers styled red. »

Find words in WordList[ ] that both begin and end with the letter “p”. »

Make a list of the first 100 primes, keeping only ones whose last digit is less than 3. »

Find Roman numerals up to 100 that do not contain “I”. »

Get a list of Roman numerals up to 1000 that are palindromes. »

Find names of integers up to 100 that begin and end with the same letter. »

Get a list of words longer than 15 characters from the Wikipedia article on words. »

Starting 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). »

Make a word cloud of 5-letter words in the Wikipedia article on computers. »

Find 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. »

Find all 10-letter words in WordList[ ] for which the total of LetterNumber values is 100. »

Make a table of integers up to 25 where every integer ending in 3 is replaced with 0. »

Use Table and If to make a 5×5 array that is 1 on its leading diagonal, and 0 otherwise.  »

Get a list of numbers up to 1000 that are equal to 1 mod both 7 and 8. »

Make 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. »

Use Select to get a list of planets whose mass is larger than Earth. »

Make a 50×50 array plot in which a square at position i,j is black if Mod[i,j]==0. »

Make 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. »

Why does one test equality with == not =?

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”.

Why is “and” written as &&, not &?

Because & means other things in the Wolfram Language. For example it’s what ends a pure function.

How are ==, >, &&, etc. interpreted?

== is Equal, ≠ (!=) is Unequal, > is Greater, ≥ is GreaterEqual, < is Less, && is And, || is Or and ! is Not.

When do I need to use parentheses with &&, ||, etc.?

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?

They ask a question that normally has an answer of True or False.

What are some other “Q” functions?

NumberQ, StringContainsQ, BusinessDayQ and ConnectedGraphQ are a few.

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[(Quantity[(10^7), "people", people, "People"])]] to find “implicit entities”, then use EntityList to get explicit lists of entities.

Tech Notes