Division and Floating Point Numbers in Python

Floating Point Numbers

In Python, floating point numbers (decimals) are used to represent numbers instead of integers.

Some examples of numbers are represented as 0.5 and -7.8237591.

They can be created directly by entering numbers with a decimal point or by using operations like integer division.

Computers cannot store floating point numbers (decimals) with complete precision, just as we cannot write out the complete decimal representation of 1/3 (0.3333333333333333).

Keep this in mind, as it often leads to annoying program bugs!

Integer Division Results in Floating Point Numbers

As mentioned earlier, dividing any two integers will produce a floating point number.

Performing operations on two floating point numbers, or on a floating point number and an integer, will yield a floating point number.

>>> 8/24.0
>>> 6*7.042.0
>>> 4+1.655.65

Python silently converts integers to floating point numbers.

0 Cannot Be a Divisor

Dividing any number by 0 in Python will result in an error.

>>> 11/0
ZeroDivisionError: division by zero

In Python, the last line of the error message indicates the type of error. Read the error message carefully, as it often tells you how to fix the program!

ZeroDivisionError: division by zero errordivision: dividezero: zero

Python Basics Series

01 – The First Python Program

02 – Python Mathematical Operations: Addition, Subtraction, Multiplication, Division



  Purely Free Online Programming Learning

Online version: http://dida100.com/its Learning programming is like playing a game, super interesting~



  Learning programming alone is too lonely
I am a beginner (WeChat python776) Note 888 I have created a WeChat programmer study group to answer questions for each other. Those who need can add me on WeChat to join the group.




Click 👇 Purely free, learn programming

Leave a Comment