Python Data Types
Data types are the classification or categorization of data items. Python supports the following built-in data 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
False. Notice that ‘T’ and ‘F’ are capital.
falseare not valid booleans and Python will throw an error for them.
- None: The
Nonerepresents the null object in Python. A
Noneis returned by functions that don’t explicitly return a value.
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.
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:
- 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.