Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to check type of variable in Python

Getting to Know Variables

Imagine you're a detective. You have a piece of evidence in your hand but you're not sure what it is. Is it a hair strand, or a fiber from a piece of clothing? To solve the case, you need to first identify what this evidence is. Similarly, in the programming world, sometimes we need to identify what kind of data we're dealing with. This is where the concept of checking the type of variable comes into play.

Variables: The Data Containers

In Python, variables are like containers for storing data values. Think of them like different kinds of boxes you use for storing various items. For example, you might have a box for storing books (string type data), another for storing money (numeric type data), and so forth.

Here's an example of how you can create variables in Python:

book = "Harry Potter"
money = 1000

In the above example, book is a variable that holds the string "Harry Potter", and money is a variable that holds the number 1000.

The Need for Checking Variable Types

As you delve deeper into programming, you'll find situations where you need to know the type of a variable.

For example, you might be writing a program that accepts user input. If you're expecting a number but the user enters a string (like "hello"), your program could crash or behave unexpectedly if you don't handle this scenario.

To prevent such situations, you can check the type of variable beforehand and take appropriate actions.

How to Check Variable Types in Python

Python provides a built-in function, type(), for checking the type of a variable. It's like our detective's tool for identifying the piece of evidence.

Here's how you can use it:

book = "Harry Potter"
print(type(book))

When you run the above code, it will output: <class 'str'>. This means that the variable book is of the type 'str' (short for string).

Let's try with a numeric variable:

money = 1000
print(type(money))

This will output: <class 'int'>, meaning that the variable money is of the type 'int' (short for integer).

Using isinstance() for Type Checking

Python has another built-in function, isinstance(), that you can use for type checking. This function takes two parameters: the variable you want to check, and the type you want to check against.

Here's how to use isinstance():

book = "Harry Potter"
print(isinstance(book, str))

When you run the above code, it will output: True. This means that book is indeed a string. If book were not a string, isinstance() would return False.

Conclusion

To wrap up our detective story, identifying the type of a variable in Python is like identifying a piece of evidence in a case. You need to know what you're working with before you can proceed appropriately. Python's type() and isinstance() functions are the detective tools you need for this task.

So the next time you find yourself dealing with mysterious variables, remember: you have the tools to reveal their true identity. Happy programming!