In programming, a data type is a classification or categorization that specifies which type of value a variable can hold. Data types are essential because they determine how data is stored in memory and what operations can be performed on that data. Python, like many programming languages, supports several built-in data types. Here are some of the common data types in Python:
-
Numeric Data Types:
- int: Represents integers (whole numbers). Example:
x = 5
- float: Represents floating-point numbers (numbers with decimal points). Example:
y = 3.14
- complex: Represents complex numbers. Example:
z = 2 + 3j
- int: Represents integers (whole numbers). Example:
-
Sequence Types:
- str: Represents strings (sequences of characters). Example:
text = "Hello, World"
- list: Represents lists (ordered, mutable sequences). Example:
my_list = [1, 2, 3]
- tuple: Represents tuples (ordered, immutable sequences). Example:
my_tuple = (1, 2, 3)
- str: Represents strings (sequences of characters). Example:
-
Mapping Type:
- dict: Represents dictionaries (key-value pairs). Example:
my_dict = {'name': 'John', 'age': 30}
- dict: Represents dictionaries (key-value pairs). Example:
-
Set Types:
- set: Represents sets (unordered collections of unique elements). Example:
my_set = {1, 2, 3}
- frozenset: Represents immutable sets. Example:
my_frozenset = frozenset([1, 2, 3])
- set: Represents sets (unordered collections of unique elements). Example:
-
Boolean Type:
- bool: Represents Boolean values (
True
orFalse
). Example:is_valid = True
- bool: Represents Boolean values (
-
Binary Types:
- bytes: Represents immutable sequences of bytes. Example:
data = b'Hello'
- bytearray: Represents mutable sequences of bytes. Example:
data = bytearray(b'Hello')
- bytes: Represents immutable sequences of bytes. Example:
-
None Type:
- NoneType: Represents the
None
object, which is used to indicate the absence of a value or a null value.
- NoneType: Represents the
-
Custom Data Types:
- You can also define your custom data types using classes and objects.