Python supports several built-in data types, each designed for specific purposes. Here's a detailed overview:
1. Numeric Types :
- int : Represents integers, whole numbers without a fractional component. Example: `x = 5`
- float : Represents floating-point numbers, numbers with a decimal point or in exponential form. Example: `y = 3.14`
- complex : Represents complex numbers in the form `a + bj`, where `a` and `b` are floats and `j` is the imaginary unit. Example: `z = 2 + 3j`
2. Sequence Types :
- str : Represents strings of characters. Strings are immutable sequences of Unicode code points. Example: `text = "Hello, World!"`
- list : Represents ordered collections of items, which can be of different types and are mutable. Example: `my_list = [1, 2, 3, 'a', 'b']`
- tuple : Similar to lists, but immutable (cannot be modified after creation). Example: `my_tuple = (1, 2, 3, 'a', 'b')`
3. Mapping Type :
- dict : Represents collections of key-value pairs. Keys must be unique and immutable, while values can be of any type. Example: `my_dict = {'a': 1, 'b': 2, 'c': 3}`
4. Set Types :
- set : Represents unordered collections of unique items. Sets are mutable, and items must be immutable. Example: `my_set = {1, 2, 3, 4}`
- frozenset : Similar to sets, but immutable. Once created, the elements cannot be changed or added. Example: `my_frozenset = frozenset({1, 2, 3})`
5. Boolean Type :
- bool : Represents Boolean values, `True` or `False`, used for logical operations and comparisons. Example: `is_valid = True`
6. None Type :
- NoneType : Represents the absence of a value or a null value. Often used as the default return value of functions that do not explicitly return anything. Example: `result = None`
7. Binary Types :
- bytes : Represents immutable sequences of bytes, often used to handle binary data. Example: `binary_data = b'hello'`
- bytearray : Mutable sequence of bytes, useful for modifying binary data. Example: `mutable_binary_data = bytearray(b'hello')`
These are the core built-in data types in Python. Each type has its own set of operations and methods for manipulation and processing. Additionally, Python allows defining custom data types through classes and object-oriented programming.
Comments
Post a Comment