Sayantan's Blog On Python Programming

PYTHON FLOATING POINT NUMBERS

PYTHON FLOATING POINT NUMBERS

INTRODUCTION:

Floating point numbers are also working the similar way like Integer. They only have the decimal part.

WORKING WITH FLOATING VARIABLES:

Simple adding using variables:

# Simple adding using variables example

a = 7.5
print("\nValue of a -> ", a)

b = 8.2
print("\nValue of a -> ", a)

c = a + b

print("\nValue of C -> ", c)

PYTHON FLOATING POINT NUMBERS : Output

Value of a ->  7.5

Value of a ->  7.5

Value of C ->  15.7

In the above example we have simply put two floating number into two variables a and b. Then calculate the sum of a and b and then put the sum into a third variable c.

PYTHON FLOATING POINT NUMBERS : Output
PYTHON FLOATING POINT NUMBERS : Output

Adding one variable to itself:

# Adding one variable to itself example

a = 7.5
print("\nValue of a -> ", a)

a += a
print("\nValue of a += a -> ", a)

PYTHON FLOATING POINT NUMBERS : Output

Value of a ->  7.5

Value of a += a ->  15.0

Subtracting one variable to itself:

# Subtracting one variable to itself example

a = 7.5
print("\nValue of a -> ", a)

a -= a
print("\nValue of a -= a -> ", a)

PYTHON FLOATING POINT NUMBERS : Output

Value of a ->  7.5

Value of a -= a ->  0.0

Multiplying one variable to itself:

# Multiplying one variable to itself example

a = 7.5
print("\nValue of a -> ", a)

a *= a
print("\nValue of a *= a -> ", a)

PYTHON FLOATING POINT NUMBERS : Output

Value of a ->  7.5

Value of a *= a ->  56.25

Dividing one variable to itself:

# Dividing one variable to itself example

a = 7.5
print("\nValue of a -> ", a)

a /= a
print("\nValue of a /= a -> ", a)

PYTHON FLOATING POINT NUMBERS : Output

Value of a ->  7.5

Value of a /= a ->  1.0

Result of arithmetic operation between Floating and Integer:

Addition:
# Result of arithmetic operation between Floating and Integer example
# Addition

a = 10
print("\nValue of a -> ", a)
print("\nData Type of a -> ", type(a))

b = 10.0
print("\nValue of b -> ", b)
print("\nData Type of b -> ", type(b))

c = a + b
print("\nValue of c -> ", c)
print("\nData Type of c -> ", type(c))

PYTHON FLOATING POINT NUMBERS : Output

Value of a ->  10

Data Type of a ->  <class 'int'>

Value of b ->  10.0

Data Type of b ->  <class 'float'>

Value of c ->  20.0

Data Type of c ->  <class 'float'>

In the above example we have added an integer variable a with a floating point variable b. The sum is stored in a variable c. c is automatically converted to a floating point variable.

So whenever we do any arithmetic operation between an integer and a floating point number the result will always be a floating point number.

Subtraction:
# Result of arithmetic operation between Floating and Integer example
# Subtraction

a = 10
print("\nValue of a -> ", a)
print("\nData Type of a -> ", type(a))

b = 8.0
print("\nValue of b -> ", b)
print("\nData Type of b -> ", type(b))

c = a + b
print("\nValue of c -> ", c)
print("\nData Type of c -> ", type(c))

PYTHON FLOATING POINT NUMBERS : Output

Value of a ->  10

Data Type of a ->  <class 'int'>

Value of b ->  8.0

Data Type of b ->  <class 'float'>

Value of c ->  18.0

Data Type of c ->  <class 'float'>

Multiplication:
# Result of arithmetic operation between Floating and Integer example
# Multiplication

a = 10
print("\nValue of a -> ", a)
print("\nData Type of a -> ", type(a))

b = 8.0
print("\nValue of b -> ", b)
print("\nData Type of b -> ", type(b))

c = a * b
print("\nValue of c -> ", c)
print("\nData Type of c -> ", type(c))

PYTHON FLOATING POINT NUMBERS : Output

Value of a ->  10

Data Type of a ->  <class 'int'>

Value of b ->  8.0

Data Type of b ->  <class 'float'>

Value of c ->  80.0

Data Type of c ->  <class 'float'>

Division:
# Result of arithmetic operation between Floating and Integer example
# Division

a = 10
print("\nValue of a -> ", a)
print("\nData Type of a -> ", type(a))

b = 8.0
print("\nValue of b -> ", b)
print("\nData Type of b -> ", type(b))

c = a / b
print("\nValue of c -> ", c)
print("\nData Type of c -> ", type(c))

PYTHON FLOATING POINT NUMBERS : Output

Value of a ->  10

Data Type of a ->  <class 'int'>

Value of b ->  8.0

Data Type of b ->  <class 'float'>

Value of c ->  1.25

Data Type of c ->  <class 'float'>

Working with String and Floating point variables:

# Working with String and Floating point variables example

a = "Hello World"
print("\nValue of a -> ", a)

b = 3.0
print("\nValue of b -> ", b)

print("\nValue of a * b -> ", a*b)

If we try to multiply a string with a floating point number then it will raise an error. It will only work with integer.

Value of a ->  Hello World

Value of b ->  3.0
Traceback (most recent call last):
  File "d:\PYTHON\PROGRAM\tempCodeRunnerFile.python", line 13, in <module>
    print("\nValue of a * b -> ", a*b)
                                  ~^~
TypeError: can't multiply sequence by non-int of type 'float'

RELATED TOPICS:

Leave a Comment

Your email address will not be published. Required fields are marked *