1.

What are the common built-in data types in Python?

Answer»

There are several built-in data types in Python. Although, Python doesn't require data types to be defined explicitly during variable declarations type errors are LIKELY to occur if the knowledge of data types and their compatibility with each other are NEGLECTED. Python provides type() and isinstance() functions to check the type of these variables. These data types can be grouped into the following categories-

  • None Type:
    None keyword represents the null values in Python. Boolean equality operation can be performed using these NoneType objects.
Class NameDescription
NoneTypeRepresents the NULL values in Python.
  • Numeric Types:
    There are three distinct numeric types - integers, floating-point numbers, and complex numbers. Additionally, booleans are a sub-type of integers.
Class NameDescription
intStores integer literals including hex, octal and binary numbers as integers
floatStores literals containing decimal values and/or exponent signs as floating-point numbers
complexStores complex numbers in the form (A + Bj) and has attributes: real and imag
boolStores boolean value (True or False).

Note: The standard library also includes fractions to store rational numbers and decimal to store floating-point numbers with user-defined precision.

  • Sequence Types:
    According to Python Docs, there are three basic Sequence Types - lists, tuples, and range objects. Sequence types have the in and not in operators defined for their traversing their elements. These operators share the same priority as the comparison operations.
Class NameDescription
listMutable sequence used to store collection of items.
tupleImmutable sequence used to store collection of items.
rangeRepresents an immutable sequence of numbers generated during execution.
strImmutable sequence of Unicode CODE points to store textual data.

Note: The standard library also includes additional types for processing:
1. Binary data such as bytearray bytes memoryview , and
2. Text strings such as str.

  • Mapping Types:

A mapping object can MAP hashable values to random objects in Python. Mappings objects are mutable and there is currently only one standard mapping type, the dictionary.

Class Name Description
dictStores comma-separated list of key: value pairs
  • Set Types:
    Currently, Python has two built-in set types - set and frozenset. set type is mutable and supports methods like add() and remove(). frozenset type is immutable and can't be modified after creation.
Class NameDescription
setMutable unordered collection of distinct hashable objects.
frozensetImmutable collection of distinct hashable objects.

Note: set is mutable and thus cannot be used as key for a dictionary. On the other hand, frozenset is immutable and thus, hashable, and can be used as a dictionary key or as an element of another set.

  • Modules:
    Module is an additional built-in type supported by the Python Interpreter. It supports one special operation, i.e., attribute ACCESS: mymod.myobj, where mymod is a module and myobj references a name defined in m's symbol table. The module's symbol table resides in a very special attribute of the module __dict__, but direct assignment to this module is neither possible nor recommended.
  • Callable Types:
    Callable types are the types to which function call can be applied. They can be user-defined functions, instance methods, generator functions, and some other built-in functions, methods and classes.
    Refer to the documentation at docs.python.org for a detailed view of the callable types.


Discussion

No Comment Found