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