Complete the code to check the type of variable x.
x = 10 print(type(x) == [1])
int with str or float.The type() function returns the type of the object. Since x is 10, which is an integer, type(x) equals int.
Complete the code to check if value is an instance of list.
value = [1, 2, 3] print(isinstance(value, [1]))
type() instead of isinstance() for type checking.tuple or dict.The isinstance() function checks if the object is an instance of the given type. Here, value is a list, so isinstance(value, list) returns True.
Fix the error in the code to correctly check if data is a string.
data = 'hello' if isinstance(data, [1]): print('It is a string')
int or list.The isinstance() function needs the type str to check if data is a string. Using str here returns True.
Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.
words = ['apple', 'bat', 'car', 'door'] lengths = {word: [1] for word in words if [2] > 3}
word instead of len(word) for the value.word directly to 3 instead of its length.The dictionary comprehension maps each word to its length using len(word). The condition filters words with length greater than 3 using len(word) > 3.
Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if the length is greater than 3.
words = ['apple', 'bat', 'car', 'door'] result = { [1]: [2] for word in words if [3] > 3 }
word instead of word.upper() for keys.word directly to 3 instead of its length.The dictionary comprehension uses word.upper() as the key, len(word) as the value, and filters words with length greater than 3 using len(word) > 3.