Python Data Types - Data Types in Python
Python Built-in Data Types
Data Types in Python
Python Data Types
Every value in Python has a data type. Since everything is an object in Python programming language. Python data types are actually classes and variables are instance (object) of classes.
In this tutorial, you can learn about different data types used in Python. After reading this article you understand what are data types in Python.
what is Data Type ?
The data type describes the characteristic of a variable.
In computer science and computer programming, a data type or simply type is an attribute of data which tells the compiler or interpreter how the programmer intends to use the data. Most programming languages support basic data types of integer numbers, floating-point numbers or real numbers, characters and Booleans.
A data type constrains the values that an expression, such as a variable or a function, might take. The data type defines the operations that can be done on the data. A data type provides a set of values from which an expression i.e. variable, function etc. may take its values.
What are all the data types in Python ?
Following are the Python Built-in Data Types. The following section describe the standard types that are built into the interpreter.
- bool
- bytearray
- bytes
- complex
- dict
- ellipsis
- float
- frozenset
- int
- list
- NoneType
- NotImplementedType
- range
- set
- str
- tuple
What are Primitive Data Types in Python ?
These are the most basic data types in Python. Python has four primitive data types:
- Integers
- Float
- Strings
- Boolean
Primitive Types are the most basic data structures. They are the building block for data manipulation, and such contains pure, simple values of data.
Following data types are the built-in data type in Python use as primitive data types.
- int
- float
- str
- bool
What is meant by Primitive Data Type ?
In computer science, a primitive is a fundamental data type that cannot be broken down into a more simple data type. For example, an integer is a primitive data type, while an array, which can store multiple data types, is not a primitive data type.
Following are the Python Built-in Data Types By Category
Binary Types : bytes, bytearray, memoryview
The core built-in types for manipulating binary data are bytes and bytearray. They are supported by memoryview which uses the buffer protocol to access the memory of other binary objects without needing to make a copy.
The memoryview objects allow Python code to access the internal data of an object that supports the buffer protocol without copying.
Boolean Type : bool
Boolean values are the two constant objects False and True. They are used to represent truth values (although other values can also be considered false or true).
In numeric contexts (for example when used as the argument to an arithmetic operator), they behave like the integers 0 and 1, respectively.
The built-in function bool() can be used to convert any value to a Boolean, if the value can be interpreted as a truth value.
Mapping Type : dict
A mapping object maps hashable values to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapping type, the dictionary.
Dictionaries can be created by placing a comma-separated list of key: value pairs within braces, or by the dict constructor.
Python Numbers
Numeric Types in Python : int, float, complex
There are three distinct numeric types : integers, floating point numbers, and complex numbers. In addition, Booleans are a subtype of integers. Integers have unlimited precision.
Numbers are created by numeric literals or as the result of built-in functions and operators.
The constructors int(), float(), and complex() can be used to produce numbers of a specific type.
Sequence Types : list, tuple, range
There are three basic sequence types: lists, tuples, and range objects. Sequences of the same type also support comparisons. In particular, tuples and lists are compared lexicographically by comparing corresponding elements. This means that to compare equal, every element must compare equal and the two sequences must be of the same type and have the same length.
Lists are mutable sequences, typically used to store collections of homogeneous items.
Tuples are immutable sequences, typically used to store collections of heterogeneous data.
The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.
Set Types : set, frozenset
A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference.
There are currently two built-in set types, set and frozenset. The set type is mutable, the contents can be changed using methods like add() and remove(). Since it is mutable, it has no hash value and cannot be used as either a dictionary key or as an element of another set.
The frozenset type is immutable and hashable its contents cannot be altered after it is created; it can therefore be used as a dictionary key or as an element of another set.
Text Type : str
Textual data in Python is handled with str objects, or strings. Strings are immutable sequences of Unicode code points.
The memoryview objects allow Python code to access the internal data of an object that supports the buffer protocol without copying.
What is Python bool data type ?
Python bool data type holds the Boolean value True or False. The bool data type in Python is immutable.
What is Python bytearray data type ?
Python bytearray data type holds the sequence of bytes. The bytearray data type in Python is mutable. The bytearray objects are a mutable counterpart to bytes objects.
What is Python bytes data type ?
Python bytes data type holds the sequence of bytes. The bytes data type in Python is immutable. Bytes objects are immutable sequences of single bytes. Since many major binary protocols are based on the ASCII text encoding.
What is Python complex data type ?
Python complex data type holds the Complex number with real and imaginary parts. The complex data type in Python is immutable.
What is Python dict data type ?
Python dict data type holds the Associative array (or dictionary) of key and value pairs; can contain mixed types (keys and values), keys must be a hashable type. The dict data type in Python is mutable.
What is Python ellipsis data type ?
Python ellipsis placeholder to be used as an index in NumPy arrays. The ellipsis in Python is immutable.
What is Python float data type ?
Python float data type holds the Double precision floating point number. The precision is machine dependent but in practice is generally implemented as a 64-bit IEEE 754 number with 53 bits of precision. The float data type in Python is immutable.
What is Python frozenset data type ?
Python frozenset data type holds Unordered set, contains no duplicates, can contain mixed types. The frozenset data type in Python is immutable.
What is Python int data type ?
Python int data type holds Integer of unlimited magnitude. The int data type in Python is immutable.
What is Python list data type ?
Python list data type holds List, can contain mixed types. The list data type in Python is mutable. A list can contain a series of values.
List variables are declared by using brackets [ ]. A list is mutable, which means we can modify the list.
What is Python NoneType data type ?
Python NoneType data type holds An object representing the absence of a value, often called null in other languages. The NoneType data type in Python is immutable.
What is Python NotImplementedType data type ?
Python NotImplementedType data type is a placeholder that can be returned from overloaded operators to indicate unsupported operand types. The NotImplementedType data type in Python is immutable.
What is Python range data type ?
Python range data type holds a Sequence of numbers commonly used for looping specific number of times in for loops.
What is Python set data type ?
Python set data type holds Unordered set, contains no duplicates, can contain mixed types. The set data type in Python is mutable.
A set is an unordered collection of items. Set is defined by values separated by a comma inside braces { }.
What is Python str data type ?
Python str data type holds a character string, sequence of Unicode codepoints. The str data type in Python is immutable.
A string is an ordered sequence of characters. can use single quotes or double quotes to represent strings. Multi-line strings can be represented using triple quotes, ''' or """. Strings are immutable which means once we declare a string we can't update the already declared string.
What is Python tuple data type ?
Python tuple data type can contain mixed types. The tuple data type in Python is immutable. A tuple is a sequence of Python objects separated by commas.
Tuples are immutable, which means tuples once created cannot be modified. Tuples are defined using parentheses (). As Tuples are immutable in Python, if we try to update the tuple, then it will generate an error.