0
0
Pythonprogramming~20 mins

type() and isinstance() in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Type Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of type() with subclass instance
What is the output of this code?
Python
class Animal:
    pass

class Dog(Animal):
    pass

pet = Dog()
print(type(pet) == Animal)
ATrue
BFalse
CTypeError
DNameError
Attempts:
2 left
๐Ÿ’ก Hint
type() returns the exact class of the object, not its parent classes.
โ“ Predict Output
intermediate
2:00remaining
Using isinstance() with multiple classes
What will this code print?
Python
class Vehicle:
    pass

class Car(Vehicle):
    pass

class Bike(Vehicle):
    pass

obj = Car()
print(isinstance(obj, (Bike, Vehicle)))
ATypeError
BFalse
CTrue
DAttributeError
Attempts:
2 left
๐Ÿ’ก Hint
isinstance() returns True if the object is an instance of any class in the tuple.
โ“ Predict Output
advanced
2:00remaining
Difference between type() and isinstance()
What is the output of this code?
Python
class A:
    pass

class B(A):
    pass

obj = B()
print(type(obj) == A, isinstance(obj, A))
ATrue False
BTrue True
CFalse False
DFalse True
Attempts:
2 left
๐Ÿ’ก Hint
type() checks exact type, isinstance() checks inheritance too.
โ“ Predict Output
advanced
2:00remaining
What error does this code raise?
What error will this code raise when run?
Python
x = 5
print(isinstance(x, (int, str, list)))
print(type(x) in (int, str, list))
print(isinstance(x, int, str))
ATypeError
BSyntaxError
CFalse
DTrue
Attempts:
2 left
๐Ÿ’ก Hint
Check the number of arguments passed to isinstance().
๐Ÿง  Conceptual
expert
2:00remaining
Why use isinstance() over type()?
Which of the following is the best reason to prefer isinstance() over type() when checking an object's type?
Aisinstance() can check for subclasses, type() cannot
Btype() is slower than isinstance()
Ctype() can check multiple types at once, isinstance() cannot
Disinstance() only works with built-in types
Attempts:
2 left
๐Ÿ’ก Hint
Think about inheritance and flexibility.