+ 2

Using if statement to check if a variable is of a certain type

Please any one help me

20th Jan 2023, 4:34 PM
CARTOON KIDS
CARTOON KIDS - avatar
4 Antworten
+ 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.
20th Jan 2023, 5:15 PM
JaScript
JaScript - avatar
+ 3
print(type(variableName))
20th Jan 2023, 4:44 PM
JaScript
JaScript - avatar
+ 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.
20th Jan 2023, 5:17 PM
JuZip
+ 1
How can I use if statement
20th Jan 2023, 4:46 PM
CARTOON KIDS
CARTOON KIDS - avatar