Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Check Data Types with type() and isinstance()
๐ Scenario: You are working on a simple program that needs to check the types of different pieces of data. This is like sorting your mail into letters, packages, and magazines before delivering them.
๐ฏ Goal: You will create a list of mixed data types, set a variable to check for a specific type, use type() and isinstance() to find which items match that type, and finally print the results.
๐ What You'll Learn
Create a list called items with mixed data types: 42, 3.14, 'hello', [1, 2, 3], and {'key': 'value'}.
Create a variable called check_type and set it to int.
Use a for loop with variables index and item to iterate over items with enumerate().
Inside the loop, use type() to check if item is exactly the check_type and print the index and item if true.
Also inside the loop, use isinstance() to check if item is an instance of check_type and print the index and item if true.
Print the results exactly as shown.
๐ก Why This Matters
๐ Real World
Checking data types is important when you receive mixed data, like user input or data from files, to make sure your program handles each piece correctly.
๐ผ Career
Many programming jobs require validating data types to avoid errors and to process data properly, especially in data science, web development, and software engineering.
Progress0 / 4 steps
1
Create a list of mixed data types
Create a list called items with these exact values in order: 42, 3.14, 'hello', [1, 2, 3], and {'key': 'value'}.
Python
Hint
Use square brackets [] to create a list and separate items with commas.
2
Set the type to check
Create a variable called check_type and set it to the type int.
Python
Hint
Use the keyword int without quotes to refer to the integer type.
3
Use a for loop with enumerate to check types
Use a for loop with variables index and item to iterate over items using enumerate(). Inside the loop, use type(item) == check_type to check if the item is exactly the type int. If true, print "type() match at index {index}: {item}". Also, use isinstance(item, check_type) to check if the item is an instance of int. If true, print "isinstance() match at index {index}: {item}".
Python
Hint
Use enumerate(items) to get both index and item in the loop.
Use f-strings to format the print output.
4
Print the final output
Run the program to print all matches found by type() and isinstance() checks. The output should show which items in items are integers.
Python
Hint
Check the console output to see the matches printed.
Practice
(1/5)
1. What does the type() function do in Python?
easy
A. It creates a new type.
B. It checks if an object is an instance of a class or its subclass.
C. It converts an object to a string.
D. It returns the exact type of an object.
Solution
Step 1: Understand the purpose of type()
The type() function returns the exact type of the given object, like int, str, or list.
Step 2: Compare with other options
isinstance() checks type inheritance, not exact type. Conversion or creation of types is not done by type().
Final Answer:
It returns the exact type of an object. -> Option D
2. Which of the following is the correct syntax to check if variable x is an instance of int?
easy
A. type(x) == isinstance(int)
B. isinstance(int, x)
C. isinstance(x, int)
D. type(x) is int()
Solution
Step 1: Recall the syntax of isinstance()
The correct syntax is isinstance(object, class_or_tuple). So, isinstance(x, int) checks if x is an int or subclass.
Step 2: Analyze other options
type(x) == isinstance(int) mixes type() and isinstance() wrongly. isinstance(int, x) reverses arguments. type(x) is int() incorrectly calls int as a function.
Final Answer:
isinstance(x, int) -> Option C
Quick Check:
isinstance(x, int) correct syntax [OK]
Hint: Use isinstance(obj, Type) to check type safely [OK]
Common Mistakes:
Swapping arguments in isinstance()
Using type() with wrong comparison
Calling type names as functions in checks
3. What will be the output of this code?
class Animal:
pass
class Dog(Animal):
pass
x = Dog()
print(isinstance(x, Animal))
print(type(x) == Animal)
medium
A. True\nFalse
B. False\nTrue
C. True\nTrue
D. False\nFalse
Solution
Step 1: Understand isinstance() with inheritance
x is an instance of Dog, which is a subclass of Animal. So, isinstance(x, Animal) returns True.
Step 2: Understand type() equality check
type(x) returns Dog, which is not equal to Animal. So, type(x) == Animal returns False.
Final Answer:
True\nFalse -> Option A
Quick Check:
isinstance() checks inheritance, type() exact type [OK]
Hint: isinstance() True if subclass; type() True only if exact type [OK]
B. The function should use isinstance() instead of type().
C. The return statements are missing parentheses.
D. The print statement syntax is wrong.
Solution
Step 1: Check type() usage
type() takes one argument to return the type. Using two arguments like type(n, int) is invalid and causes a TypeError.
Step 2: Identify correct usage
To check if n is an int, use isinstance(n, int). The return statements and print syntax are correct.
Final Answer:
type() is used with two arguments incorrectly. -> Option A
Quick Check:
type() needs one argument [OK]
Hint: type() takes one argument; use isinstance() to check type [OK]
Common Mistakes:
Passing two arguments to type()
Confusing type() and isinstance() syntax
Thinking return needs parentheses
5. You have a list of mixed data:
class MyInt(int):
pass
items = [123, 'hello', 45.6, True, MyInt(456)]
. You want to create a new list containing only integers (including booleans, since they are subclasses of int). Which code correctly uses isinstance() to do this?
hard
A. ints = [x for x in items if type(x) is int or type(x) is bool]
B. ints = [x for x in items if isinstance(x, int)]
C. ints = [x for x in items if isinstance(x, bool)]
D. ints = [x for x in items if type(x) == int]
Solution
Step 1: Understand subclass behavior with isinstance()
Booleans are subclasses of int in Python, so isinstance(True, int) returns True. Using isinstance(x, int) includes both ints and bools.
Step 2: Compare with other options
ints = [x for x in items if type(x) == int] excludes bools because type(True) is bool, not int. ints = [x for x in items if isinstance(x, bool)] selects only bools, not ints. ints = [x for x in items if type(x) is int or type(x) is bool] is verbose and misses subclass flexibility.
Final Answer:
ints = [x for x in items if isinstance(x, int)] -> Option B
Quick Check:
isinstance() includes subclasses [OK]
Hint: Use isinstance(x, int) to include bools as ints [OK]