In this article, we will discuss how to handle float numbers in Python. We will discuss about the various ways to represent floating point. Let’s begin our journey of floating point numbers.

What are Floating Point Numbers?

Numbers which have a fractional part are classified as floating point numbers. Floating point numbers are used to represent very large numbers and very small numbers. (In case you need more information about floating point numbers, read this article on Floating Point Arithmetic at Wikipedia)

Representing Floating Point number in Python

Floating point numbers in Python can be expressed in fractional form or in exponential form. Floating point numbers can be positive or negative. Some examples of valid floating point numbers using the fractional form and exponential form are given below.

Floating point (fractional representation)

In the below code snippet, the variable a contains a positive floating point number, variable b contains a negative floating point number. Variable c contains a number whose fractional part is 0, but still qualifies as a float because of the point.

# understanding float numbers
a=3.6
print(a)        #3.6
print(type(a))  #float

b=-2.7
print(b)        #-2.7
print(type(b))  #float

c=9.0
print(c)        #9.0
print(type(c))  #float

Floating point (exponential representation)

In the below code snippet floating point values in different form using exponential representation are used. Variable a contains a positive floating point number with a positive exponent and variable b contains a positive floating point number with a negative exponent. Variable c contains a negative floating point number with a positive exponent and variable d contains a negative floating point number with a negative exponent.

  • a = 3.25e2 = 3.25×102 = 3.25×100 = 325.0
  • b = 3.25e-2 = 3.25×10-2 = 3.25/100 = 0.0325
  • c = -3.25e2 = -3.25×102 = -3.25×100 = -325.0
  • d = -3.25e-2 = -3.25×10-2 = -3.25/100 = -0.0325
# understanding float numbers
a=3.25e2
print(a)        #325.0
print(type(a))  #float

b=3.25e-2
print(b)        #0.0325
print(type(b))  #float

c=-3.25e2
print(c)        #-325.0
print(type(c))  #float

d=-3.25e-2
print(d)        #-0.0325
print(type(d))  #float

Type Conversion

The built-in float() function can be used to convert values from int/string to float.

Type Conversion from int to float

Integer numbers (int) can be converted to floating point numbers (float), by using the built-in float() function. A fractional part with value 0 is added to the integer number.

#converting int to float 
a=2
b=float(a)
print(b)    #result = 2.0

Type Conversion from string (integer) to float

Numerical strings containing an integer value can be converted to floating point number (float) by using the built-in float() function.

#converting integer value string to float
a="3"
b=float(a)
print(b)    #result = 3.0

Type Conversion from string (float) to float

Numerical strings containing floating point number can be converted to floating point number(float) by using the built-in float() function.

#converting float value string to float
a="3.5"
b=float(a)        
print(b)         #result = 3.5

More Concepts in Data Types

Last modified: April 2, 2023

Author