Python Data Types

Before starting the discussion, please note that everything in Python is an object. Following are the types built into Python:

  1. None – This type has a single value and there is a single object with this value. This object is accessed through the built-in name None. It is used to signify the absence of a value. For example, it is returned from functions that don’t explicitly return anything. Its truth value is false.
  2. NotImplemented – This type has a single value and there is a single object with this value. This object is accessed through the built-in name NotImplemented. It’s truth value is true. NotImplemented should be returned by numeric methods(like __eq__(), __lt__(), __add__() etc) if they do not provide implementation for the operands provided.
  3. Ellipsis – This type has a single value and there is a single object with this value. This object is accessed through the literal … or the built-in name Ellipsis. Its truth value is true.
  4. numbers.Number – These are created by numeric literals. Numbers are used as input to arithmetic operations the output of which is again numbers. Numeric objects in Python are immutable, i.e. once created their value never changes. In Python there are three types of numbers: integers, floating point numbers and complex numbers.
    • numbers.Integral – These represent positive and negative integers. There are two types of integers:
      1. Integers(‘int’) – These represent numbers in an unlimited range, subject to available virtual memory. Example: x=1
      2. Booleans(bool) – These represent the value True and False. The Boolean type is a subtype of integer type. Boolean values behave like the values 0 (False) and 1( True) in almost all context except then these are converted to string objects “True” and “False”. Example: x = True
    • numbers.Real (float) – These represent machine-level double precision floating point numbers. The range depends on the underlying machine architecture and implementation. Python does not support single precision floating point numbers.
    • numbers.Complex (complex) – These represent complex numbers as a pair of machine-level double precision floating point numbers. The complex numbers have the same conditions and limitations as floating point numbers. The real and imaginary parts of a complex number z can be retrieved through the read-only attributes z.real and z.imag. Example: x = 10.5
  5. Sequences – Sequences represent finite ordered sets indexed by non-negative numbers. If the number of elements is n then the indexes in the sequence are from 0 to n-1. Elements of the sequence can be accessed using the notation seq[index] where seq is the sequence and index is the index of the sequence.
    • Immutable sequences – An immutable sequence object can not be changed once created. Following are the immutable sequences:
      1. Strings – A string is a sequence of Unicode code points which can range between U+0000 – U+10FFFF. Python does not have a ‘char’ type. Example: x = "Hello World"
      2. Tuples – A tuple is a sequence of objects. It is immutable in nature. Example: x = (10,21)
      3. Bytes – A Bytes object is an immutable sequence of 8-bit bytes. The items of the sequence are represented by integers in the range of 0<=x<256.
    • Mutable sequences – Unlike Immutable sequences, mutable sequences can be changed after they are created. Following are mutable sequences:
      1. Lists – A list is a sequence of objects. It is mutable in nature. A list is represented as a comma separated list within square brackets. Example: [10, 21]
      2. Byte Array – A bytearray is a mutable array. It is same like Bytes with the only difference it is mutable.
  6. Sets type – Sets represent unordered, finite set of unique and immutable objects. There are following set types:
    • Sets – They represent a mutable set. Sets can be modified. Example – {"Sunday","Monday","Tuesday"}
    • Frozen Sets – These represent an immutable set. Example: x = frozenset({"Sunday", "Monday", "Tuesday"})
  7.  Mappings – A Mapping is usually explained as “key-value” pair. There is a single Mappings type:
    • Dictionary – An example of dictionary is x = {"name" : "John", "age" : 50}. Here “name” and “age” are keys and “John” and 50 are values mapped to the keys. Dictionaries preserve insertion order.

There are other types which should be mentioned here.

  • Callable types
    1. User defined functions
    2. Instance methods
    3. Generator functions
    4. Coroutine functions
    5. Classes
    6. Class Instances
  • Modules
  • Custom classes
  • Class instances
  • I/O objects (file objects)

How to get Data Type of an object in Python

Python is dynamically typed language. The type of the variable is defined when you assign a value to that variable.
You can get the data type of any object by using the type() function:

x = "Hello World"

x = 10


<class 'str'>
<class 'int'>

To read in detail about the Python data types, please refer Python’s official documentation. That is the most comprehensive source for any topic on Python for us and anyone else.