In Python, there are several built-in data types that are used to store and manipulate data.
Here are the main ones:
1. Numeric Types
int: Integer type, used to represent whole numbers.- Example:
x = 5
- Example:
float: Floating-point type, used to represent real numbers (decimals).- Example:
x = 3.14
- Example:
complex: Used for complex numbers with a real and imaginary part.- Example:
x = 2 + 3j
- Example:
2. Sequence Types
list: An ordered, mutable collection of items. It can store elements of different data types.- Example:
x = [1, 2.5, "Hello", True]
- Example:
tuple: An ordered, immutable collection of items. It is similar to a list but cannot be changed once defined.- Example:
x = (1, 2.5, "Hello")
- Example:
range: Represents a sequence of numbers, often used in loops.- Example:
x = range(5)
- Example:
3. Text Type
str: A string type used to represent text (a sequence of characters).- Example:
x = "Hello, World!"
- Example:
4. Mapping Type
dict: A dictionary type, which is an unordered collection of key-value pairs.- Example:
x = {"name": "Alice", "age": 25}
- Example:
5. Set Types
set: An unordered collection of unique items.- Example:
x = {1, 2, 3, 4}
- Example:
frozenset: Similar to a set, but immutable (cannot be changed).- Example:
x = frozenset([1, 2, 3, 4])
- Example:
6. Boolean Type
bool: A type for Boolean values, which can either beTrueorFalse.- Example:
x = True
- Example:
7. Binary Types
bytes: Immutable sequence of bytes.- Example:
x = b"hello"
- Example:
bytearray: Mutable sequence of bytes.- Example:
x = bytearray([65, 66, 67])
- Example:
memoryview: A memory view object allows access to the internal data of an object without copying it.- Example:
x = memoryview(b"hello")
- Example:
8. None Type
None: Represents the absence of a value or a null value.- Example:
x = None
- Example:
These data types in Python provide flexibility to work with a variety of data in different scenarios, whether you're dealing with numbers, collections, or textual data.