What are Python's built-in data types
Python has several built-in data types, which can be categorized into the following groups:
1. Numeric Types
int
: Integer numbers (e.g.,5
,-3
,1000
)float
: Floating-point numbers (e.g.,3.14
,-0.001
,2.0
)complex
: Complex numbers (e.g.,3 + 4j
,1 - 2j
)
2. Sequence Types
str
: Strings (immutable sequences of Unicode characters, e.g.,"hello"
,'Python'
)list
: Mutable ordered sequences (e.g.,[1, 2, 3]
,['a', 'b', 'c']
)tuple
: Immutable ordered sequences (e.g.,(1, 2, 3)
,('a', 'b', 'c')
)range
: Immutable sequence of numbers (e.g.,range(5)
,range(1, 10, 2)
)
3. Mapping Type
dict
: Key-value pairs (e.g.,{'name': 'Alice', 'age': 25}
)
4. Set Types
set
: Unordered, mutable collection of unique elements (e.g.,{1, 2, 3}
)frozenset
: Immutable version ofset
(e.g.,frozenset({1, 2, 3})
)
5. Boolean Type
bool
: RepresentsTrue
orFalse
6. Binary Types
bytes
: Immutable sequence of bytes (e.g.,b'hello'
)bytearray
: Mutable sequence of bytes (e.g.,bytearray(b'hello')
)memoryview
: Memory view of an object’s binary data
7. None Type
None
: Represents the absence of a value (e.g.,x = None
)
Examples:
# Numeric num_int = 42 num_float = 3.14 num_complex = 2 + 3j # Sequence text = "Hello, Python!" my_list = [1, 2, 3] my_tuple = (4, 5, 6) my_range = range(5) # Mapping person = {"name": "Alice", "age": 30} # Set my_set = {1, 2, 3} my_frozenset = frozenset({4, 5, 6}) # Boolean is_valid = True # Binary data = b"binary" byte_data = bytearray(b"bytes") # None empty_value = None
These built-in data types are fundamental to Python programming and are used to store and manipulate different kinds of data efficiently.