Last Updated : 11 Jul, 2025
A float (floating-point number) is a data type used to represent real numbers with a fractional component. It is commonly used to store decimal values and perform mathematical calculations that require precision.
Characteristics of Float TypeExample:
Python
a = 10.5 # Float declaration
b = -3.14 # Negative float
c = 2.0 # Even if it looks like an integer, it's a float
d = 1.23e4 # Scientific notation (1.23 × 10⁴ = 12300.0)
e = 5e-3 # Scientific notation (5 × 10⁻³ = 0.005)
print(a,b,c,d,e)
Built-in Methods for float type
Python provides several built-in methods for float objects.
Built-in Methods forfloat
Type in Python Method Description float.as_integer_ratio() Returns a tuple representing the float as a ratio of two integers. float.conjugate() Returns the same float value (useful for compatibility with complex numbers). float.fromhex(s)
Converts a hexadecimal string to a float. (Static method) float.hex() Returns the hexadecimal representation of the float. float.is_integer() Returns True if the float is an integer (has no decimal part), else False. float.__abs__() Returns the absolute value of the float. float.__add__(other) Adds two float values (self + other). float.__sub__(other) Subtracts two float values (self - other). float.__mul__(other) Multiplies two float values (self * other). float.__truediv__(other) Performs true division (self / other). float.__floordiv__(other) Performs floor division (self // other). float.__mod__(other) Returns the remainder of division (self % other). float.__pow__(other) Returns the float raised to the power of other (self ** other). float.__round__(n) Rounds the float to n
decimal places. 1. float.as_integer_ratio()
The as_integer_ratio()
method returns a tuple of two integers, whose ratio equals the float. This method is useful for precise representation of floating-point numbers as fractions, which can help avoid floating-point precision errors in arithmetic calculations. The returned integers represent the numerator and denominator of the fraction.
f = 2.75
ratio = f.as_integer_ratio()
print(ratio)
Here, 2.75
is exactly represented as 11/4
. The method breaks down the float into an exact fraction by multiplying it by a power of 2 internally and simplifying it into two integers.
float.conjugate()
The conjugate()
method returns the same float value. This method exists primarily for compatibility with complex numbers, where the conjugate of a complex number negates its imaginary part. Since a float has no imaginary part, calling conjugate()
on a float simply returns itself.
f = 5.5
print(f.conjugate())
This method does not modify the float; it just returns the same value. It ensures compatibility when working with complex numbers, where the conjugate of a + bi
is a - bi
.
The fromhex()
method converts a hexadecimal string representation of a floating-point number into a float. This is useful when dealing with binary representations or low-level floating-point operations.
s = "0x1.91eb851eb851fp+1"
a = float.fromhex(s)
print(a)
The hexadecimal string represents a floating-point number in scientific notation. The p+1
denotes a power of two exponent. Converting from hexadecimal ensures precise representation of binary floating-point numbers.
The hex()
method returns the hexadecimal representation of a float. This is useful for debugging floating-point precision issues and for storing exact binary representations.
0x1.91eb851eb851fp+1
The output is a hexadecimal scientific notation representing the float. The p+1
means multiplying by 2^1
. This format is useful for exact floating-point storage and computation.
The is_integer()
method checks if a float has no decimal part and returns True
if it is equivalent to an integer. This is useful when working with numerical computations where integer-like behavior is required.
print((4.0).is_integer())
print((4.5).is_integer())
Here, 4.0
is equivalent to the integer 4
, so is_integer()
returns True
. However, 4.5
has a decimal part, so it returns False
.
The __abs__()
method returns the absolute value of a float, which is the non-negative version of the number. It is equivalent to the built-in abs()
function.
f = -7.3
print(f.__abs__())
print(abs(f))
The negative value -7.3
is converted to its positive equivalent 7.3
. This is useful when working with distances, magnitudes, or other computations where only the positive value is needed.
The __add__()
method performs addition between two float values. This is automatically used when we use the +
operator.
a = 5.5
b = 2.2
print(a.__add__(b))
print(a + b)
Here, 5.5 + 2.2
results in 7.7
. The +
operator internally calls the __add__()
method.
The __sub__()
method performs subtraction between two float values. It is used when we apply the -
operator.
a = 10.5
b = 3.2
print(a.__sub__(b))
print(a - b)
Here, 10.5 - 3.2
results in 7.3
. The -
operator calls __sub__()
internally.
The __mul__()
method performs multiplication between two float values.
a = 4.2
b = 2.0
print(a.__mul__(b))
print(a * b)
Multiplication of 4.2 * 2.0
results in 8.4
. The *
operator invokes __mul__()
internally.
The __truediv__()
method performs true division (returns a float even when dividing two integers).
a = 7.5
b = 2.5
print(a.__truediv__(b))
print(a / b)
The division 7.5 / 2.5
results in 3.0
, ensuring a floating-point output.
The __floordiv__()
method performs floor division, which returns the largest integer less than or equal to the quotient.
a = 7.5
b = 2.5
print(a.__floordiv__(b))
print(a // b)
The quotient is 3.0
with no remainder, so the floor division is the same as normal division here.
The __mod__()
method returns the remainder of division.
a = 10.5
b = 4.0
print(a.__mod__(b))
print(a % b)
Here, 10.5 / 4.0
results in 2
with a remainder of 2.5
.
The __pow__()
method raises the float to the power of another number.
a = 3.0
b = 2.0
print(a.__pow__(b))
print(a ** b)
The expression 3.0 ** 2.0
calculates 3.0
raised to the power of 2.0
, which is 9.0
.
The __round__()
method rounds the float to n
decimal places.
f = 3.14159
print(f.__round__(2))
print(round(f, 2))
The float 3.14159
is rounded to 3.14
when specifying 2
decimal places.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4