0
0
Pythonprogramming~10 mins

type() and isinstance() 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(x) == [1])
Drag options to blanks, or click blank then click option'
Afloat
Bstr
Clist
Dint
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using quotes around the type name like 'int' instead of int.
Confusing int with str or float.
2fill in blank
medium

Complete the code to check if value is an instance of list.

Python
value = [1, 2, 3]
print(isinstance(value, [1]))
Drag options to blanks, or click blank then click option'
Alist
Bdict
Cset
Dtuple
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using type() instead of isinstance() for type checking.
Choosing the wrong type like tuple or dict.
3fill in blank
hard

Fix the error in the code to correctly check if data is a string.

Python
data = 'hello'
if isinstance(data, [1]):
    print('It is a string')
Drag options to blanks, or click blank then click option'
Alist
Bint
Cstr
Dbool
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using quotes around the type name like 'str'.
Checking against wrong types like int or list.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.

Python
words = ['apple', 'bat', 'car', 'door']
lengths = {word: [1] for word in words if [2] > 3}
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using word instead of len(word) for the value.
Comparing word directly to 3 instead of its length.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if the length is greater than 3.

Python
words = ['apple', 'bat', 'car', 'door']
result = { [1]: [2] for word in words if [3] > 3 }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Dword
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using word instead of word.upper() for keys.
Comparing word directly to 3 instead of its length.