0
0
Pythonprogramming~10 mins

Type checking using type() in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Atype
B10
C'x'
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the value 10 directly inside type() instead of the variable.
Using quotes around the variable name.
2fill in blank
medium

Complete 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'
Aint
Bstr
Cfloat
Dlist
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.
3fill in blank
hard

Fix 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'
Alist
Bstr
C== list
D== str
Attempts:
3 left
💡 Hint
Common Mistakes
Using single equals = instead of double equals ==.
Putting the type inside quotes, making it a string.
4fill in blank
hard

Fill 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'
Atype(a)
Btype(b)
Ca
Db
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the variable itself instead of its type.
Mixing up the variables inside type().
5fill in blank
hard

Fill 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'
Aitem
Btype(item)
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Not using type() to get the type.