In this tutorial, you will learn how to see type of variable in Python.
In Python, variables can have different data types.
This variable type determines the type of data that can be stored in a variable.
Here are some of the common data types in Python:
Integers data type
This data type is used to represent whole numbers, such as 1, 2, 3, -4, -5, etc.
In Python, integer variables are created using the int() function.
Float data type
This data type is used to represent decimal numbers, such as 3.14, -2.5, etc.
In Python, float variables are created using the float() function.
Boolean data type
This data type is used to represent truth values, which can be either True or False.
In Python, boolean variables are created using the keywords True and False.
String data type
This data type is used to represent text, such as “Hello, World!”, “Python is awesome”, etc.
In Python, string variables are created using quotes, either single quotes (‘) or double quotes (“).
List data type
This data type is used to represent a collection of items that can be of any data type, such as [1, 2, 3], [“apple”, “banana”, “orange”], etc.
In Python, list variables are created using square brackets [].
Tuple data type
This data type is similar to lists, but the items cannot be changed once the tuple is created.
Tuples are created using parentheses ().
Dictionary data type
This data type is used to represent a collection of key-value pairs, such as {‘name’: ‘John’, ‘age’: 30}, {‘apple’: 1.5, ‘banana’: 2.0}, etc.
In Python, dictionary variables are created using curly braces {}.
Knowing the data type of a variable is important because it determines what operations can be performed on the variable and how it can be used in your code.
Now, let’s see how to see type of variable in python.
Method 1
How to see type of variable in python?
Checking the type of a variable in Python is comparatively simple as compared to other programming languauges.
You can see the type of a variable using the built-in type() function.
What is type () in Python?
type() is a built-in function in Python that is used to determine the data type of a variable or value.
The syntax for type() is as follows:
type(object)
where object is the variable or value whose data type you want to determine.
When type() is called with an argument, it returns the data type of that argument as a class object.
Let’s see how to use type() in python.
How to use type() in Python?
Let’s see the following example code:
# Example 1 - Integer
x = 5
print(type(x))
# Example 2 - String
y = "Hello, World!"
print(type(y))
# Example 3 - Float
z = 3.14
print(type(z))
Output
In each example, type() is used to determine the data type of the variable x, y, and z, respectively.
The function returns the data type as a class object.
Some use cases for the type() function
You can use the type() function in a variety of ways, such as:
- To check the data type of a variable before performing a certain operation on it.
- To debug your code and identify unexpected data types.
- To ensure that your code is working as intended by verifying the data types of your variables and values.
Method 2
The isinstance() method
isinstance() is a built-in Python function that is used to check whether an object belongs to a specific class or not.
It takes two arguments – the object that needs to be checked and the class that needs to be checked against.
The syntax for isinstance() is as follows:
isinstance(object, classinfo)
where object is the object or variable that needs to be checked.
And classinfo is either a class or a tuple of classes.
The function returns True if the object is an instance of the specified class or a subclass of the specified class, and False otherwise.
For example, the following code checks if the variable x is an instance of the class int:
x = 5
if isinstance(x, int):
print("x is an integer")
else:
print("x is not an integer")
Output
x is an integer
In this example, since x is an integer, the output is “x is an integer”.
Similarly, you can check if an object belongs to a specific class or a subclass of that class using isinstance().
This can be useful in many scenarios, such as type-checking in function arguments or validating user input.
The possibilities are limitless.
Practice what you learned with our free Online Python Compiler.
Discover more from Python Mania
Subscribe to get the latest posts sent to your email.