Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check the type of variable x.
Python
x = 10 print(type([1]))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the value 10 directly inside type() instead of the variable.
Using quotes around the variable name.
✗ Incorrect
Using
type(x) returns the type of the variable x.2fill in blank
mediumComplete the code to check if y is of type str.
Python
y = 'hello' if type(y) == [1]: print('y is a string')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around 'str' which makes it a string literal, not a type.
Comparing to the wrong type like
int or float.✗ Incorrect
The type of a string is
str, so we compare type(y) to str.3fill in blank
hardFix the error in the code to correctly check if z is a list.
Python
z = [1, 2, 3] if type(z) [1]: print('z is a list')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using single equals
= instead of double equals ==.Putting the type inside quotes, making it a string.
✗ Incorrect
The comparison operator should be
==, and the type to compare is list without quotes.4fill in blank
hardFill both blanks to create a dictionary of variable names and their types for a and b.
Python
a = 5 b = 'text' types = {'a': [1], 'b': [2] print(types)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the variable itself instead of its type.
Mixing up the variables inside
type().✗ Incorrect
Use
type(a) and type(b) to get the types of variables a and b.5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps each word to its type if the word is a string.
Python
items = [123, 'apple', 4.5] types_dict = { [1] : [2] for [3] in items if type([3]) == str } print(types_dict)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Not using
type() to get the type.✗ Incorrect
Use
item as the variable name, map it to type(item), and iterate with for item in items.