Write A Python Program To Change The Data Type(Int To Float) Of An Array

In this article you will learn a Python program that takes an array of integer data type and then changes the array data type to floating point.

This can be done using the type conversion,  the array of integers will be converted to the array of floating point values using a simple  type casting method. 

 

Write A Python Program To Change The Data Type(Int To Float) Of An Array

 



Write A Python Program To Change The Data Type(Int To Float) Of An Array

Let's import the required modules,  we require an array module in order to store the array of integers and array of floating point values.

Take an Empty array of integers by initializing  it to an empty list.

Now in order to accept the array as input to the Python program take a length variable and keep asking the user to enter the array item using input function call and then appended to the array


from array import array

num_array = array("i", list())
length = int(input("Enter List Length : "))
for _ in range(length):
   temp = int(input("Enter array item : "))
   num_array.append(temp)





Once the for loop is completed you will be having an array of integers stored in the above variable.  using the type conversion supported by array function call convert the integer values to the floating point values and store them into another variable.

use the print function call to print the array of integers and array of floating point values as shown below,  this completes the Python program.

print(f"Array of Integers : ", num_array)
float_array = array("f", num_array)
print(f"Array of Floating Point Numbers : ", float_array)





Complete python Program
=========================

from array import array

num_array = array("i", list())
length = int(input("Enter List Length : "))
for _ in range(length):
   temp = int(input("Enter array item : "))
   num_array.append(temp)


print(f"Array of Integers : ", num_array)
float_array = array("f", num_array)
print(f"Array of Floating Point Numbers : ", float_array)




Output
=============

Enter List Length : 4
Enter array item : 1
Enter array item : 2
Enter array item : 3
Enter array item : 4
Array of Integers :  array('i', [1, 2, 3, 4])
Array of Floating Point Numbers :  array('f', [1.0, 2.0, 3.0, 4.0])




Output
===============

Enter List Length : 6
Enter array item : 33
Enter array item : 44
Enter array item : 66
Enter array item : 77
Enter array item : 332
Enter array item : 76
Array of Integers :  array('i', [33, 44, 66, 77, 332, 76])
Array of Floating Point Numbers :  array('f', [33.0, 44.0, 66.0, 77.0, 332.0, 76.0])




Conclusion
==================

The above Python program except the random integer values as input to the Python program then stores them into an integer  array.  then using the type conversion  it converts it into the floating point array and finally prints them.

 execute the above Python  Code by providing random integers as input to the python script,  then the integers will be converted to the floating point values and will be printed to the standard output.

comment and down below if you have any queries regarding the above by ton program.  Also if you have any other solution do let me know in the comment section

Post a Comment

0 Comments