by: Cobena Isaac

Python Data Types

Python Data Types

Every programming language deals with data, and Python is no exception. Data can be numbers, text, true/false values, lists of items, or even more complex structures. Whenever you create a variable, Python will check what kind of data it holds.

In Python, data types tell the interpreter what kind of value a variable holds and how it can be used. Python also has the ability to automatically detect data types when you assign a value to a variable.

What Are Data Types?

A data type defines what kind of value a variable stores and what operations you can perform with it.

For example:

1
2
3
4
x = 10          # Integer
y = 3.14        # Float
name = "Alice"  # String
is_ready = True # Boolean
  • x is an integer (int) — it stores whole numbers.
  • y is a float — it stores decimal numbers.
  • name is a string (str) — it stores text.
  • is_ready is a boolean (bool) — it stores either True or False.

Python can automatically recognize these types just by looking at the value you assign.

Checking a Variable’s Data Type

You can check what data type a variable holds by using the built-in type() function.

1
2
3
4
5
6
7
8
9
x = 10
y = 3.14
name = "Alice"
is_ready = True

print(type(x))
print(type(y))
print(type(name))
print(type(is_ready))

Output:

1
2
3
4
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>

Python will tell you exactly what type of object each variable is.

Data Types in Python

Python has built-in data types that fall into the following categories:

1️⃣ Numeric Types → int, float, complex

Numeric data types like integers and floats are used to store numbers, enabling you to perform mathematical operations like addition, subtraction, and multiplication.

1
2
3
4
5
6
7
x = 10              # int
y = 3.14            # float
z = 2 + 3j          # complex (real + imaginary part)

print(x + y)
print(z.real)        # Get real part
print(z.imag)        # Get imaginary part

Python can handle both small and very large numbers without overflow! This means you don’t have to worry about number size limits like you might in other programming languages.

2️⃣ Text Type → str

Strings are a sequence of characters enclosed in single ('), double (") or triple quotes (''' or """). It store text enclosed in quotes (' ' or " ").*

1
2
3
message = "Hello, Python!"
print(message.upper())       # Convert to uppercase
print(len(message))          # Get length of string

You can use many built-in methods to manipulate text.


3️⃣ Boolean Type → bool

Booleans store logical values: True or False. They’re often used in conditions and comparisons.

1
2
3
4
5
is_logged_in = True
is_active = False

print(is_logged_in and is_active)
print(10 > 5)

When you compare numbers or strings, Python will return a boolean value — True or False.


4️⃣ Sequence Types → list, tuple, range

These types hold collections of items.

  1. List — is Mutable (you can change its contents):
1
2
3
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits)
  1. Tuple — is Immutable (you cannot change it):
1
2
colors = ("red", "green", "blue")
print(colors[0])

Range — Represents a sequence of numbers:

1
2
numbers = range(5)
print(list(numbers))

💬 You can use list() to view a range as a list.


5️⃣ Set Types → set, `frozenset

Sets store unique items — no duplicates allowed!

1
2
numbers = {1, 2, 3, 3, 2}
print(numbers)  # Duplicates removed

If you want a set that cannot be changed, use frozenset():

1
frozen = frozenset([1, 2, 3])

Python can use sets for fast lookups and removing duplicates efficiently.


6️⃣ Mapping Type → dict

Dictionaries store key–value pairs. You can use them to map one piece of data to another.

1
2
person = {"name": "Alice", "age": 25}
print(person["name"])

You can also add new key-value pairs:

1
2
person["city"] = "Toronto"
print(person)

Python will check if the key already exists before updating or adding new data.


7️⃣ Binary Types → bytes, bytearray, memoryview

These types store data in binary form, often used for files, images, or network data.

1
2
data = b"Hello"
print(type(data))       # bytes

If you want to modify binary data, use bytearray() instead.


8️⃣ None Type → NoneType

This special data type represents the absence of a value.

1
2
3
result = None
print(result)
print(type(result))

Python uses None to represent the absence of a value. It acts as a placeholder to indicate that a variable, like result, doesn’t currently contain any meaningful data.

Changing Data Types (Type Casting)

Casting in Python allows you to convert between data types easily.

1
2
3
4
5
6
x = 5              # int
y = float(x)       # Convert int to float
z = str(x)         # Convert int to string

print(y)
print(z)

You can convert strings that represent numbers back into actual numbers:

1
2
num = int("10")
print(num + 5)

With this, Python will check whether the string is numeric before conversion — otherwise, you’ll get an error.

Why Data Types Matter
  • Python uses data types to know how to store values in memory.
  • Data types determine what operations are allowed (e.g., you can’t add a number to a string directly).
  • Understanding data types helps you debug, optimize, and write cleaner code.
🧾 Quick Reference Table
Category Type Example Mutable?
Sequence list [1, 2, 3] ✅ Yes
  tuple (1, 2, 3) ❌ No
  range range(5) ❌ No
Set set {1, 2, 3} ✅ Yes
  frozenset frozenset([1, 2, 3]) ❌ No
Mapping dict {"key": "value"} ✅ Yes
Binary bytes b"hello" ❌ No
  bytearray bytearray(b"hello") ✅ Yes
  memoryview memoryview(b"data") ✅ Yes
Try It Yourself!

Quickly check all built-in types in Python by running:

1
print(dir(__builtins__))

You can explore a variable’s details using:

1
2
3
x = 10
print(type(x))
print(isinstance(x, int))

isinstance() lets you check if a variable belongs to a specific type.

In Summary
  • You can check a variable’s type using type() or isinstance().
  • Python will automatically assign types based on the values you use.
  • Python has built-in data types grouped into numeric, text, boolean, sequence, set, mapping, binary, and NoneType.
  • Lists and dictionaries are the most used in everyday coding.
  • Data types can be converted using casting functions like int(), float(), str().
  • Choosing the right type improves clarity, efficiency, and correctness of your programs.

👉 Next step: Explore Python Operators to see how you can work with these data types.

Hope this tutorial helps! Read our other tutorials to learn more.