Python Tutorials - Herong's Tutorial Examples - v2.14, by Herong Yang
Data Type - 'float' for Real Numbers
This section describes the 'float' data type, which using the double precision of floating-point number format defined in the IEEE 754 standard.
What Is the "float" Data Type? "float" is the floating number data type in Python. It has infinite number of objects covering all real numbers.
Note that "float" data type stores real numbers in double precision (using 8-byte storage) format according to the IEEE 754 standard.
"float" data type has the following main Features.
1. "float" data objects can be created in several ways:
2. "float" data type is an immutable data type. Once a "float" data object is created to store a real number, this number will never change.
Don't get confused about "float" immutability and "float" variable re-assignment capability. A "float" variable can be re-assigned with different "float" objects many times.
# new object is created for 1.0 and assigned to x >>> x = 1.0 >>> id(x) 4551429136 # assign the same object to y >>> y = x >>> id(y) 4551429136 # new object is created for 2.0 and assigned to x >>> x = 2.0 >>> id(x) 4551429360 # the first object for 1.0 is still there in memory >>> y 1.0 >>> id(y) 4551429136
3. Because "float" data type is immutable, an object created for a given real number is not allowed to change its value. This allow "float" objects to be cached in memory and reused later whenever they are needed again. Reusing objects will reduce execution time and memory consumption.
On my macOS computer, "float" objects are cached and reused in some cases, not in all cases. The code example below shows you that there are 4 "float" objects created for 5 requests of real number 1.0.
>>> x = 1.0 >>> y = 1.0 >>> (id(x), id(y), id(1.0), id(float(1)), id(float('1'))) (4551427088, 4551429360, 4551429136, 4551426576, 4551426576)
4. "float" objects support the following arithmetic operations:
Syntax Operation Resulting Type ------ --------- -------------- x + y Addition float x - y Subtraction float x * y Multiplication float x / y Division float
5. "float" objects support the following comparison operations.
Syntax Operation ------ --------- x < y Less than x <= y Less than or equal x > y Greater than x >= y Greater than or equal x == y Equal x != y Not equal
6. Some built-in functions are provided for "float" objects.
7. Some class/instance methods are provided for "float" objects.
Table of Contents
Common Features of All Data Types
Data Type - NoneType for Nothing
Data Type - 'bool' for Boolean Values
Data Type - 'int' for Integer Values
►Data Type - 'float' for Real Numbers
Data Type - 'bytes' for Byte Sequence
Data Type - 'str' for Character String
Data Type - 'tuple' for Immutable List
Data Type - 'list' for Mutable List
Data Type - 'dict' for Dictionary Table
Variables, Operations and Expressions
Function Statement and Function Call
Iterators, Generators and List Comprehensions
Packages and Package Directories
"pathlib" - Object-Oriented Filesystem Paths
"pip" - Package Installer for Python
SciPy.org - Python Libraries for Science
pandas - Data Analysis and Manipulation
Anaconda - Python Environment Manager