On this page
float
Prev: filter | Up: __builtins__ | Next: format
Rel: bool, int, complex, str, bytes
Ref: docs.python.org — float
Tags: #public #python3 #builtins
Import & Availability
# Built-in (available globally without import)
from builtins import float
Overview & Signature
float(x=0, /)
Convert a string or number to a floating-point number, if possible.
Common Methods & Attributes
float.as_integer_ratio(self, /)— Return a pair of integers, whose ratio is exactly equal to the original float.float.conjugate(self, /)— Return self, the complex conjugate of any float.float.from_number(number, /)— Convert real number to a floating-point number.float.fromhex(string, /)— Create a floating-point number from a hexadecimal string.float.hex(self, /)— Return a hexadecimal representation of a floating-point number.float.imag(...)— the imaginary part of a complex numberfloat.is_integer(self, /)— Return True if the float is an integer.float.real(...)— the real part of a complex number
Usage
assert float("3.14") == 3.14
assert float("inf") > 1e308
assert float("nan") != float("nan")
Gotchas
- Binary IEEE 754 floating point:
0.1 + 0.2 != 0.3—usemath.isclose()for float comparisons anddecimal.Decimalfor exact currency math.
REPL Inspect
help(float)
Linked from
- tuts/python3/__builtins__
- tuts/python3/__builtins__/__builtins__
- tuts/python3/__builtins__/bool
- tuts/python3/__builtins__/bytearray
- tuts/python3/__builtins__/bytes
- tuts/python3/__builtins__/complex
- tuts/python3/__builtins__/dict
- tuts/python3/__builtins__/filter
- tuts/python3/__builtins__/format
- tuts/python3/__builtins__/frozenset
- tuts/python3/__builtins__/int
- tuts/python3/__builtins__/list
- tuts/python3/__builtins__/memoryview
- tuts/python3/__builtins__/object
- tuts/python3/__builtins__/range
- tuts/python3/__builtins__/set
- tuts/python3/__builtins__/slice
- tuts/python3/__builtins__/str
- tuts/python3/__builtins__/tuple
- tuts/python3/__builtins__/type
- tuts/python3/tutorials/core_fundamentals/primitive_data_types
Related notes
- tuts/python3/__builtins__/__builtins__ Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/bool Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/bytes Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/complex Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/filter Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/format Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/int Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/str Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__ links to this page; shared tags: #builtins
- tuts/python3/__builtins__/bytearray links to this page; shared tags: #builtins