0
0
Pythonprogramming~15 mins

type() and isinstance() in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Check the console output to see the matches printed.