What Are Data Types in Python?

In programming, a data type defines what kind of value a variable can hold and how that value can be used. Python automatically determines a variable’s type at runtime, so explicit type declarations are not required. This flexibility makes Python a dynamically typed language.

You can check the type of any value or variable using the built-in type() function.

Built-In Data Types in Python

Python includes several built-in data types. These are categories of objects the language supports natively:

CategoryData Types
Text Typestr
Numeric Typesint, float, complex
Sequence Typeslist, tuple, range
Mapping Typedict
Set Typesset, frozenset
Boolean Typebool
Binary Typesbytes, bytearray, memoryview
None TypeNoneType

Numeric Data Types

Numeric types represent numbers:

  • int: Whole numbers (e.g. 10, -5).

  • float: Decimal numbers (e.g. 3.14, 2.0).

  • complex: Numbers with real and imaginary parts (e.g. 2 + 3j).

Example:

x = 10

Text Type

  • str: Used for text, created with single ('...') or double ("...") quotes.

Example:

name = "Python" print(type(name)) # <class 'str'>

Strings can also be multi-line using triple quotes ("""...""").

Sequence Types

Sequence types allow storing ordered collections:

  • list: Mutable sequence (e.g. [1, 2, 3]).

  • tuple: Immutable sequence (e.g. (1, 2, 3)).

  • range: Represents a sequence of numbers (e.g. range(5)).

Example:

fruits = ["apple", "banana", "cherry"] print(type(fruits)) # <class 'list'>

Mapping Type

  • dict: Stores key-value pairs (e.g. {"name": "Alice", "age": 30}).

Example:

person = {"name": "Alice", "age": 30} print(type(person)) # <class 'dict'>

Dictionaries are unordered (prior to Python 3.7) and allow fast lookup by key.

Set Types

  • set: Unordered collection of unique elements.

  • frozenset: Immutable version of a set.

Example:

numbers = {1, 2, 3, 3} print(numbers) # {1, 2, 3} print(type(numbers)) # <class 'set'>

Sets automatically remove duplicates.

Boolean Type

  • bool: Holds one of two values: True or False. Often used in conditions and loops.

Example:

a = True print(type(a)) # <class 'bool'>

Booleans are also subclasses of integers (True == 1, False == 0).

Binary Types

Binary types represent raw bytes of data:

  • bytes: Immutable sequence of bytes.

  • bytearray: Mutable sequence of bytes.

  • memoryview: View of another binary object without copying.

Example:

data = b"Hello" print(type(data)) # <class 'bytes'>

None Type

  • NoneType: Represents the absence of a value. There is only one None object in Python.

Example:

result = None print(type(result)) # <class 'NoneType'>

This type often acts as a placeholder.

Checking and Converting Types

Use type() to check a data type. You can convert between types using functions like int(), float(), str(), etc.

Example:

x = "10" y = int(x) print(type(y)) # <class 'int'>