In Python, there are several main data types, also known as built-in or primitive data types, that are used to represent different kinds of values. The main data types in Python include:
Numeric Types:
- int: Represents integers (whole numbers) like 5, -10, 0, etc.
- float: Represents floating-point numbers with decimal places, like 3.14, -2.5, etc.
- complex: Represents complex numbers in the form of a + bj, where a and b are floats and j represents the imaginary unit.
Boolean Type:
- bool: Represents either True or False. Booleans are often used in logical expressions and control flow statements.
Sequence Types:
- str: Represents strings of characters, such as “hello”, “world”, etc.
- list: Represents ordered, mutable (changeable) sequences of elements enclosed in square brackets, such as [1, 2, 3], [‘apple’, ‘banana’], etc.
- tuple: Represents ordered, immutable (unchangeable) sequences of elements enclosed in parentheses, such as (1, 2, 3), (‘red’, ‘green’, ‘blue’), etc.
Mapping Type:
- dict: Represents key-value pairs enclosed in curly braces. It allows you to store and retrieve values based on their associated keys, such as {‘name’: ‘John’, ‘age’: 25}, {‘fruit’: ‘apple’, ‘color’: ‘red’}, etc.
Set Types:
- set: Represents an unordered collection of unique elements enclosed in curly braces or created using the set() constructor, such as {1, 2, 3}, {‘apple’, ‘banana’}, etc.
- frozenset: Similar to a set, but immutable (unchangeable).
None Type:
- None: Represents the absence of a value. It is often used as a default value or to indicate the absence of a meaningful result.
These are the main built-in data types in Python. Additionally, Python is a dynamically typed language, which means you don’t need to explicitly declare the data type of a variable. The interpreter infers the type based on the value assigned to the variable.