xxxxxxxxxx
# To check the type of a variable
print( type(price) )
# To check if a veriable is an instance of something (float in this case)
print( isinstance(price, float) )
xxxxxxxxxx
# To check if the type of o is exactly str, excluding subclasses of str:
if type(o) is str:
# Use isinstance to check if o is an instance of str or any subclass of str:
if isinstance(o, str):
xxxxxxxxxx
# Example variables
var1 = "Hello"
var2 = 123
var3 = True
var4 = 3.14
# Check type of variables
print(type(var1)) # <class 'str'>
print(type(var2)) # <class 'int'>
print(type(var3)) # <class 'bool'>
print(type(var4)) # <class 'float'>
xxxxxxxxxx
>>> i = 123
>>> type(i)
<type 'int'>
>>> type(i) is int
True
>>> i = 123.456
>>> type(i)
<type 'float'>
>>> type(i) is float
True