Built-in Types
The principal built-in types are numerics, sequences, mappings, classes, instances and exceptions [doce]. This section covers only the most base of them. Others will be discussed in the future within their own topics.
Numeric types
There are 3 distinct numeric types: integers, floating point numbers, and
complex numbers. In addition, Booleans are a subtype of integers. Integers
have unlimited precision. Floating point numbers are usually implemented using
double
in C. Complex numbers have a real and imaginary part, which are each
a floating point number.
Numbers are created by numeric literals or as the result of built-in functions
and operators. Unadorned integer literals (including hex, octal and binary
numbers) yield integers. Numeric literals containing a decimal point or an
exponent sign yield floating point numbers. Appending j
or J
to a
numeric literal yields an imaginary number (a complex number with a zero real
part) which you can add to an integer or float to get a complex number with
real and imaginary parts.
Supported operations
Operation |
Result |
---|---|
|
sum of x and y |
|
difference of x and y |
|
product of x and y |
|
quotient of x and y |
|
floored quotient of x and y |
|
remainder of |
|
x negated |
|
x unchanged |
|
absolute value or magnitude of x |
|
x converted to integer |
|
x converted to floating point |
|
a complex number with real part re, imaginary part im. im defaults to zero. |
|
the pair |
|
x to the power y |
Note
x // y
also referred to as integer division. The resultant value
is a whole integer, through the result’s type is not necessarily int.
The result is always rounded towards minus infinity:
1 // 2
is 0
Note
float(x)
also accepts the strings "nan"
and "inf"
with
an optional prefix “+” or “-” for NaN and
positive and negative infinity.
Code examples
1>>> x = 42
2>>> y = 24
3>>> a = int(42)
4>>> b = int(42.24)
5>>> # result is of the integer type
6>>> result_add = x + y # for both int operands only
7>>> result_sub = x - y # for both int operands only
8>>> result_mul = x * y # for both int operands only
9>>> result_div = x // y # for any numeric operands
1>>> x = 42.
2>>> y = .24
3>>> z = 42.24
4>>> a = float(42)
5>>> b = float(42.24)
6>>> # result is of the float type
7>>> result_add = x + y # for any float operand
8>>> result_sub = x - y # for any float operand
9>>> result_mul = x * y # for any float operand
10>>> result_div = x / y # for any numeric operands
Value comparisons
Numbers of built-in numeric types can be compared within and across their types (with the restriction that complex numbers do not support order comparison). Within the limits of the types involved, they compare mathematically correct without loss of precision.
The “not-a-number” values float("NaN")
and decimal.Decimal('NaN')
are
special. Any ordered comparison of a number to a not-a-number value is false.
Sequence types
There are 3 basic sequence types: lists, tuples, and range objects. Additional sequence types tailored for processing of binary data and text strings are described in dedicated sections.
Lists
Lists are mutable sequences, typically used to store collections of items. They may be constructed in several ways:
using a pair of square brackets to denote the empty list:
[]
using square brackets, separating items with commas:
[a]
,[a, b, c]
using a list comprehension:
[x for x in iterable]
using the type constructor:
list()
orlist(iterable)
Tuples
Tuples are immutable sequences, typically used to store collections of data or
for cases where an immutable sequence of data is needed (such as allowing
storage in a set
or dict
instances).
They may be constructed in a number of ways:
using a pair of parentheses to denote the empty tuple:
()
using a trailing comma for a singleton tuple:
a,
or(a, b, c)
separating items with commas:
a, b, c
or(a, b, c)
using the type constructor:
tuple()
ortuple(iterable)
Common operations
Operation |
Result |
---|---|
|
|
|
the concatenation of s and t |
|
equivalent to adding s to itself n times |
|
i-th item of s, origin 0 |
|
slice of s from i to j with step k. any of these (or even all 3) may be omitted. |
|
length of s |
|
smallest item of s |
|
largest item of s |
|
total number of occurrences of x in s |
|
index of the first occurrence of x in s at or after index i and before index j |
Value comparisons
Sequences can be compared only within each of their types, with the restriction
that ranges do not support order comparison. Equality comparison across these
types results in inequality, and ordering comparison across these types raises
TypeError
.
Sequences compare lexicographically using comparison of corresponding elements. The built-in containers typically assume identical objects are equal to themselves. Lexicographical comparison between built-in collections works as follows:
For two collections to compare equal, they must be of the same type, have the same length, and each pair of corresponding elements must compare equal.
Collections that supports order comparison are ordered the same as their first unequal elements. If corresponding element does not exist, the shorter collection is ordered first.
Mutable sequence types
Operation |
Result |
---|---|
|
item i of s is replaced by x |
|
slice of s from i to j replaced by the contents of t |
|
same as |
|
the elements of s are replaced by those of t [1] |
|
removes the elements of |
|
appends x to the end of the sequence; the same as
|
|
removes all items from s ( |
|
creates a shallow copy of s ( |
|
extends s with the contents of t |
|
inserts x into s at the index i; same as
|
|
retrieves the item at i and also removes it from s (if i is omitted - use the last one available index) |
|
remove the first item that is equal to x from s |
|
reverses the items of s in place |
Text sequence type
String methods
Strings implement all of the common sequence operations, along with the additional methods.
Value comparisons
Strings compare lexicographically using the numerical Unicode code points of their characters.
Set types
Value comparisons
Sets (instances of set
or frozenset
) can be compared within and across
their types. They define order comparison operators to mean subset and superset
tests. Those relations do not define total ordering (e.g. two sets {1, 2}
and {2, 3}
are not equal, nor subsets of one another, nor supersets of one
another).
Mapping type
A mapping object maps hashable values to arbitrary objects. Mappings are mutable objects. There is currently ony one standard mapping type, the dictionary. In few words mappings are collections of keys and their values.
A dictionary’s keys are almost arbitrary values. Values that are not hashable
may not be used as keys. Values that compare equal (such as 1
, 1.0
and
True
) can be used interchangeably to index the same dictionary entry.
Dictionaries can be created by several means:
Use braces to denote the empty dictionary:
{}
Use a comma-separated list of
key: value
pairs with braces:{'first_name': 'Serhii', 'last_name': 'Horodilov', 'age': 34}
Use a dict comprehension:
{x: x ** 2 for x in range(10)}
Use the type constructor:
dict()
,dict([('key', 100)])
,dict(key=100)
Value comparisons
Instances of dict
compare equal if and only if they have equal key-value
pairs.
Order comparisons raise TypeError
.