Python Return Float From Function

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., return 10/3).

πŸ‘‰ Recommended Tutorial: The return keyword in Python

Method 1: Using the float() Function

Python’s 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 3.0.

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 return x.

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 10/3 yields 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.

Related Tutorials

Programmer Humor

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.