Python Data Types

Understanding data types is one of the most important steps when learning Python. Data types define the kind of data a variable can hold. Python is a dynamically typed language, which means you don’t need to declare the type of a variable explicitly. It automatically detects the type based on the value assigned.

In this article, we’ll explore all the standard data types in Python with examples.

What Are Data Types in Python?

Data types in Python classify data items. They tell the interpreter what kind of value a variable holds and how the program should handle it. Python has several built-in data types that are grouped into categories:

Categories of Python Data Types:

  1. Text Type: str
  2. Numeric Types: int, float, complex
  3. Sequence Types: list, tuple, range
  4. Mapping Type: dict
  5. Set Types: set, frozenset
  6. Boolean Type: bool
  7. Binary Types: bytes, bytearray, memoryview
  8. None Type: NoneType

1. Text Type

str (String)

Strings are used to store text data in Python.

# Example of a string
greeting = "Hello, Python!"
print(greeting)         # Output: Hello, Python!
print(type(greeting))   # Output: <class 'str'>

 

2. Numeric Types

int (Integer)

Integers are whole numbers, positive or negative, without decimals.

# Example of an integer
age = 25
print(age + 5)          # Output: 30
print(type(age))        # Output: <class 'int'>

float (Floating Point)

Floats represent decimal (floating point) numbers.

# Example of a float
price = 19.99
print(price * 2)        # Output: 39.98
print(type(price))      # Output: <class 'float'>

complex (Complex Number)

Used for scientific calculations involving imaginary numbers.

# Example of a complex number
z = 4 + 3j
print(z.real)           # Output: 4.0
print(z.imag)           # Output: 3.0
print(type(z))          # Output: <class 'complex'>

3. Sequence Types

list

Lists are ordered and mutable collections.

# Example of a list
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits)           # Output: ['apple', 'banana', 'cherry', 'orange']
print(type(fruits))     # Output: <class 'list'>

tuple

Tuples are ordered and immutable collections.

# Example of a tuple
coordinates = (10.5, 20.3)
print(coordinates[0])   # Output: 10.5
print(type(coordinates))# Output: <class 'tuple'>

range

Represents a sequence of numbers, often used in loops.

numbers = range(5)

Example 1: Basic Usage with One Argument

for i in range(5):
    print(i)

Output:

0
1
2
3
4

Starts at 0 and ends at 4 (not 5).

4. Mapping Type

dict (Dictionary)

Dictionaries store data in key-value pairs.

# Example of a dictionary
student = {"name": "Alice", "age": 22}
print(student["name"])  # Output: Alice
print(type(student))    # Output: <class 'dict'>

5. Set Types

set

Sets are unordered collections of unique items.

# Example of a set
unique_ids = {101, 102, 103, 101}
print(unique_ids)       # Output: {101, 102, 103}
print(type(unique_ids)) # Output: <class 'set'>

frozenset

An immutable version of a set.

immutable_ids = frozenset([201, 202, 203])

6. Boolean Type

bool

Represents logical values: True or False.

is_valid = True

7. Binary Types

bytes

Immutable sequence of bytes.

data = b"Hello"

bytearray

Mutable sequence of bytes.

buffer = bytearray(5)

memoryview

Used to access the internal data of objects that support the buffer protocol.

view = memoryview(b"Python")

8. None Type

NoneType

Used to define a null or no-value state.

# Example of None
result = None
print(result)           # Output: None
print(type(result))     # Output: <class 'NoneType'>

Type Conversion in Python

You can convert between data types using Python’s built-in functions:

x = int("5")        # str to int
y = str(25.6)       # float to str
z = list((1, 2, 3)) # tuple to list

Final Thoughts

Python’s data types are fundamental to programming in Python. Understanding how and when to use them will help you write cleaner, more efficient code. Whether you're manipulating strings, performing calculations, or storing user data, selecting the right data type is essential.