Type Checking

If you are not sure what type a variable is, you can ask Python to tell you. For example, if you have a variable called x, you can ask Python what type it is by using the type() function:

x = 10
y = 20.0
z = 'hello'

print(type(x))
print(type(y))
print(type(z))

which will print:

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

Related to type() is the isinstance() function, which can be used to check if a variable is of a certain type:

print(isinstance(x, int))
print(isinstance(y, float))
print(isinstance(z, str))

print(isinstance(x, float))
print(isinstance(y, str))
print(isinstance(z, int))

which will print:

True
True
True
False
False
False