Convert list of Celsius values into Fahrenheit using NumPy array
Let us see how to convert a list of Celsius temperatures into Fahrenheit by converting them into NumPy array.
The formula to convert Celsius to Fahrenheit is :
feh = (9 * cel / 5 + 32)
Method 1 : Using the numpy.array() method.
python3
# importing the moduleimport numpy as np# taking the celsius inputinp = [0, 12, 45.21, 34, 99.91]# storing the input# in numpy arraycel = np.array(inp)print(f"Celsius {cel}")# using formulaefeh = (9 * cel / 5 + 32)# printing resultsprint(f"Fahrenheit {feh}") |
Output :
Celsius [ 0. 12. 45.21 34. 99.91] Fahrenheit [ 32. 53.6 113.378 93.2 211.838]
Method 2 : Using the numpy.asarray() method.
python3
# importing the moduleimport numpy as np# taking the celsius inputinp = [0, 12, 45.21, 34, 99.91]# storing the input# in numpy arraycel = np.asarray(inp)print(f"Celsius {cel}")# using formulaefeh = (9 * cel / 5 + 32)# printing resultsprint(f"Fahrenheit {feh}") |
Output :
Celsius [ 0. 12. 45.21 34. 99.91] Fahrenheit [ 32. 53.6 113.378 93.2 211.838]
Method 3 : Using numpy.arange().
python3
# importing the moduleimport numpy as np# taking thecelsius inputinp = [0, 12, 45.21, 34, 99.91]# arange will not directly# convert into numpy arraycel = np.arange(5)# the above code# will give o/p as# cel = [0, 1, 2, 3, 4]cel = [i for i in inp]# thus the input(inp)# is stored in cel# using list comprehensionprint(f"Celsius {cel}")# applying formulae# using list comprehensionfeh = [(9 * i / 5 + 32) for i in cel]# printing resultsprint(f"Fahrenheit {feh}") |
Output :
Celsius [ 0. 12. 45.21 34. 99.91] Fahrenheit [ 32. 53.6 113.378 93.2 211.838]




.png)