Quick answer: Python’s core data types are int, float, bool, str, list, tuple, and dict. The first four are single values; list, tuple, and dict are collections that can hold many values. Choose a list when contents change, a tuple when they must not, and a dictionary when you want to look values up by name instead of position.

Part 2 of our Python series. Missed the setup? Start with environment setup and your first script.

Why data types matter

Every bug you will write for the next six months comes from this article, so it is worth reading twice. Python declares no types up front — a name points at a value, and the value carries its own type. That freedom is powerful and dangerous: age + 1 works when age is 25 and crashes when age is "25".

The single-value types

count = 7            # int — whole numbers, no size limit
price = 19.99        # float — decimals (and the famous imprecision)
name = "Asha"        # str  — text, in single or double quotes
ok = True            # bool — True or False, capital first letter

Watch the float surprise once and never uninstall that knowledge:

>>> 0.1 + 0.2
0.30000000000000004

This is not a Python bug — floats are binary approximations of decimals. Compare floats with a tolerance (math.isclose(0.1 + 0.2, 0.3)), not with ==.

Lists: the workhorse collection

A list is an ordered, changeable sequence. Create with square brackets, index from zero.

scores = [88, 92, 79]
scores[0]        # 88  — first element
scores[-1]       # 79  — minus indexes count from the end
len(scores)      # 3
scores.append(95)          # now [88, 92, 79, 95]
scores[1] = 90             # lists are mutable: edit in place
scores[0:2]                # [88, 90] — slicing takes start, not end

A list can mix types, and a very common mix is a list of strings:

days = ["Mon", "Tue", "Wed"]

Tuples: the sealed collection

A tuple is a list that cannot be modified after creation. Parentheses instead of brackets:

point = (12.5, 4.0)
# point[0] = 20      -> TypeError: tuple does not support item assignment

Use tuples for coordinates, RGB colors, or the multiple values a function returns. The immutability is a feature: nothing anywhere in the codebase can scramble a tuple.

Dictionaries: values with names

A dict maps keys to values — a real-world contact card, not a numbered list.

student = {"name": "Asha", "age": 21, "course": "Python"}
student["age"]                 # 21
student["age"] = 22            # update an existing key
student["city"] = "Chennai"    # add a new key — no error, just appears
"age" in student               # True — membership checks KEYS

Pulling from a missing key is the classic crash; the safe pattern is get:

student["phone"]          # KeyError!
student.get("phone")      # None — safe default
student.get("phone", "n/a")  # "n/a" — explicit default

Complete executable example

# types-demo.py — core data types in one runnable script
name = "Asha"                     # str
age = 21                          # int
balance = 149.50                  # float
active = True                     # bool
courses = ["Python", "SQL"]       # list — changes freely
point = (4, 7)                    # tuple — sealed forever
profile = {"city": "Chennai"}     # dict — keyed lookup

courses.append("Statistics")
profile["mentor"] = "Vikram"

print(f"{name}, age {age}, balance {balance}, active={active}")
print("courses:", courses)
print("point:", point)
print("profile:", profile)
print("age next year:", age + 1)   # ints are exact; floats would not be

Line by line: variables are assigned with = and no type keyword; lists grow with .append; dicts gain entries just by assignment; f-strings interpolate variables into text with {}.

Common mistakes and edge cases

  • TypeError: can only concatenate str — mixing "Age: " + age where age is an int. Convert: str(age), or better, use an f-string: f"Age: {age}".
  • IndexError on off-by-one — with len(scores) == 3, scores[3] fails. Last valid index is always len(x) - 1.
  • Modifying a tuple — tuples are immutable; rebuild or use a list.
  • KeyError on dict lookups — use .get() when the key may not exist.
  • ValueError: invalid literal for int() — int(input()) with non-numeric text. Convert only after validating, or wrap in try/except (Article 4 territory).

Key takeaways and challenge

  • int/float/bool/str hold one value; list/tuple/dict hold many.
  • Lists are ordered and mutable, tuples ordered and sealed, dicts keyed and mutable.
  • Floats are approximations; compare with tolerance.

Challenge: build inventory.py with a dict of three grocery items mapped to prices, print each as item: price using an f-string loop hint, increase all prices by 10 percent, and print the average. Stuck on looping? That is the very next article.

Want one-to-one help getting ramped in Python? Ampersand Academy offers hands-on training.

What are the core data types in Python?

int, float, bool, and str hold single values; list, tuple, and dict are collections. A list is ordered and mutable, a tuple is ordered and immutable, and a dict maps keys to values.

When should I use a list instead of a tuple?

Use a list when the contents will change, such as items appended while a program runs. Use a tuple for fixed data like coordinates or RGB colors, where immutability protects the values.

Why does 0.1 + 0.2 not equal 0.3 in Python?

Floats store binary approximations of decimal numbers, so 0.1 + 0.2 produces 0.30000000000000004. Compare floats with math.isclose() instead of the == operator.

How do I avoid a KeyError with dictionaries?

Use the .get() method with a default value, such as student.get(‘phone’, ‘n/a’). Plain bracket access raises KeyError when the key does not exist.

Can a Python list hold different types at once?

Yes. A list such as [42, ‘hello’, 3.14, True] is valid because each element is just a reference to a value. Most production code keeps a list homogeneous for clarity.

Last updated on · Written by