Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
Python provides two data types to represent real numbers: float
and decimal
. The float
type is a built-in data type that uses the IEEE 754 floating-point standard, while the decimal
type is provided by the decimal
module and offers better precision and control over rounding.
This tutorial will guide you through using both the float
and decimal
types in Python.
Using float:
You can define a float value by either writing a number with a decimal point or by using scientific notation:
# Defining float values pi_float = 3.14159 large_number = 1.2e5 print(pi_float) # Output: 3.14159 print(large_number) # Output: 120000.0
Float values can be used in arithmetic operations:
x = 5.0 y = 3.5 z = x + y print(z) # Output: 8.5
Float limitations:
Float values have limited precision, which can lead to rounding errors:
a = 0.1 b = 0.2 c = a + b print(c) # Output: 0.30000000000000004
Using decimal:
To work with the decimal
type, first import the Decimal
class from the decimal
module:
from decimal import Decimal
Define a Decimal
value by passing a string or a number to the Decimal
constructor:
# Defining decimal values pi_decimal = Decimal('3.14159') large_number_decimal = Decimal(1.2e5) print(pi_decimal) # Output: 3.14159 print(large_number_decimal) # Output: 120000
Decimal values can be used in arithmetic operations:
x = Decimal('5.0') y = Decimal('3.5') z = x + y print(z) # Output: 8.5
Decimal precision and rounding:
The decimal
module provides better precision and control over rounding compared to float
. To set the global precision and rounding mode, use the getcontext()
function:
from decimal import Decimal, getcontext # Set the global precision to 3 getcontext().prec = 3 # Set the global rounding mode to ROUND_HALF_UP getcontext().rounding = 'ROUND_HALF_UP' x = Decimal('5.555') y = Decimal('3.333') z = x + y print(z) # Output: 8.89
In summary, Python provides two data types for representing real numbers: float
and decimal
. While the float
type is more efficient and commonly used for most applications, the decimal
type offers better precision and control over rounding. Understanding the differences between the two types and their use cases is essential for working with real numbers in Python.
Creating and initializing float objects in Python:
my_float = 3.14
Float literals and constants in Python:
float('inf')
for positive infinity and float('nan')
for NaN (Not a Number).positive_infinity = float('inf') nan_value = float('nan')
Operations and methods for working with floats in Python:
x = 5.0 y = 2.0 addition_result = x + y square_root = x.sqrt()
Converting between floats and other data types in Python:
float()
, int()
, and str()
allow you to convert between floats and other data types.float_to_int = int(3.14) int_to_float = float(5)
Precision and rounding with float in Python:
round()
can be used to manage precision.pi_approximation = round(3.14159265359, 2) # Rounds to 2 decimal places
Handling special cases like NaN and infinity with floats in Python:
result = 10 / 0 # Results in positive infinity is_nan = math.isnan(result)
Common challenges with floating-point arithmetic in Python:
result = 0.1 + 0.2 # May not be exactly 0.3 due to precision limitations