Python Float Number

About Python Float Number

In Python, a float number is a numeric data type that represents real number with decimal points.Float is used to store and manipulate numbers with fractional parts.

Creating a Float Number

You can define a float number in Python by simply assigning a number with decimal point to a variable.

In the following example, a positive number '3.14' is assigned to the variable 'num_float' to create a float variable.

In the same way, a negative number '-3.14' is assigned to the variable 'neg_num_float' to create a negative float variable.

'type()' function is used to get the type of the variable 'num_float''.

Please have a closer look at the following example:

Example of Float Number:
Output of the above example
    <class 'float'>
    <class 'float'>
                                

For both positive and negative float numbers, we get the class type 'float'. That refers that both are float numbers.

Uses of Python Float Number

Float numbers are extensively used in mathematical calculations, scientific calculations, engineering simulations, and any computation where fractional values are required.

Mathematical operations:

Various mathematical operations such as addition, subtraction, multiplication, and division can be performed with float in Python.

You need to use '+' operator for addition, '-' operator for subtraction, '*' operator multiplication, and '/' operator division.

In the following example, a = 10.5 and b = 3.0 float variables are created. Different mathematical operations are performed using these variables.

Please observe the following examples:

Example of mathematical operations of float numbers:
Output of the above example
    13
    7
    30
    3.5
                                
Mathematical calculations:

In the following example, the area of a circle has been determined. radius = 5.0 and area = 3.14 * (radius ** 2) are two float variables.

Example of mathematical calculations of float numbers:
Output of the above example
    
    78.5
                                
Scientific Computing:

Floats are widely used in scientific computing for simulations, data analysis, and other scientific applications using Python libraries like NumPy, SciPy, and Matplotlib.

In the following example, Python library NumPy used to create a one-dimensional NumPy array with float numbers and get the square root of each element in the array.

Please have a closer look at the following examples:

Example of using float numbers in creating NumPy array:

Output of the above example


    [2.44948974 1.41421356 1.73205081 2.23606798]
                                

It's important to note that due to the way floating-point numbers are represented in computer, there can be precision issues. This can be mitigated using decimal module. For details, please check decimal module.

Python Compiler For Live Practice

Practice the above Python code in the following Python Editor. Create more Python float number variables and perform more mathematical and scientific computing in the following code editor and click Execute button. Output will be displayed in Python Code Output section.

Python logo Python Editor
Python Code Output