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:
- 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.
- Boolean Type:
- Boolean (bool): Represents the truth values True or False. It is often used in conditional statements and logical operations.
- 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.
- 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.
- 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.
- 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.
- Sequence of Bytes:
- Bytes (bytes): Represents a sequence of immutable bytes.
- Bytearray (bytearray): Represents a mutable sequence of bytes.
- 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.