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 decimal/float type (float)

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.

  1. 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
    
  2. 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
    
  3. 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
    
  4. 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.

  1. Creating and initializing float objects in Python:

    • Description: Float objects in Python represent real numbers. You can create and initialize them by assigning a decimal value to a variable.
    • Code example:
      my_float = 3.14
      
  2. Float literals and constants in Python:

    • Description: Float literals are directly specified in the code. Python also provides constants like float('inf') for positive infinity and float('nan') for NaN (Not a Number).
    • Code example:
      positive_infinity = float('inf')
      nan_value = float('nan')
      
  3. Operations and methods for working with floats in Python:

    • Description: You can perform various operations on float objects, including basic arithmetic operations and using built-in methods.
    • Code example:
      x = 5.0
      y = 2.0
      addition_result = x + y
      square_root = x.sqrt()
      
  4. Converting between floats and other data types in Python:

    • Description: Conversion functions like float(), int(), and str() allow you to convert between floats and other data types.
    • Code example:
      float_to_int = int(3.14)
      int_to_float = float(5)
      
  5. Precision and rounding with float in Python:

    • Description: Floating-point numbers have limited precision. Rounding functions like round() can be used to manage precision.
    • Code example:
      pi_approximation = round(3.14159265359, 2)  # Rounds to 2 decimal places
      
  6. Handling special cases like NaN and infinity with floats in Python:

    • Description: Floats can represent special cases like NaN and infinity. Checking for these cases is important in certain calculations.
    • Code example:
      result = 10 / 0  # Results in positive infinity
      is_nan = math.isnan(result)
      
  7. Common challenges with floating-point arithmetic in Python:

    • Description: Floating-point arithmetic can sometimes lead to unexpected results due to limited precision. This can result in small rounding errors.
    • Code example:
      result = 0.1 + 0.2  # May not be exactly 0.3 due to precision limitations