Data Types

Data Types Supported by Python

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
  • float: Floating-point type, used to represent real numbers (decimals).
    • Example: x = 3.14
  • complex: Used for complex numbers with a real and imaginary part.
    • Example: x = 2 + 3j

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]
  • 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")
  • range: Represents a sequence of numbers, often used in loops.
    • Example: x = range(5)

3. Text Type

  • str: A string type used to represent text (a sequence of characters).
    • Example: x = "Hello, World!"

4. Mapping Type

  • dict: A dictionary type, which is an unordered collection of key-value pairs.
    • Example: x = {"name": "Alice", "age": 25}

5. Set Types

  • set: An unordered collection of unique items.
    • Example: x = {1, 2, 3, 4}
  • frozenset: Similar to a set, but immutable (cannot be changed).
    • Example: x = frozenset([1, 2, 3, 4])

6. Boolean Type

  • bool: A type for Boolean values, which can either be True or False.
    • Example: x = True

7. Binary Types

  • bytes: Immutable sequence of bytes.
    • Example: x = b"hello"
  • bytearray: Mutable sequence of bytes.
    • Example: x = bytearray([65, 66, 67])
  • memoryview: A memory view object allows access to the internal data of an object without copying it.
    • Example: x = memoryview(b"hello")

8. None Type

  • None: Represents the absence of a value or a null value.
    • Example: x = None

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.