0
0
Pythonprogramming~20 mins

Type checking using type() in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Type Checking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A['int', 'int', 'str', 'list']
B['int', 'float', 'str', 'list']
C['int', 'float', 'string', 'list']
D['int', 'float', 'str', 'tuple']
Attempts:
2 left
💡 Hint
Use type() and __name__ attribute to get the type names as strings.
Predict Output
intermediate
1: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')
AInteger
BNot integer
CTypeError
DSyntaxError
Attempts:
2 left
💡 Hint
Check if x is exactly an int using type().
Predict Output
advanced
2: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}))
ADict with keys ['a', 'b']
BTypeError
COther type
DDict with keys a,b
Attempts:
2 left
💡 Hint
Check how keys() returns a view and how join expects strings.
Predict Output
advanced
2: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)
AFalse
BTrue
CTypeError
DSyntaxError
Attempts:
2 left
💡 Hint
type() checks exact type, not inheritance.
🧠 Conceptual
expert
3:00remaining
Understanding type() vs isinstance()
Which statement is true about type() and isinstance() in Python?
ABoth type() and isinstance() behave exactly the same for all objects.
Btype() returns True if an object is an instance of a class or its subclasses, isinstance() returns exact type only.
Ctype() returns the exact type of an object, while isinstance() returns True for an object and its subclasses.
Dtype() can check multiple types at once, isinstance() cannot.
Attempts:
2 left
💡 Hint
Think about inheritance and how these functions treat subclasses.