Chapter 1 of ?
python 12 min read

Python for Data Science — Chapter 2: Types

Module 1 · Chapter 2

Data Types

Every value in a dataset has a type. Data science bugs are often type bugs — knowing Python's core types, and how to convert between them, is foundational to data cleaning and analysis.

2.1 What Is a Type?

A type is how Python represents different kinds of data — integers like 11, real numbers like 21.213, or words. Python checks the type of any value using the built-in type() function.

Common Python Data Types Every value in Python has a type — check it with the type() function EXPRESSION type(...) OUTPUT MEANING 11 <class 'int'> Whole number, positive or negative 21.213 <class 'float'> Real number, includes decimals "Data Science" <class 'str'> A sequence of characters (text) True / False <class 'bool'> One of exactly two values

2.2 Integers (int) and Floats (float)

Integers are whole numbers, positive or negative. Floats are real numbers — they include integers, but also every value in between them. There's always another float between any two floats you pick.

Floats Fill the Gaps Between Integers Between 0 and 1: 0 (int) 1 (int) 0.1, 0.2, 0.3 ... 0.9 (all floats) zoom in ↓ Between 0.5 and 0.6: 0.5 0.6 0.51, 0.52, 0.53 ... 0.59 (still floats — and we could zoom in again) Between any two floats there's always another float in between — down to Python's precision limit.

This is why measurements, prices, and averages in a dataset are almost always floats, while counts (number of rows, number of customers) are integers.

2.3 Strings (str)

A string is a sequence of characters — text. Names, labels, and free-text fields in a dataset are all strings.

name = "Data Science"
print(type(name))
OutputPython Output
<class 'str'>

2.4 Type Casting

You can deliberately change the type of a value — this is called type casting. It's one of the most common things you'll do when cleaning messy data.

Type Casting: Converting Between Types int float str bool float(2) → 2.0 int(1.1) → 1 ⚠ loses decimal str(10) → "10" int("10") → 10 int("ten") → ⚠ ValueError bool(1) → True bool(0) → False int(True) → 1, int(False) → 0 str(21.2) → "21.2"
Interactive Type Casting Playground

Enter any string, number, or boolean literal, select a target type, and observe Python's conversion result and explanation note.

In [2]:
int(1.1)
Out[2]:
1
int(1.1) drops everything after the decimal point — it does not round. 1.1 → 1.

2.5 Booleans (bool)

A Boolean can take on exactly two values: True or False — note the capital first letter. Booleans and numbers are closely linked: True behaves like 1, and False behaves like 0, when cast to int or float.

Key Takeaways

  • type() tells you the type of any value.
  • int = whole numbers; float = real numbers; str = text; bool = True/False.
  • Floats are dense — there's always another float between any two floats.
  • Type casting converts a value from one type to another (int(), float(), str(), bool()).
  • int → float is always safe. float → int can lose information. A non-numeric string → int/float raises an error.

Practice Exercises

Chapter 2 Quiz — Test Your Knowledge

1. What is the result of evaluating int(9.99) in Python?

2. Which type conversion will raise a ValueError?

3. What data type is returned by the expression type(True)?

Done with this chapter?
Mark it complete to track your progress and unlock your certificate.
Next Up

Learner Reviews

Write a Review
Share your experience to help other learners.
Your Rating *