+ 2
Using if statement to check if a variable is of a certain type
Please any one help me
4 Réponses
+ 7
age = 21
if isinstance(age,int):
print("age is an integer")
Syntax isinstance()
isinstance(object, classtype)
Parameters
object: An object whose instance you are comparing with classtype. It will return true if the type matches otherwise false.
class type: A type or a class or a tuple of types and/or classes.
Return value:
It will return true if the object is an instance of classtype and false if not.
+ 3
print(type(variableName))
+ 3
What type would you test for?
If you have another variable y knowing that it's the type you want or creating it for that, you could use if type(x) == type(y):
Not very cool but should do the job.
+ 1
How can I use if statement