Home »
Python »
Python programs
Python program to convert tuple to float value
In this problem, we are given a tuple consisting of two integer values. Our task is to create a Python program to convert the tuple to float value.
Submitted by Shivang Yadav, on January 09, 2022
Sometimes programmers need to avoid loss in data store the given values in some collection. The extraction of such values from such collections is a useful tool for programmers. Here, we will see a Python program to convert the tuple to a float value.
Before going further with the problem, let's recap some basic topics that will help in understanding the solution.
Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.
Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.
Example:
tuple = ("python", "includehelp", 43, 54.23)
Converting Tuple elements to Float Value
In this article, we are given a tuple consisting of two elements. Our task is to create a Python program to convert the elements of the tuple to a float value.
Input:
(5, 1)
Output:
5.1
To perform this conversion, we will iterate the tuple and use the first element as an integer part of the floating-point value and the second element as the decimal part of the value.
In Python, we will be performing this conversion by simply joining the two elements of the tuple with a dot as a separator and then converting the resultant value to a floating-point value.
We will loop over the tuple and use the str() method to convert the elements to string with are joined using the join() method and finally the float() method performs the final conversion.
# Python program to convert tuple
# to a float value
# Initializing and printing the tuple
myTuple = (7356, 67584)
print("The elements of the tuple are " + str(myTuple))
# converting tuple to float
floatVal = float('.'.join(str(value) for value in myTuple))
# Printing result
print("The converted float value is " + str(floatVal))
Output:
The elements of the tuple are (7356, 67584)
The converted float value is 7356.67584
Python Tuple Programs »