Lesson

Python Data Types

10 min reading
Free Course

Python Data Types: Core Built-In Types and Mutability

Every value in Python belongs to a specific built-in data type that defines what operations you can perform on it. Knowing your data types prevents runtime errors like trying to add a number directly to text.

Python groups its core types into numeric, text, sequence, mapping, set, boolean, and binary categories. You can inspect any object's type at runtime with type() or verify inheritance with isinstance().

Overview of Built-In Python Types

Category Type Names Mutable? Example
Text str No "hello"
Numeric int, float, complex No 42, 3.14
Sequence list, tuple, range list only [1, 2], (1, 2)
Mapping dict Yes {"id": 1}
Set set, frozenset set only {1, 2, 3}
Boolean bool No True, False
Empty NoneType No None

Checking Types Safely in Code

When your function accepts flexible input, prefer isinstance() over type(x) == ... because isinstance() handles subclasses properly.

order_price = 49.95
tags = ["python", "backend"]
profile = {"name": "Tariq", "verified": True}
empty_result = None

print(isinstance(order_price, float))  # True
print(isinstance(tags, list))          # True
print(type(empty_result))              # <class 'NoneType'>

Common Mistakes to Avoid

  • Concatenating strings and integers: "Port: " + 8080 raises TypeError; convert with str(8080) or use an f-string f"Port: {8080}".
  • Comparing mutable and immutable types: Tuples and strings cannot change in place, whereas lists, dicts, and sets mutate in place.
  • Testing for None with ==: Always write if result is None: rather than if result == None:.

Frequently Asked Questions

What is the difference between mutable and immutable types?

Mutable objects like list and dict allow you to add, remove, or edit items in place. Immutable objects like int, str, and tuple create a new object whenever you change their value.

What does None mean in Python?

None is Python's single null object representing the absence of a value, commonly returned by functions that have no explicit return statement.

Are Python booleans related to integers?

Yes. bool is a subclass of int, so True equals 1 and False equals 0 in numeric contexts.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
Python Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum