Python is a versatile, dynamic language. While it doesn’t explicitly categorize data types as “primitive” or “non-primitive” like Java, understanding these concepts helps organize and manage data efficiently.
Primitive Data Types
Primitive data types are the basic data types that store a single value. These data types are immutable, which means their values cannot be modified after they are created. Commonly used primitive data types are:
- int: Stores whole numbers, such as 10 and -25.
- float: Stores decimal numbers, such as 10.5 and 3.14.
- bool: Stores one of two values: True or False.
- str: Stores a sequence of characters enclosed in single or double quotes.
Example: Here, we create variables of different primitive data types and print their types using the type() function.
x = 10
y = 10.5
is_active = True
name = "Alex"
print(type(x))
print(type(y))
print(type(is_active))
print(type(name))
Output
<class 'int'> <class 'float'> <class 'bool'> <class 'str'>
Explanation: variables x, y, is_active, and name store values of type int, float, bool, and str, respectively. The type() function returns the data type of each variable.
Non-Primitive Data Types
Non-primitive data types are used to store multiple values or collections of data. Most non-primitive data types are mutable, which means their contents can be modified after they are created. However, some types, such as tuples, are immutable. Commonly used non-primitive data types are:
- list: Stores an ordered collection of items.
- tuple: Stores an ordered collection of items that cannot be modified.
- dict: Stores data as key-value pairs.
- set: Stores an unordered collection of unique items.
Example: Here, we create variables of different non-primitive data types and print their types using the type() function.
numbers = [1, 2, 3, 4]
coordinates = (10.0, 20.0)
person = {"name": "Emma", "age": 30}
unique_numbers = {1, 2, 3, 4}
print(type(numbers))
print(type(coordinates))
print(type(person))
print(type(unique_numbers))
Output
<class 'list'> <class 'tuple'> <class 'dict'> <class 'set'>
Explanation: variables numbers, coordinates, person, and unique_numbers store values of type list, tuple, dict, and set, respectively. The type() function returns the data type of each variable.
The table below summarizes the key differences between primitive and non-primitive data types:
| Feature | Primitive Data Types | Non-Primitive Data Types |
|---|---|---|
| Stores | A single value | Multiple values or structured data |
| Mutability | Immutable | Mostly mutable (tuple is immutable) |
| Examples | int, float, bool, str | list, tuple, dict, set |
| Memory Usage | Generally uses less memory | Generally uses more memory |
| Methods | Fewer built-in methods | Provides many built-in methods |
| Copying | Assignment typically refers to immutable values | Supports shallow and deep copying (where applicable) |
| Best Used For | Numbers, text, Boolean values, and simple calculations | Collections of data, grouped values, and complex data structures |