Dataslope logoDataslope

Data Types

A tour of Python's built-in types and how to convert between them

Python comes with a small, focused set of built-in types. You'll spend 99% of your time juggling these. Each one gets its own page later; this is the map.

Every snippet on this page runs in your browser—no setup required.

Real-world context: Dynamic vs static typing

Python is dynamically typed: the type of a value is checked at runtime, not compile time. This is different from statically typed languages like Java, Go, or TypeScript, where you declare types upfront and the compiler enforces them.

Dynamic typing tradeoffs:

  • ✅ Faster to write: no boilerplate type declarations.
  • ✅ More flexible: a function can accept any type that supports the operations it uses (duck typing).
  • ❌ Errors surface late: typos and type mismatches aren't caught until the code runs.
  • ❌ Harder to refactor: tooling can't always infer what type a variable should be.

Type annotations (Python 3.5+) bridge the gap: you can add optional type hints and use tools like mypy to catch type errors before runtime, while keeping Python's runtime flexibility.

def greet(name: str) -> str:
    return f"Hello, {name}"

In production codebases, type annotations are increasingly standard practice.

The cheat sheet

CategoryTypeExampleMutable?
Numericint42, -7, 0b1010No
Numericfloat3.14, 1e-3No
Numericcomplex3+2jNo
Textstr"hello", 'a'No
BooleanboolTrue, FalseNo
Sequencelist[1, 2, 3]Yes
Sequencetuple(1, 2, 3)No
Sequencerangerange(10)No
Mappingdict{"a": 1}Yes
Setset{1, 2, 3}Yes
Setfrozensetfrozenset({1, 2})No
Binarybytesb"\\x00\\x01"No
Binarybytearraybytearray(b"...")Yes
NothingNoneTypeNoneNo

Type hierarchy

Python's type system is built on a hierarchy rooted in object. Every value in Python is an instance of some class, and every class (eventually) inherits from object.

bool is a subclass of int

This is a quirk of Python's design: True and False are instances of bool, which inherits from int. So True == 1 and False == 0 are both true, and you can do arithmetic with booleans: True + True is 2.

Checking the type

type(obj) returns the exact class; isinstance(obj, T) checks the class or any of its parent classes.

Code Block
Python 3.13.2

isinstance is what you should use in real code, because it respects inheritance:

Code Block
Python 3.13.2

bool inherits from int

Because bool is a subclass of int, boolean values behave as integers in arithmetic contexts:

print(True + True)   # 2
print(False * 10)    # 0

This is usually harmless, but it can surprise you in functions that expect an integer.

Converting between types

Every built-in type doubles as a converter when you call it as a function.

Code Block
Python 3.13.2
Code Block
Python 3.13.2
Code Block
Python 3.13.2
Code Block
Python 3.13.2

Mutability matters

Two patterns to remember:

  1. Immutable types (int, float, str, tuple, frozenset) cannot be changed after creation. Operations like s.upper() return a new string.
  2. Mutable types (list, dict, set, bytearray) can be changed in place. Methods like list.append() modify the object directly.
Code Block
Python 3.13.2
Code Block
Python 3.13.2
Code Block
Python 3.13.2

None

None is the unique value of type NoneType. It represents the absence of a value. Use is None or is not None to check for it (not ==).

Code Block
Python 3.13.2
Code Block
Python 3.13.2

Why 'is None' not '== None'?

None is a singleton—there's only one None object in memory. is checks object identity (same object?), while == checks value equality. For singletons like None, True, and False, always use is.

Challenges

Challenge
Python 3.13.2
Parse a CSV-ish line

A variable line holds the string "42,3.14,hello,True". Split it on commas and produce a list called parsed containing, in order:

  1. The integer 42
  2. The float 3.14
  3. The string "hello"
  4. The boolean True

Hard-code the types (you do not need to detect them automatically).

Challenge
Python 3.13.2
Safe int conversion

Write a function safe_int(s, default=0) that tries to convert the string s to an integer. If the conversion fails (e.g., s is "abc"), return default instead. Use a try/except block.

Check your understanding

QuestionSelect one

Which of the following Python types is immutable?

list

dict

tuple

set

QuestionSelect one

What does isinstance(True, int) return?

False

True

A TypeError

None

QuestionSelect one

After this code runs, what is the type of x?

x = "42"
x = int(x)

str

int

float

A TypeError

QuestionSelect one

Which statement about None is false?

None is the only value of type NoneType.

You should use is None to check for None.

A function that doesn't return anything raises an error.

None represents the absence of a value.

The next pages drill into each type, starting with numbers.

On this page