Challenge - 5 Problems
Type Checking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of type() with mixed types
What is the output of this Python code?
Python
values = [42, 3.14, 'hello', [1, 2, 3]] result = [type(v).__name__ for v in values] print(result)
Attempts:
2 left
💡 Hint
Use type() and __name__ attribute to get the type names as strings.
✗ Incorrect
The list contains an int, a float, a string, and a list. type() returns the exact type, and __name__ gives the type name as a string. So the output is ['int', 'float', 'str', 'list'].
❓ Predict Output
intermediate1:30remaining
Type checking with type() and condition
What will be printed by this code?
Python
x = 10 if type(x) == int: print('Integer') else: print('Not integer')
Attempts:
2 left
💡 Hint
Check if x is exactly an int using type().
✗ Incorrect
x is assigned 10, which is an int. The condition type(x) == int is True, so it prints 'Integer'.
❓ Predict Output
advanced2:30remaining
Output of nested type() checks
What is the output of this code snippet?
Python
def check_type(val): if type(val) == list: return 'List of length ' + str(len(val)) elif type(val) == dict: return 'Dict with keys ' + ','.join(val.keys()) else: return 'Other type' print(check_type({'a':1, 'b':2}))
Attempts:
2 left
💡 Hint
Check how keys() returns a view and how join expects strings.
✗ Incorrect
val.keys() returns dict_keys(['a', 'b']), which can be joined by ',' to form 'a,b'. So the returned string is 'Dict with keys a,b'.
❓ Predict Output
advanced2:00remaining
Type checking with inheritance and type()
What will this code print?
Python
class Animal: pass class Dog(Animal): pass pet = Dog() print(type(pet) == Animal)
Attempts:
2 left
💡 Hint
type() checks exact type, not inheritance.
✗ Incorrect
pet is an instance of Dog, so type(pet) is Dog, not Animal. Therefore, type(pet) == Animal is False.
🧠 Conceptual
expert3:00remaining
Understanding type() vs isinstance()
Which statement is true about type() and isinstance() in Python?
Attempts:
2 left
💡 Hint
Think about inheritance and how these functions treat subclasses.
✗ Incorrect
type() returns the exact type of an object. isinstance() returns True if the object is an instance of the class or any subclass, making it more flexible for inheritance checks.