đź’Ą TRENDING: /library/stdtypes.html - Complete Album!
Built-in Types¶
The following sections describe the standard types that are built into the interpreter.
The principal built-in types are numerics, sequences, mappings, classes, instances and exceptions.
Some collection classes are mutable. The methods that add, subtract, or
rearrange their members in place, and don’t return a specific item, never return
the collection instance itself but None.
Some operations are supported by several object types; in particular,
practically all objects can be compared for equality, tested for truth
value, and converted to a string (with the repr() function or the
slightly different str() function). The latter function is implicitly
used when an object is written by the print() function.
Truth Value Testing¶
Any object can be tested for truth value, for use in an if or
while condition or as operand of the Boolean operations below.
By default, an object is considered true unless its class defines either a
__bool__() method that returns False or a
__len__() method that
returns zero, when called with the object. [1] If one of the methods raises an
exception when called, the exception is propagated and the object does
not have a truth value (for example, NotImplemented).
Here are most of the built-in objects considered false:
constants defined to be false:
NoneandFalsezero of any numeric type:
0,0.0,0j,Decimal(0),Fraction(0, 1)empty sequences and collections:
'',(),[],{},set(),range(0)
Operations and built-in functions that have a Boolean result always return 0
or False for false and 1 or True for true, unless otherwise stated.
(Important exception: the Boolean operations or and and always return
one of their operands.)
Boolean Operations — and, or, not¶
These are the Boolean operations, ordered by ascending priority:
Operation |
Result |
Notes |
|---|---|---|
|
if x is true, then x, else y |
(1) |
|
if x is false, then x, else y |
(2) |
|
if x is false, then |
(3) |
Notes:
This is a short-circuit operator, so it only evaluates the second argument if the first one is false.
This is a short-circuit operator, so it only evaluates the second argument if the first one is true.
nothas a lower priority than non-Boolean operators, sonot a == bis interpreted asnot (a == b), anda == not bis a syntax error.
Comparisons¶
There are eight comparison operations in Python. They all have the same
priority (which is higher than that of the Boolean operations). Comparisons can
be chained arbitrarily; for example, x < y <= z is equivalent to x < y and
y <= z, except that y is evaluated only once (but in both cases z is not
evaluated at all when x < y is found to be false).
This table summarizes the comparison operations:
Operation |
Meaning |
|---|---|
|
strictly less than |
|
less than or equal |
|
strictly greater than |
|
greater than or equal |
|
equal |
|
not equal |
|
object identity |
|
negated object identity |
Unless stated otherwise, objects of different types never compare equal.
The == operator is always defined but for some object types (for example,
class objects) is equivalent to is. The <, <=, > and >=
operators are only defined where they make sense; for example, they raise a
TypeError exception when one of the arguments is a complex number.
Non-identical instances of a class normally compare as non-equal unless the
class defines the __eq__() method.
Instances of a class cannot be ordered with respect to other instances of the
same class, or other types of object, unless the class defines enough of the
methods __lt__(), __le__(), __gt__(), and
__ge__() (in general, __lt__() and
__eq__() are sufficient, if you want the conventional meanings of the
comparison operators).
The behavior of the is and is not operators cannot be
customized; also they can be applied to any two objects and never raise an
exception.
Two more operations with the same syntactic priority, in and
not in, are supported by types that are iterable or
implement the __contains__() method.
Numeric Types — 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. Floating-point
numbers are usually implemented using double in C; information
about the precision and internal representation of floating-point
numbers for the machine on which your program is running is available
in sys.float_info. Complex numbers have a real and imaginary
part, which are each a floating-point number. To extract these parts
from a complex number z, use z.real and z.imag. (The standard
library includes the additional numeric types fractions.Fraction, for
rationals, and decimal.Decimal, for floating-point numbers with
user-definable precision.)
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.
The constructors int(), float(), and
complex() can be used to produce numbers of a specific type.
Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different numeric types, the operand with the “narrower” type is widened to that of the other, where integer is narrower than floating point. Arithmetic with complex and real operands is defined by the usual mathematical formula, for example:
x + complex(u, v) = complex(x + u, v)
x * complex(u, v) = complex(x * u, x * v)
A comparison between numbers of different types behaves as though the exact values of those numbers were being compared. [2]
All numeric types (except complex) support the following operations (for priorities of the operations, see Operator precedence):
Operation |
Result |
Notes |
Full documentation |
|---|---|---|---|
|
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 |
(1)(2) |
|
|
remainder of |
(2) |
|
|
x negated |
||
|
x unchanged |
||
|
absolute value or magnitude of x |
||
|
x converted to integer |
(3)(6) |
|
|
x converted to floating point |
(4)(6) |
|
|
a complex number with real part re, imaginary part im. im defaults to zero. |
(6) |
|
|
conjugate of the complex number c |
||
|
the pair |
(2) |
|
|
x to the power y |
(5) |
|
|
x to the power y |
(5) |
Notes:
Also referred to as integer division. For operands of type
int, the result has typeint. For operands of typefloat, the result has typefloat. In general, the result is a whole integer, though the result’s type is not necessarilyint. The result is always rounded towards minus infinity:1//2is0,(-1)//2is-1,1//(-2)is-1, and(-1)//(-2)is0.Not for complex numbers. Instead convert to floats using
abs()if appropriate.Conversion from
floattointtruncates, discarding the fractional part. See functionsmath.floor()andmath.ceil()for alternative conversions.float also accepts the strings “nan” and “inf” with an optional prefix “+” or “-” for Not a Number (NaN) and positive or negative infinity.
Python defines
pow(0, 0)and0 ** 0to be1, as is common for programming languages.The numeric literals accepted include the digits
0to9or any Unicode equivalent (code points with theNdproperty).See the Unicode Standard for a complete list of code points with the
Nd