Python Float: A Complete Beginner's Guide

Posted in /  

Python Float: A Complete Beginner's Guide
vinaykhatri

Vinay Khatri
Last updated on March 28, 2024

    Python comes with a built-in function float() that can convert a valid string and integer number to Python floating-point number. Python float numbers represent decimal numbers or real numbers that provide more precision to the numeric value. In Python programming, we have different data types to represent different types of data values.

    For example we have int type to represent integer numbers, str type to represent string or character sequences, complex to represent complex numbers, and float to represent decimal or floating-point numbers.

    To know more about all the data types of Python, click here .

    In this tutorial, we will walk through the Python floating-point numbers and discuss how to use the inbuilt float() function to convert a string or integer value to a floating-point number.

    Python Float

    In Python, there are two data types that represent numerical values int and float . The int datatype is used to store integer values from positive infinity to negative infinity. And the float data type is used to store real numbers or decimal numbers from positive infinity to negative infinity. Python uses the 64-bit double-precision values to store floating-point numbers and the maximum floating-point value could be 1.8 x 10 308 which can also be represented as float('inf') float infinity.

    Example

    # integer number
    int_num = 30
    
    # float num
    float_num = 30.0
    
    print("The Type of int_num is:", type(int_num))
    
    print("The Type of float_num is: ", type(float_num))

    Output

    The Type of int_num is: <class 'int'>
    The Type of float_num is: <class 'float'>

    In this example, the value of int_num is 30 and the value of float_num is 30.0. The value of both the variables is equal even if we try to perform an equal to comparison operation between the two we will find that the result is True. The only difference between the two values is the decimal point and this distinguishes their data type. A floating-point number always has a decimal point and an int value don't.

    Python Float function

    In Python we have the concept of type conversion, using which we can convert the data type of one object to another, using some built-in Python functions. float() is also one of the Python type conversion inbuilt functions, that can convert the numeric value of string and integer value to a floating-point number.

    Syntax

    float(number)

    return value The float() object does not convert the actual variable to the float, instead it converts the copy of the value and returns it.

    Example

    # convert an integer to float
    int_num = 30
    
    # convert the integer into float
    float_num = float(int_num)
    
    print(f"int_num ==> {int_num}; float_num == {float_num}")

    Output

    int_num ==> 30; float_num == 30.0
    

    Convert an integer number into Float

    If a number is of integer data type we can convert it into float using the float() function. The float() function will put a .0 value after the integer number and the number will be converted into the float.

    Example

    # convert a integer to float
    int_num = 30
    
    # convert the integer into float
    float_num = float(int_num)
    
    print(f"int_num ==> {int_num}; float_num == {float_num}")

    Output

    int_num ==> 30; float_num == 30.0

    As we already know that the float() function does not change the actual variable object instead it works on the copy of object value and return the result. There is also a trick to convert a Python integer number into a floating-point number without using the float function. All we need to do is add 0.0 to the integer number and because of Python implicit type conversion property we will receive a floating-point number, as a result.

    Example

    # convert a integer to float
    int_num = 30
    
    # convert the integer into float without using float()
    float_num = int_num + 0.0
    
    print(f"int_num ==> {int_num}; float_num == {float_num}")

    output

    int_num ==> 30; float_num == 30.0
    

    Convert a String value to float

    We can also convert the valid string numerical value to floating-point numbers using the float() function.

    syntax

    float('number')

    Example

    # string number
    string_num = '-30.74'
    
    # convert the string number into float using float()
    float_num = float(string_num)
    
    print(f"int_num ==> {string_num}; float_num == {float_num}")

    Output

    int_num ==> -30.74; float_num == -30.74
    

    If we try to convert an invalid string value into the float that contains any letter and special symbols (except underscore), we would receive the ValueError .

    Example

    # invalid string number
    string_num = '-30.74o'
    
    # convert the string number into float using float()
    float_num = float(string_num)
    
    print(f"int_num ==> {string_num}; float_num == {float_num}")

    Output

    Traceback (most recent call last):
    File "main.py", line 5, in <module>
    float_num = float(string_num)
    ValueError: could not convert string to float: '-30.74o'

    The last character of string_num is o which is a letter and the float() function cannot convert letter values to floating values.

    Infinity and Not a Number in Float.

    There are three special string letters that can converted into float using float() function.

    1. 'Infinity' to represent the infinite numbers.
    2. 'inf' same as infinity.
    3. 'nan' not a number.

    We can only use the float function on these three special string character values to convert them into a floating-point number.

    Example

    # positive infinite number
    max_number = float('inf')
    # negetive infinite number
    min_number = float('-Infinity')
    
    # not a number
    nan = float('nan')
    
    print('Max Number:', max_number)
    print('Min Number:', min_number)
    print('Not a Number:', nan)

    output

    Max Number: inf
    Min Number: -inf
    Not a Number: nan

    Convert Boolean values to float Numbers

    There are two boolean values in Python True and False . And when we try to convert them into a floating-point number using float() function True became 1.0 and False become 0.0 .

    Example

    # true value
    true = float(True)
    # false value
    false = float(False)
    
    print('True Number:', true)
    print('False Number:', false)

    Output

    True Number: 1.0
    False Number: 0.0

    This is also why Python treat 0 as a Falsey Value.

    Wrapping Up!

    Python floating points numbers bring more precision to the numeric data values. And as a Python developer, you will be working more with floating-point numbers as compared to integers. Also with the help of Python float() the function we can convert the data type of boolean, string and integer to a floating-point number, which makes it easy for us to convert the data type of the value.

    People are also reading:

    Leave a Comment on this Post

    0 Comments