Python Pop Quiz - Type Addition
Python has multiple built-in functions you can use to introspect your code. You’ll learn more about those functions in a later quiz.
For this quiz, you’ll focus on isinstance(), a function you can use to check if a variable is a particular type. For example, you might want to check if a variable is a string or an integer.
Good luck!
The Quiz
What will be the output if you run this code?
A) integers_found = 2 bools_found = 3
B) integers_found = 5 bools_found = 2
C) integers_found = 5 bools_found = 0
D) integers_found = 5 bools_found = 5
Hint
Some types inherit from other types.
Stop here and try to figure out the quiz above. When you are ready, you can continue to the answer!
The Answer
C) integers_found = 5 bools_found = 0
Explanation
The trick here is that in Python, the Boolean type is a subclass of int, so Booleans are integers. The other trick is that you check if the item is an int data type BEFORE you check if it is bool, so ALL the Boolean types get added to the integers_found list.
If you look at my_list, you will see two integers: 1 and 5. There are also three Boolean values: True, False and True. Two plus three equals five!
Get the Book
Want more Python quizzes? Check out my book, The Python Quiz Book.



