Each of the elements in the list only has a single set catogerisation ('Head'). As kglr says, using
Head /@ {1, 3.1, 2/3, x, 3 + I, "Yellow"}
Will show what each of these elements are catogerised as. In this case it is {Integer, Real, Rational, Symbol, Complex, String}, respectively.
Head:
Integer is only used for... well... integers.
Real is being used for floating point numbers.
Rational is being used for rational numbers being expressed as a
fraction without a floating point in and that is not equivalent to an integer.
Symbol is generally speaking, things that appear in blue in the
workbook. They start with letters and are followed by letters and numbers. Not inside of speech marks.
Complex is any mix of real (or the subsets of real) mixed with
an imaginary number. Here it is reading 'I' as the imaginary unit.
String is any sequence in speech marks, "Like so".
Essentially, the reason it is not working for you, is because you and Mathematica are using different definitions of real (and rational).
Mathematica is using real to mean the set of real numbers excluding integers and numbers expressed as fractions without a floating point in, for example. Whereas I assume you are looking for the full definition of real numbers which would include these subsets. The fact that it is returning only 1 element exactly for each of those patterns is just a coincidence that that list happens to have 1 of each Head listed in it.
Ways round it:
- Use kglr's head code above, to show all the heads for each thing in
your list, and just manually sort them.
- If you're just interested in Real's, you can do it with a single pattern search, by inserting floating points in the integers and rationals e.g.
Cases[{1., 3.1, 2./3, x, 3 + I, "Yellow"}, _Rational | _Real]
- Easiest is probably what you've shown in the question though and just search for Integer or Real or Rational.
As an aside, you can also use tests such as:
IntegerQ[3.1]
Hopefully this provides some clarity on the patterns, as requested. If there is a specific pattern condition you need, let me know and I can try to help putting something together with you if you would like. And welcome to Mathematica Stack Exchange!
Head /@ {1, 3.1, 2/3, x, 3 + I, "Yellow"}
and see Real >> Details, Rational >> Details and Integer >> Details in the docs. $\endgroup$Alternatives[]
(shorthand:|
). $\endgroup$