September 27

Data Types

Python supports several built-in data types that are used to represent different kinds of values and information. Here are some of the commonly used data types in Python:

  1. Numeric Types:
    • Integer (init): Represents whole numbers, such as 3, -7, or 0.
    • Floating-Point (float): Represents decimal numbers with fractional parts, such as 3.14 or -0.5.
    • Complex (complex): Represents numbers in the form of a + bj, where a and b are real numbers, and j represents the square root of -1.
  2. Boolean Type:
    • Boolean (bool): Represents the truth values True or False. It is often used in conditional statements and logical operations.
  3. Text Type:
    • String (str): Represents a sequence of characters enclosed in single quotes (‘ ‘) or double quotes (” “). Strings are immutable, meaning their values cannot be changed once they are created.
  4. Sequence Types:
    • List (list): Represents an ordered collection of items enclosed in square brackets [ ]. Lists can contain elements of different data types and are mutable, allowing for modifications.
    • Tuple (tuple): Similar to a list but enclosed in parentheses ( ). Tuples are immutable, meaning their values cannot be modified after creation.
    • Range (range): Represents a sequence of numbers. It is commonly used in loops and can be generated using the range() function.
  5. Mapping Type:
    • Dictionary (dict): Represents a collection of key-value pairs enclosed in curly braces { }. Keys are unique and used to access corresponding values. Dictionaries are mutable.
  6. Set Types:
    • Set (set): Represents an unordered collection of unique elements. Sets are enclosed in curly braces { } or can be created using the set() function.
    • FrozenSet (frozenset): Similar to a set, but immutable. Once created, its elements cannot be modified.
  7. Sequence of Bytes:
    • Bytes (bytes): Represents a sequence of immutable bytes.
    • Bytearray (bytearray): Represents a mutable sequence of bytes.
  8. None Type:
    • None (NoneType): Represents the absence of a value or the absence of a return value in functions.

These built-in data types provide the foundation for representing and manipulating data in Python. Additionally, Python allows for creating custom data types using classes, which can further extend the language’s capabilities.