ADVERTISEMENT
ADVERTISEMENT

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 »



ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.