Python for Data Analytics & AutomationLesson 3 of 41
Lesson 3 of 41

String Operations, Slicing & Modern F-String Formatting

Overview & Learning Objectives

Variables in Python are object references rather than fixed memory containers. Python employs dynamic typing, where types are verified at runtime. For data analysts handling financial transactions, understanding numeric types (integers with arbitrary precision vs IEEE 754 floating-point numbers) is essential to avoid catastrophic rounding errors in revenue reporting.

  • Understand variable assignment, reference semantics, and dynamic typing.
  • Differentiate between int, float, bool, and NoneType objects.
  • Handle floating-point precision quirks using Python's `decimal` and `round()` modules.

Industry Standard

Standard: Financial audit standards at JPMorgan Chase and Goldman Sachs forbid using standard IEEE 754 floats for currency arithmetic, requiring `decimal.Decimal` with fixed decimal precision.

Real-World Example: ScoopCraft Order Price Computation

ScoopCraft bills an ice cream order with base price, CGST (9%), SGST (9%), and delivery surcharge.

Item / FeeTypeAmountData Type
Belgian Choco TubBase Price₹350.00Decimal("350.00")
Taxes (18% GST)Tax Percentage0.18float
Quantity OrderedUnit Count2int
Express PackagingAdd-on FlagTruebool

Syntax & Core Code Implementation

# Variable assignment & type inspection
item_name = "Mango Tub"
unit_price = 249.50
quantity = 3
is_in_stock = True

print(type(unit_price))  # 

# Float precision issue
total_float = 0.1 + 0.2
print(total_float)  # 0.30000000000000004

# Financial grade precision
from decimal import Decimal
exact_total = Decimal("0.10") + Decimal("0.20")
print(exact_total)  # 0.30

Parameters & Arguments Breakdown

  • int: Arbitrary-precision integer; expands to fit any number limited only by available memory.
  • float: Double-precision 64-bit IEEE 754 floating point number with ~15-17 decimal digits of precision.
  • Decimal: Fixed-point and floating-point arithmetic class from `decimal` module for exact currency math.
  • type(): Built-in function returning the runtime class/type of any Python object.

Sample Walkthrough (Q&A)

Question: Why does `0.1 + 0.2 == 0.3` evaluate to `False` in Python?

Code Pattern: 0.1 + 0.2 == 0.3

Result: False (evaluates to 0.30000000000000004)

Explanation: Binary floating-point hardware cannot represent 1/10 exactly in base-2, creating tiny recurring binary fractional rounding errors.

Common Mistakes & Limitations

  • Using standard float division when integer counting is required, causing integer IDs to become floats with potential precision truncation.
  • Comparing floats directly with `==` instead of `math.isclose()` or `np.isclose()`.

Keyboard Shortcuts (Jupyter & VS Code)

ActionWindowsMac
Inspect Object Documentation in JupyterShift + TabShift + Tab
Quick Variable Type Check in Consoletype(var)type(var)

Pro Tips & Modern Best Practices

  • Use `math.isclose(a, b, rel_tol=1e-9)` for floating point comparisons.
  • Use Python 3.10+ type hints (`price: float = 199.0`) with MyPy to catch type errors before pipeline deployment.