Skip to content
Happy Programming Guide
Start learning
Python

Python Data Types Explained

Strings, integers, floats, booleans, lists, dictionaries and None — what each is for, how to convert between them, and how to check a type when code misbehaves.

Every value in Python has a type, and the type decides what you can do with it. Knowing which type you are holding explains most confusing beginner errors.

Python
print(type("hello"))   # <class 'str'>
print(type(42))        # <class 'int'>
print(type(3.14))      # <class 'float'>
print(type(True))      # <class 'bool'>

str — text#

Python
name = "Ayesha"
print(len(name))          # 6
print(name.upper())       # AYESHA
print(name[0])            # A
print("she" in name)      # False (case matters)
print(name.replace("A", "@"))

Strings cannot be changed in place. name.upper() gives you a new string; the original is untouched unless you reassign it.

int and float — numbers#

Python
count = 7          # int
price = 19.99      # float

print(10 / 3)      # 3.3333333333333335  — always a float
print(10 // 3)     # 3    — whole-number division
print(10 % 3)      # 1    — the remainder
print(2 ** 10)     # 1024 — to the power of

The % operator is more useful than it looks: n % 2 == 0 is how you test whether a number is even.

bool — True and False#

Python
is_open = True
print(5 > 3)          # True
print(bool(""))       # False — empty things are falsy
print(bool([]))       # False
print(bool(0))        # False

Empty strings, empty lists and zero all count as false in a condition. That is why if name: works as “did they type anything?”.

list, dict, tuple, set — collections#

Python
scores = [10, 8, 9]                      # list — ordered, changeable
person = {"name": "Ada", "age": 36}      # dict — labelled values
point = (3, 4)                           # tuple — ordered, fixed
unique = {1, 2, 2, 3}                    # set — no duplicates → {1, 2, 3}

See Python lists and Python dictionaries for detail.

None — the absence of a value#

Python
result = None

if result is None:
    print("Nothing yet")

Use is None, not == None. A function with no return statement returns None, which is behind a lot of “why is my variable empty” confusion.

Converting between types#

Python
int("42")        # 42
float("3.5")     # 3.5
str(42)          # "42"
list("abc")      # ['a', 'b', 'c']
int(3.9)         # 3 — chops off, does not round
round(3.9)       # 4

Conversion can fail loudly:

Python
int("hello")     # ValueError: invalid literal for int() with base 10

Guard it when the value comes from a person:

Python
raw = input("Enter a number: ")
if raw.isdigit():
    number = int(raw)
else:
    print("That was not a number.")

Why 0.1 + 0.2 is not 0.3#

Python
print(0.1 + 0.2)   # 0.30000000000000004

Floats are stored in binary, and some decimal fractions have no exact binary form — the same way 1/3 has no exact decimal form. This is not a Python bug; almost every language behaves this way.

For display, round it. For money, work in whole paisa or cents, or use the decimal module.

Python
print(round(0.1 + 0.2, 2))   # 0.3

Mutable versus immutable#

Some types can be changed in place; some cannot.

Changeable Fixed once created
list, dict, set str, int, float, bool, tuple

This is why b = a on a list means both names see the same data, but on a number it does not. Covered in Python variables explained.

Questions people ask#

How do I check a type in a condition?

Use isinstance(value, int) rather than comparing type(value) == int. It handles inheritance correctly and reads better.

What is the difference between a list and a tuple?

A tuple cannot be changed after it is created. Use one for fixed groupings such as coordinates or a database row, where accidental edits would be a bug.

Should I write type hints?

They are optional and Python does not enforce them, but editors use them for better autocomplete and error checking. Worth adopting once your files get past a hundred lines or so.

Where to go next#

Next lessonPython if / elif / else statements

Keep reading

Keep going — pick your next guide

The fastest way to improve is to read one guide, then build the thing it describes. Start with the basics, or jump straight to a project.

Ask a question or share what worked

Your email address will not be published. Required fields are marked *