Do you need to create a function that returns a float but you don’t know how? No worries, in sixty seconds, you’ll know! Go! 🔥🔥🔥
A Python function can return any object such as a float value such as
3.14. To return a float, you can use the built-in
float() function or create your own function with an arbitrary simple or complex expression within the function body and put the result of this after the
return keyword (e.g.,
👉 Recommended Tutorial: The
return keyword in Python
Method 1: Using the float() Function
float() function takes an argument such as a string or an integer and attempts to convert it to a float. You don’t need to import a library as it is built-in. For example,
float('3.14') converts the string to a float 3.14 and
float(3) converts the integer to the float
Here’s a code snippet exemplifying this approach:
# String to Float x = '3.14' print(float(x)) # 3.14 # Int to Float x = 3 print(float(x)) # 3.0
⭐⭐⭐ This is the most straightforward approach to returning a float from a function.
You can also watch my explainer video and visit the recommended blog tutorial on the topic:
🌍 Recommended Tutorial: Python float() Function
Method 2: Create Your Own Function and Return Float Right Away
You can create your custom function returning a float by using the keyword
def, followed by a function name, followed by an arbitrarily complicated function body to determine the resulting float. Say, you’ve stored the resulting float in the local variable
x. To return it from the function, use the expression
Let’s have a look at a minimal example that creates a function
my_float() that returns a float value
3.14 and does nothing else:
def my_float(): return 3.14 print(my_float()) # 3.14
⭐⭐⭐ This is the most flexible approach to returning a float from a function because you can do anything in the function body, it’s Turing complete!
Method 3: Use Float Return Expression
In your function body, you can also use arbitrary mathematical or programmatical expressions to determine the float. For example, the expression
return 10/3 computes the float as the result of the division operator on two ints and returns it from the function.
Here’s an easy example:
def my_float(): return 10/3 print(my_float()) # 3.3333333333333335
Note that floats can be imprecise and introduce floating point errors, especially in the representation of digits far into the right of the decimal point. That’s why
3.333333333333335 in the previous example.
Feel free to check out my “Division Deep Dive” video:
Method 4: Use Complicated Function Body to Compute Float
For comprehensibility, you can use an arbitrarily complex function body to calculate and return a float value.
Here’s an example of computing the value π (Pi) in the function body:
def calculate_pi(): pi = 0 n = 10**7 add = True for i in range(1, n, 2): if add: pi += 1/i else: pi -= 1/i add = not add return pi*4 print(calculate_pi()) # 3.1415924535897797
This uses the Leibniz formula for computing π:
If you want to learn more about this code snippet to calculate the value of Pi, feel free to check out our Finxter tutorial on the topic.
- Python Return String From Function
- Python Return Dict From Function
- Python Return Set From Function
- Python Return List From Function
- Python Return Integer From Function
Q: How do you tell an introverted computer scientist from an extroverted computer scientist? A: An extroverted computer scientist looks at your shoes when he talks to you.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.