Python Data Types

Data types are the classification or categorization of data items. Python supports the following built-in data types.

Scalar Types

  • int: Positive or negative whole numbers (without a fractional part) e.g. -10, 10, 456, 4654654.
  • float: Any real number with a floating-point representation in which a fractional component is denoted by a decimal symbol or scientific notation e.g. 1.23, 3.4556789e2.
  • complex: A number with a real and imaginary component represented as x + 2y.
  • bool: Data with one of two built-in values True or False. Notice that 'T' and 'F' are capital. true and false are not valid booleans and Python will throw an error for them.
  • None: The None represents the null object in Python. A None is returned by functions that don't explicitly return a value.

Sequence Type

A sequence is an ordered collection of similar or different data types. Python has the following built-in sequence data types:

  • String: A string value is a collection of one or more characters put in single, double or triple quotes.
  • List: A list object is an ordered collection of one or more data items, not necessarily of the same type, put in square brackets.
  • Tuple: A Tuple object is an ordered collection of one or more data items, not necessarily of the same type, put in parentheses.

Mapping Type

Dictionary: A dictionary Dict() object is an unordered collection of data in a key:value pair form. A collection of such pairs is enclosed in curly brackets. For example: {1:"Steve", 2:"Bill", 3:"Ram", 4: "Farha"}

Set Types

  • set: Set is mutable, unordered collection of distinct hashable objects. The set is a Python implementation of the set in Mathematics. A set object has suitable methods to perform mathematical set operations like union, intersection, difference, etc.
  • frozenset: Frozenset is immutable version of set whose elements are added from other iterables.

Mutable and Immutable Types

Data objects of the above types are stored in a computer's memory for processing. Some of these values can be modified during processing, but contents of others can't be altered once they are created in the memory.

Numbers, strings, and Tuples are immutable, which means their contents can't be altered after creation.

On the other hand, items in a List or Dictionary object can be modified. It is possible to add, delete, insert, and rearrange items in a list or dictionary. Hence, they are mutable objects.