Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to check type in Python

Understanding Types in Python

When we talk about 'type' in programming, we are referring to the category of data that a variable belongs to. This can be an integer (a whole number), a float (a decimal number), a string (a sequence of characters), a list (an ordered collection of items), and so on.

In Python, everything is an object, and every object has a type. Knowing the type of an object is important because it determines what operations you can perform on it. For example, you can add two integers together, but you can't add an integer to a string.

The Importance of Checking Types

Imagine you're trying to make a smoothie. You've got your ingredients: strawberries, bananas, and milk. But instead of a blender, you realize you've got a toaster. No matter how hard you try, that toaster isn't going to give you a smoothie. It's just not the right type for the job.

Similarly, in Python, if you try to perform an operation on a data type that it's not designed for, you're going to run into problems. It's like trying to blend a smoothie in a toaster. That's why checking types is so crucial.

How to Check Types in Python

Python provides a built-in function called type() that allows you to check the type of any object. The syntax is simple:

type(object)

Here object is the variable or value that you want to check the type of. Let's look at some examples:

print(type(10))
print(type(3.14))
print(type('Hello, World!'))
print(type([1, 2, 3, 4, 5]))

In the above code, we're checking the type of an integer, a float, a string, and a list. If you run this code, it will output:

<class 'int'>
<class 'float'>
<class 'str'>
<class 'list'>

Diving Deeper: The isinstance() Function

While the type() function is great for checking the exact type of an object, Python also provides another function, isinstance(), which is often more useful.

The isinstance() function checks if an object is of a certain type or is a subclass of that type. A subclass is a class that inherits properties from another class, called its superclass.

Think of it like this: a strawberry is a type of fruit, but it's also a type of berry, which is a subclass of fruit. So if we were checking if a strawberry is a fruit, both type() and isinstance() would return true. But if we were checking if a strawberry is a berry, only isinstance() would return true.

Here's how you use isinstance():

isinstance(object, type)

Let's look at some examples:

print(isinstance(10, int))
print(isinstance(3.14, float))
print(isinstance('Hello, World!', str))
print(isinstance([1, 2, 3, 4, 5], list))

If you run this code, it will output:

True
True
True
True

Conclusion: Embrace the Flexibility of Python

Python is a dynamically typed language. This means that we don't need to declare the type of a variable when we create it, and we can even change the type of a variable later if we want to. This provides a lot of flexibility, but it also means that we need to be extra careful to ensure that we're using the right types for our operations.

In the world of Python, knowing your types is like knowing your ingredients when preparing a meal. You need the right types to create your code just as you need the right ingredients to prepare a dish. So don't try to blend your smoothie in a toaster. Use the type() and isinstance() functions to ensure you've got the right tools for the job and keep your coding kitchen running smoothly. Remember, in Python, as in cooking, understanding your components is the key to creating successful, error-free projects. Happy coding!