0
0
NumPydata~10 mins

Type casting with astype() in NumPy - Interactive Code Practice

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

Complete the code to convert the NumPy array to float type using astype().

NumPy
import numpy as np
arr = np.array([1, 2, 3])
arr_float = arr.[1](float)
print(arr_float)
Drag options to blanks, or click blank then click option'
Aastype
Bconvert
Ctofloat
Dcast
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist like 'convert' or 'cast'.
Trying to change type by assignment instead of using a method.
2fill in blank
medium

Complete the code to convert the integer array to string type using astype().

NumPy
import numpy as np
arr = np.array([10, 20, 30])
arr_str = arr.[1](str)
print(arr_str)
Drag options to blanks, or click blank then click option'
Aastype
Bto_str
Cconvert
Dcast
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent method like 'to_str'.
Trying to convert by casting without a method.
3fill in blank
hard

Fix the error in the code to convert the float array to integer type using astype().

NumPy
import numpy as np
arr = np.array([1.5, 2.7, 3.1])
arr_int = arr.[1](int)
print(arr_int)
Drag options to blanks, or click blank then click option'
Acast
Bastypee
Cconvert
Dastype
Attempts:
3 left
💡 Hint
Common Mistakes
Misspelling the method name as 'astypee'.
Using a method that does not exist.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values, only for words longer than 3 characters.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
lengths = { [1] : len([2]) for [1] in words if len([1]) > 3 }
print(lengths)
Drag options to blanks, or click blank then click option'
Aword
Blen
Cwords
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Using the list name instead of the loop variable inside len().
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their float lengths as values, only for words longer than 3 characters.

NumPy
import numpy as np
words = ['apple', 'bat', 'carrot', 'dog']
lengths = { [1] : np.array([len([2])]).[3](float)[0] for [2] in words if len([2]) > 3 }
print(lengths)
Drag options to blanks, or click blank then click option'
Aword.upper()
Bword
Castype
Dwords
Attempts:
3 left
💡 Hint
Common Mistakes
Using the list name instead of the loop variable.
Forgetting to convert length to float.
Using wrong method names.