Skip to Content
Learn Python by Building Data Science Applications
book

Learn Python by Building Data Science Applications

by Philipp Kats, David Katz
August 2019
Beginner
482 pages
12h 56m
English
Packt Publishing
Content preview from Learn Python by Building Data Science Applications

The isinstance function

Similar to typeisinstance checks whether a variable is of a particular data type, structure, or class. This makes it very useful for testing purposes, or if you want to check arguments for the correct type:

>>> isinstance('Hello world', str)True

Here, we checked whether the value is a string—and indeed it is! We can also pass multiple variable types, in which case isinstance will return True if the value matches any of the given types:

>>> isinstance(1, (int, float))True

Note the second parenthesis, surrounding two value types we pass in. This parenthesis represents a tuple, one of the data structures, which we'll discuss in the next chapter.

Finally, there is dir, which is invaluable if we need to work with obscure, ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python for Data Science

Python for Data Science

Yuli Vasiliev
Introduction to Machine Learning with Python

Introduction to Machine Learning with Python

Andreas C. Müller, Sarah Guido

Publisher Resources

ISBN: 9781789535365Supplemental Content