# Python Data Types Overview: Numbers, Strings, Lists, and More

Source: https://devdocs10.com/articles/python/python-data-types  
By Murat Bo · Published 2026-10-28 · DevDocs10 (https://devdocs10.com)

Python data types tell you what kind of value a variable holds and what you can do with it. Numbers, text, lists, dictionaries, and booleans each behave differently, so choosing the right type affects your code’s correctness and readability. This article shows the main built-in Python data types, how they work, and the mistakes beginners make most often.

> **Difficulty:** Beginner
>
> **Helpful to know first:** basic variables, assignment with `=`, and how to run a Python file.

## How Python Data Types Store Different Kinds of Values

In Python, every value has a type. The type tells Python whether the value is a whole number, decimal number, piece of text, true/false value, collection of items, or something else.

You can check a value’s type with `type()`. You usually do not need to write the type when you create a variable, because Python figures it out for you.

```python
age = 21
price = 19.99
name = "Maya"
is_active = True

print(type(age))
print(type(price))
print(type(name))
print(type(is_active))

# Output:
# <class 'int'>
# <class 'float'>
# <class 'str'>
# <class 'bool'>
```

Here, Python stores four different kinds of data. The same assignment syntax works for all of them, but each type has its own rules for arithmetic, comparison, and storage.

## Common Python Built-in Data Types and What They Are For

The most common built-in data types are `int`, `float`, `bool`, `str`, `list`, `tuple`, `dict`, `set`, and `None`. These cover most beginner and day-to-day Python programs.

| Type | What it holds | Example | Common use |
| --- | --- | --- | --- |
| `int` | Whole numbers | `42` | Counting, ages, loop indexes |
| `float` | Decimal numbers | `3.14` | Measurements, averages, money in simple examples |
| `bool` | True or False | `True` | Conditions and flags |
| `str` | Text | `"hello"` | Names, messages, file paths |
| `list` | Ordered, changeable collection | `[1, 2, 3]` | Items you may add, remove, or reorder |
| `tuple` | Ordered, unchangeable collection | `(1, 2, 3)` | Fixed data, coordinates, records |
| `dict` | Key-value pairs | `{"name": "Ana"}` | Look up values by key |
| `set` | Unique items, unordered | `{1, 2, 3}` | Remove duplicates, test membership |
| `None` | No value | `None` | Placeholder, missing result, optional data |

### Numbers in Python: int and float

`int` stores whole numbers, while `float` stores decimal numbers. Python uses them for arithmetic, but the result type can change depending on the operation.

```python
whole = 10
fraction = 3.5

print(whole + 2)
print(whole / 4)
print(whole // 4)
print(fraction + 1)

# Output:
# 12
# 2.5
# 2
# 4.5
```

The `/` operator always gives a float in Python 3, even when the division is even. The `//` operator performs floor division, which drops the decimal part.

### Text in Python: str

A `str` stores text. Strings use quotes, and you can join them with `+`, repeat them with `*`, or pull out characters by index.

```python
first_name = "Ada"
last_name = "Lovelace"

print(first_name + " " + last_name)
print(first_name[0])
print(last_name[-1])

# Output:
# Ada Lovelace
# A
# e
```

Strings are immutable, which means you cannot change one character in place. If you need a modified string, create a new one.

### Collections in Python: list, tuple, dict, and set

Collections hold more than one value. They are useful when one variable needs to represent a group of related items.

```python
fruits = ["apple", "banana", "cherry"]
point = (10, 20)
person = {"name": "Mina", "age": 18}
unique_scores = {70, 85, 70}

print(fruits[1])
print(point[0])
print(person["name"])
print(unique_scores)

# Output:
# banana
# 10
# Mina
# {70, 85}
```

The important differences are mutability and structure. Lists can change, tuples cannot, dictionaries map keys to values, and sets store unique items.

### Boolean and None values

`bool` and `None` are special because they often control program flow instead of storing data for display.

```python
is_logged_in = False
result = None

print(is_logged_in)
print(result is None)

# Output:
# False
# True
```

`None` means “no value yet” or “nothing returned.” It is not the same thing as `0`, `False`, or an empty string, even though all of them can behave like false in an `if` statement.

## How Python Types Behave in Real Programs

### Mutable and immutable types change how assignment works

Some Python types can be changed after they are created. These are mutable types, such as lists and dictionaries. Others are immutable, such as integers, floats, strings, and tuples.

That difference matters because assignment usually copies a reference to the same object, not a fresh deep copy of the data.

```python
a = [1, 2]
b = a
b.append(3)

print(a)
print(b)

# Output:
# [1, 2, 3]
# [1, 2, 3]
```

Both names refer to the same list object. Changing the list through one name changes what the other name sees.

### Python chooses a type based on the value, not the variable name

Python does not enforce that a variable always keeps the same type. A name can point to an integer now and a string later, although that usually makes code harder to read.

```python
value = 10
print(type(value))

value = "ten"
print(type(value))

# Output:
# <class 'int'>
# <class 'str'>
```

This flexibility is part of Python’s design, but it also means you need to pay attention to the actual value a variable holds at the moment you use it.

### Some types can be used as dictionary keys and set items, and others cannot

Dictionary keys and set items must be hashable. Immutable built-in types like strings, integers, and tuples of hashable values work well. Lists do not.

```python
prices = {"apple": 1.5, "pear": 2.0}
print(prices["apple"])

# Output:
# 1.5
```

> A hashable value can be used as a stable dictionary key because Python can compute a reliable hash for it.

### Truthiness lets values act like True or False

Python treats some values as false in conditions, even when they are not the boolean `False`. Empty sequences and collections are false; non-empty ones are true.

```python
items = []

if items:
    print("Has items")
else:
    print("Empty")

# Output:
# Empty
```

This is useful because you often want to test whether a collection has anything in it without writing a separate length check.

## Predict the Output

### Integer division versus true division

```python
print(7 / 2)
print(7 // 2)
```

What does this print?

**Answer:**

`/` performs true division, so the first line prints `3.5`. `//` performs floor division, so the second line prints `3`.

```bash
# Output:
# 3.5
# 3
```

### Aliasing a mutable list

```python
numbers = [1]
copy = numbers
copy.append(2)
print(numbers)
```

What happens?

**Answer:**

copy and numbers refer to the same list object. Appending through one name changes the shared list, so the print call shows both values.

```bash
# Output:
# [1, 2]
```

## Find the Bug

### Using a mutable default list

This function looks like it creates a fresh list each time, but it does not. Find the bug.

```python
def add_item(item, items=[]):
    items.append(item)
    return items

print(add_item("a"))
print(add_item("b"))
```

**Answer:**

The default list is created once when Python defines the function, not each time the function runs. That means later calls reuse the same list and keep old items.

**Fixed code:**

```python
def add_item(item, items=None):
    if items is None:
        items = []
    items.append(item)
    return items

print(add_item("a"))
print(add_item("b"))
```

The fixed version works because it creates a new list only when the caller does not provide one.

## Common Mistakes With Python Data Types

### Adding a number to a string without converting it first

Beginners often read input and then try to use it like a number. But `input()` always returns text.

**Problem:** Python cannot add an `int` and a `str`, so this raises a type error.

```python
age = input("Enter your age: ")
print(age + 1)
```

**Fix:** Convert the input to `int` before doing arithmetic.

```python
age = int(input("Enter your age: "))
print(age + 1)
```

The corrected version works because both values are numbers before the addition happens.

### Trying to change an immutable value in place

Strings and tuples cannot be modified one item at a time. You need to build a new value instead.

**Problem:** This tries to replace one character inside a string, which Python does not allow.

```python
name = "Lina"
name[0] = "M"
```

**Fix:** Create a new string from slices and the new character.

```python
name = "Lina"
name = "M" + name[1:]
print(name)
```

The new string works because strings are immutable and must be replaced, not edited.

### Using a list where a dictionary key is required

Some data structures need hashable values, but lists are mutable and cannot be used as dictionary keys.

**Problem:** Python raises `TypeError: unhashable type: 'list'` because lists can change after creation.

```python
locations = {[1, 2]: "corner"}
```

**Fix:** Use a tuple if the grouped values should stay fixed.

```python
locations = {(1, 2): "corner"}
print(locations[(1, 2)])
```

The corrected version works because tuples are immutable and can be hashed safely.

### Indexing past the end of a sequence

Lists and strings have valid index positions from `0` to `len(value) - 1`. Going too far causes an index error.

**Problem:** The last valid index in a three-item list is `2`, not `3`.

```python
items = ["a", "b", "c"]
print(items[3])
```

**Fix:** Use `len()` or loop directly over the items.

```python
items = ["a", "b", "c"]
for item in items:
    print(item)
```

The corrected version works because it does not guess an index that may not exist.

## When to Use Each Python Data Type and When to Choose Something Else

Choose the smallest type that matches the data and the operations you need. That usually leads to simpler code and fewer bugs.

| If you need... | Use this type | Why |
| --- | --- | --- |
| A count or whole number | `int` | Exact whole-number arithmetic |
| A decimal value | `float` | Supports fractional results |
| Text | `str` | Built for human-readable content |
| A list you will edit | `list` | Ordered and mutable |
| Fixed related values | `tuple` | Ordered and immutable |
| Named values | `dict` | Good for lookup by key |
| Unique items only | `set` | Automatically removes duplicates |

> If you are choosing between a list and a tuple, ask one question: will the collection change? If yes, use a list. If no, use a tuple.

### Data type choice and common alternatives

| Choice | Best when | Not ideal when |
| --- | --- | --- |
| `list` | You need to append, remove, or reorder items | You need a stable, fixed record |
| `tuple` | The values belong together and should not change | You need to edit items often |
| `dict` | You want labels for values | The data is just an ordered sequence |
| `set` | You care about membership and uniqueness | You care about order or duplicates |

### Interview questions about Python data types

#### What is the difference between mutable and immutable types?

Mutable types can be changed after creation, like lists and dictionaries. Immutable types cannot be changed in place, like strings, integers, and tuples.

#### Why does Python have both lists and tuples?

Lists are for data that may change. Tuples are for fixed groups of values. Using a tuple can make your intent clearer and can help prevent accidental modification.

#### Why does `type()` matter?

Knowing the type tells you which operations are valid. It helps you debug errors like adding text to numbers or using a list where a dictionary key is needed.

## Try It Yourself

Write a small program that stores a student’s name, age, and grades, then prints a short summary.

- Create a string for the student’s name.
- Create an integer for the age.
- Create a list for three grades.
- Print the student’s name, age next year, and the highest grade.

**Expected output:**

```python
Student: Noor
Next year: 15
Highest grade: 98
```

**Hint:** Use `max()` to find the highest number in the list.

**Solution:**

```python
student_name = "Noor"
age = 14
grades = [91, 98, 87]

print(f"Student: {student_name}")
print(f"Next year: {age + 1}")
print(f"Highest grade: {max(grades)}")
```

## Summary

- Python values have types, and the type decides what operations are valid.
- `int` and `float` store numbers, `str` stores text, and `bool` stores True or False.
- Lists and dictionaries are mutable; strings, numbers, and tuples are immutable.
- `None` means no value, not zero or False.
- Assignment often shares the same mutable object, so be careful with aliasing.
- Pick the type that matches the data and the way you plan to use it.

Next, learn about `Python type conversion` so you can safely turn text into numbers and avoid type errors when working with user input.
