w3resource logo
Python Exercises

Python NumPy: Array converted to a float type

Python NumPy: Array Object Exercise-7 with Solution

Write a Python program to an array converted to a float type.

Sample Solution:-

Python Code:

import numpy as np
import numpy as np
a = [1, 2, 3, 4]
print("Original array")
print(a)
x = np.asfarray(a)
print("Array converted to a float type:")
print(x)

Sample Output:

Original array                                                          
[1, 2, 3, 4]                                                            
Array converted to a float type:                                        
[ 1.  2.  3.  4.]

Python Code Editor:

import numpy as np
a = [1, 2, 3, 4]
print("Original array")
print(a)
x = np.asfarray(a)
print("Array converted to a float type:")
print(x)

Improve this sample solution and post your code through Disqus