- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Convert tuple to float value in Python
When it is required to convert a tuple to a float value, the 'join' method, 'float' method, 'str' method and generator expression can be used.
Generator is a simple way of creating iterators. It automatically implements a class with '__iter__()' and '__next__()' methods and keeps track of the internal states, as well as raises 'StopIteration' exception when no values are present that could be returned.
The 'float' method converts a given element to float data type.
The 'str' method converts the given element into string data type.
Below is a demonstration of the same −
my_tuple_1 = ( 7, 8) print ("The first tuple is : " ) print(my_tuple_1) my_result = float('.'.join(str(elem) for elem in my_tuple_1)) print("After converting the tuple to float, the tuple is : ") print(my_result)
The first tuple is : (7, 8) After converting the tuple to float, the tuple is : 7.8
- A tuple is defined and is displayed on the console.
- The '.' operator and 'join' method is used to join the two elements in the tuple as a decimal number.
- This result is assigned to a variable.
- It is displayed as output on the console.
- Related Articles
- Convert String to Tuple in Python
- Convert Tuple to integer in Python
- How to convert float to integer in Python?
- Convert location coordinates to tuple in Python
- Python program to sort a tuple by its float element
- Java Program to convert string value to float using Float.valueOf()
- Convert tuple to adjacent pair dictionary in Python
- Python program to convert float decimal to octal number
- Python program to convert Set into Tuple and Tuple into Set
- Convert Nested Tuple to Custom Key Dictionary in Python
- How to convert Float array list to float array in Java?
- Python program to convert elements in a list of Tuples to Float
- How to convert a list into a tuple in Python?
- Convert from float to String in Java
- Convert from String to float in Java