0Pricing
Learn AI with Python · Lesson

Python Data Types for Data Science

int, float, complex, bool, str — how precision and type choice affect data analysis.

Python Data Types for Data Science is a free Learn AI with Python lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Numbers in Data Science

Python has two core numeric types: int (arbitrary precision whole numbers) and float (64-bit IEEE 754 decimals). Choosing the right one prevents subtle data bugs.

int is Exact and Unbounded

Python integers never overflow. They grow as large as memory allows, which is great for exact counts and large factorials.

big = 2 ** 200
print(big)
print(type(big))   # <class 'int'>

float Precision Pitfall

Floats cannot represent every decimal exactly. The classic surprise: 0.1 + 0.2 is not exactly 0.3.

print(0.1 + 0.2)          # 0.30000000000000004
print(0.1 + 0.2 == 0.3)   # False

Comparing Floats Safely

Never test floats with ==. Use math.isclose with a tolerance instead.

import math
print(math.isclose(0.1 + 0.2, 0.3))  # True

Decimal for Money

For currency, use decimal.Decimal, which stores exact base-10 values. Always build Decimals from strings, not floats, to avoid inheriting float error.

from decimal import Decimal
price = Decimal("0.10") + Decimal("0.20")
print(price)   # 0.30

bool is a Subclass of int

In Python True equals 1 and False equals 0. This lets you sum booleans to count matches.

mask = [True, False, True, True]
print(sum(mask))      # 3 matches
print(True + True)    # 2

Counting with Booleans

This bool-as-int behavior is everywhere in NumPy and pandas: summing a boolean mask counts how many rows satisfy a condition.

ages = [12, 20, 35, 17, 40]
adults = sum(a >= 18 for a in ages)
print(adults)   # 3

Inspecting Type with type()

type(x) returns the exact class of a value. Useful when a column unexpectedly holds strings instead of numbers.

print(type(5))      # <class 'int'>
print(type(5.0))    # <class 'float'>
print(type("5"))    # <class 'str'>

Checking Type with isinstance()

Prefer isinstance over type == because it respects inheritance. Note that a bool passes an int check.

print(isinstance(5, int))        # True
print(isinstance(True, int))     # True (bool subclasses int)
print(isinstance(5.0, (int, float)))  # True

Coercion Pitfalls

Mixing types coerces silently. Integer division of floats yields floats; concatenating a number with a string raises a TypeError. Clean your data types early.

print(5 / 2)        # 2.5  (true division always float)
print(5 // 2)       # 2    (floor division)
# "id-" + 5         # TypeError
print("id-" + str(5))  # id-5

Float Surprises in Aggregation

Summing many floats accumulates tiny errors. For large datasets prefer NumPy, which uses pairwise summation to reduce drift.

vals = [0.1] * 10
print(sum(vals))    # 0.9999999999999999
# NumPy: np.sum(vals) is more numerically stable

Quick Check

Test your grasp of Python types.

Recap

Key numeric facts for data work:

  • int is exact and unbounded
  • float is approximate, use math.isclose not ==
  • Decimal("...") for money
  • bool is an int: sum(mask) counts
  • type() shows the class, isinstance() respects inheritance
  • Watch silent coercion in division and concatenation

Frequently asked questions

Is the “Python Data Types for Data Science” lesson free?

Yes — the full text of “Python Data Types for Data Science” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Python Data Types for Data Science”?

int, float, complex, bool, str — how precision and type choice affect data analysis. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Python Data Types for Data Science” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn AI with Python lesson?

Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Virtual Environments and pip
  2. Jupyter Notebooks for Data Science
  3. Python Data Types for Data Science
  4. Working with the Python REPL and IPython
← Back to Learn AI with Python