Recall & Review
beginner
What does the
type() function do in Python?The
type() function tells you the type of a value or variable, like whether it is a number, text, list, or something else.Click to reveal answer
beginner
How do you check if a variable
x is an integer using type()?You compare
type(x) to int like this: type(x) == int. If it is true, x is an integer.Click to reveal answer
beginner
What will
type('hello') return?It will return
<class 'str'> because 'hello' is a string (text).Click to reveal answer
beginner
Can
type() be used to check the type of a list?Yes, for example
type([1, 2, 3]) == list will be True because the value is a list.Click to reveal answer
intermediate
Is
type() useful for making decisions in code?Yes, you can use
type() in if statements to run different code depending on the type of a variable.Click to reveal answer
What does
type(3.14) return?✗ Incorrect
3.14 is a decimal number, so its type is float.How do you check if variable
y is a string?✗ Incorrect
Strings have the type
str, so you compare type(y) to str.What will
type(True) return?✗ Incorrect
True is a boolean value, so its type is bool.Which of these is NOT a valid use of
type()?✗ Incorrect
type() only tells you the type; it does not change the type of a variable.What does
type([1, 2, 3]) == list evaluate to?✗ Incorrect
The value is a list, so
type([1, 2, 3]) is list, making the comparison true.Explain how to use
type() to check if a variable is a string or an integer.Think about comparing the result of type() to the type name.
You got /3 concepts.
Describe a simple real-life example where checking the type of a variable with
type() might be useful.Imagine you get different kinds of data and want to treat them differently.
You got /3 concepts.