To make my question clearer, I put an example in code, R
which is the language in which I have a little more experience. Suppose I have the simple variable "a" that is equal to 5. First of all I want to check if it is numeric using is.numeric:
a <- 5
is.numeric(a)
[1] TRUE
Now, to check if it is not numeric, we use a ! before the method and we get the following:
!is.numeric(a)
[1] FALSE
My question is if it is possible to do something like this in Python, "negating" a method like isinstance() that follows the structure:
isinstance(variable, tipo)
For example, if we apply negation to isinstance, I get:
a= 5
!isinstance(a, int)
"isinstance" no se reconoce como un comando interno o externo,
programa o archivo por lotes ejecutable.
I didn't find any useful information, that's why I pose my question. If there is material that I have overlooked, I would also greatly appreciate if you would let me know.
The negation operator in Python is
not
.The admiration
!
is not a valid symbol in Python, although you can use it in certain interactive interpreters, such as Jupyter Notebook or IPython. In them, it is used to invoke external commands of the operating system (shell), which explains the error message that you have seen (who does not recognize the shellisinstance
as a valid command ).The way to negate in Python is directly an
not
for example:What you have to keep in mind are some peculiarities of Python with respect to R.
is.numeric()
R is a higher level function thanisinstance()
Python, itisinstance()
checks an object to see if it matches a certain class, and in base Python, as in R, there are several classes that can be considered numerical (in fact there are more):In R:
in python
The trick in Python is to pass to
isinstance()
a list with the classes to check, obviously with those that we consider numerical. It could be something like this:isinstance(5.0, (int, float))
, however something more comfortable is to take advantage of the modulenumbers
to know the classes that we must consider numerical: