Lesson 1 of 5 · 7 min · The language

Python data types and mutability

Covers: Python 3.14. Updated 13 Nov 2026. Part of the Data Analyst roadmap.

What you will learn

  • The built-in types you use every day
  • The difference between a value that can change (mutable) and one that cannot (immutable)
  • Why two names can point at the same list, and how to copy safely
  • The classic bug of a mutable default argument

In simple words

Think of a variable as a sticky label you put on a box. The box holds a value. Some boxes are sealed: a number or a piece of text can never be changed, you can only stick your label on a new box. Other boxes are open, like a list: you can add and remove things inside while the label stays put. Knowing which boxes are sealed and which are open explains many surprising bugs.

Words to know

  • Variable: a name that points to a value.
  • Type: the kind of value, such as a number, text or list.
  • Mutable: a value that can be changed in place, such as a list.
  • Immutable: a value that cannot be changed after it is made, such as a string or a tuple.

The types you will use

Python decides the type from the value; you do not declare it.

age = 24            # int
price = 199.5       # float
name = "Asha"       # str
is_paid = True      # bool
nothing = None      # NoneType: "no value"
skills = ["sql", "excel"]          # list
point = (17.38, 78.48)             # tuple
profile = {"name": "Asha", "age": 24}   # dict
tags = {"sql", "excel", "sql"}     # set: duplicates vanish

Check a type with type(age) and test it with isinstance(age, int). Two details trip up beginners:

  • / always gives a float (7 / 2 is 3.5); // is whole-number division (7 // 2 is 3); % is the remainder.
  • Floats are approximate: 0.1 + 0.2 is 0.30000000000000004. For money, use whole paise as integers or the decimal module.

Strings are text. "Hello"[0] is "H", "Hello"[-1] is "o", and slicing "Hello"[1:3] gives "el". Use f-strings to build text: f"{name} is {age}".

Mutable and immutable

Immutable types never change after they are created: int, float, str, bool, tuple (and frozenset). Operations return a new value:

s = "data"
t = s.upper()     # a new string "DATA"
print(s)          # data   (s is unchanged)

Mutable types change in place: list, dict, set.

skills = ["sql"]
skills.append("excel")
print(skills)     # ['sql', 'excel']

A tuple is immutable, but a mutable object inside it can still change: ([1, 2], 3) lets you append to the inner list. Immutability is about the container, not about what it holds.

Two names, one list

Assigning a mutable value does not copy it. It gives the same box a second label:

a = [1, 2, 3]
b = a            # b is another name for the same list
b.append(4)
print(a)         # [1, 2, 3, 4]  (a changed too)

Check with a is b (True), which tests whether two names point at the same object, unlike ==, which tests whether the values are equal. To make a real copy:

b = a.copy()     # or list(a) or a[:]

These are shallow copies: the outer list is new but the items inside are shared. For nested data (a list of lists) use copy.deepcopy(a) from the copy module.

The mutable default argument bug

Default values are created once, when the function is defined, not each time it is called:

def add_skill(skill, skills=[]):     # bug
    skills.append(skill)
    return skills

print(add_skill("sql"))     # ['sql']
print(add_skill("excel"))   # ['sql', 'excel']   surprise

The standard fix is None as the default:

def add_skill(skill, skills=None):
    if skills is None:
        skills = []
    skills.append(skill)
    return skills

Truthiness and None

In a condition, empty values count as false: 0, 0.0, "", [], {}, set() and None. Everything else is true. Compare to None with is, not ==: if value is None:.

Common mistakes

  • Changing a list you are looping over, which skips items. Loop over a copy, or build a new list.
  • Using == where is is meant for None checks (and the reverse for values).
  • Using a list as a dictionary key. Keys must be immutable (hashable): use a tuple.
  • Believing b = a copies a list.

Try it yourself

  1. Create a list a, set b = a, change b, print a. Then repeat with b = a.copy().
  2. Try s = "data"; s[0] = "D". Read the error, then fix it by building a new string.
  3. Reproduce the mutable default bug, then fix it with None.
  4. Put a list inside a tuple, append to the inner list, and explain why that is allowed.

Check yourself

Which of these can be changed in place: str, list, tuple, dict?

list and dict are mutable. str and tuple are immutable, so operations on them return new objects.

After a = [1, 2] and b = a, you run b.append(3). What is a?

[1, 2, 3]. Both names point at the same list, because assignment does not copy.

What is wrong with def f(x, items=[])?

The default list is created once and shared by every call that does not pass items, so changes pile up between calls. Use items=None and create a new list inside.

Can a list be a dictionary key?

No. Keys must be hashable, which in practice means immutable. Use a tuple instead.

Interview questions

What is the difference between a shallow copy and a deep copy?

A shallow copy makes a new outer container but reuses the same inner objects, so changing a nested list shows up in both copies. A deep copy (copy.deepcopy) copies everything inside too, so the two are fully independent.

What is the difference between is and ==?

== compares values for equality. is compares identity, whether both names refer to the very same object. Use is for None.

Why are strings immutable, and what does that mean for building a long string in a loop?

Immutability makes strings safe to share and use as dictionary keys. Repeated += in a loop creates new strings each time; collect the pieces in a list and call "".join(pieces) once.

Summary

  • Numbers, strings, booleans and tuples are immutable; lists, dicts and sets are mutable.
  • Assignment shares a mutable object; copy it when you need your own.
  • Never use a mutable default argument; use None.
  • Use is None for None checks and tuples for composite keys.

Going deeper

See how much of this you can explain

A free mock interview asks you new questions on Python essentials for analysts for Data Analyst and scores every answer, with what to fix.