.. XXX: reference/datamodel and this have quite a few overlaps!
.. _bltin-types:
**************
Built-in Types
**************
The following sections describe the standard types that are built into the
interpreter.
.. index:: pair: built-in; types
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 :func:`repr` function or the
slightly different :func:`str` function). The latter function is implicitly
used when an object is written by the :func:`print` function.
.. _truth:
Truth Value Testing
===================
.. index::
statement: if
statement: while
pair: truth; value
pair: Boolean; operations
single: false
Any object can be tested for truth value, for use in an :keyword:`if` or
:keyword:`while` condition or as operand of the Boolean operations below.
.. index:: single: true
By default, an object is considered true unless its class defines either a
:meth:`__bool__` method that returns ``False`` or a :meth:`__len__` method that
returns zero, when called with the object. [1]_ Here are most of the built-in
objects considered false:
.. index::
single: None (Built-in object)
single: False (Built-in object)
* constants defined to be false: ``None`` and ``False``.
* zero of any numeric type: ``0``, ``0.0``, ``0j``, ``Decimal(0)``,
``Fraction(0, 1)``
* empty sequences and collections: ``''``, ``()``, ``[]``, ``{}``, ``set()``,
``range(0)``
.. index::
operator: or
operator: and
single: False
single: True
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:
Boolean Operations --- :keyword:`!and`, :keyword:`!or`, :keyword:`!not`
=======================================================================
.. index:: pair: Boolean; operations
These are the Boolean operations, ordered by ascending priority:
+-------------+---------------------------------+-------+
| Operation | Result | Notes |
+=============+=================================+=======+
| ``x or y`` | if *x* is false, then *y*, else | \(1) |
| | *x* | |
+-------------+---------------------------------+-------+
| ``x and y`` | if *x* is false, then *x*, else | \(2) |
| | *y* | |
+-------------+---------------------------------+-------+
| ``not x`` | if *x* is false, then ``True``, | \(3) |
| | else ``False`` | |
+-------------+---------------------------------+-------+
.. index::
operator: and
operator: or
operator: not
Notes:
(1)
This is a short-circuit operator, so it only evaluates the second
argument if the first one is false.
(2)
This is a short-circuit operator, so it only evaluates the second
argument if the first one is true.
(3)
``not`` has a lower priority than non-Boolean operators, so ``not a == b`` is
interpreted as ``not (a == b)``, and ``a == not b`` is a syntax error.
.. _stdcomparisons:
Comparisons
===========
.. index::
pair: chaining; comparisons
pair: operator; comparison
operator: ==
operator: < (less)
operator: <=
operator: > (greater)
operator: >=
operator: !=
operator: is
operator: is not
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 |
+------------+-------------------------+
| ``is`` | object identity |
+------------+-------------------------+
| ``is not`` | negated object identity |
+------------+-------------------------+
.. index::
pair: object; numeric
pair: objects; comparing
Objects of different types, except different numeric types, never compare equal.
The ``==`` operator is always defined but for some object types (for example,
class objects) is equivalent to :keyword:`is`. The ``<``, ``<=``, ``>`` and ``>=``
operators are only defined where they make sense; for example, they raise a
:exc:`TypeError` exception when one of the arguments is a complex number.
.. index::
single: __eq__() (instance method)
single: __ne__() (instance method)
single: __lt__() (instance method)
single: __le__() (instance method)
single: __gt__() (instance method)
single: __ge__() (instance method)
Non-identical instances of a class normally compare as non-equal unless the
class defines the :meth:`~object.__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 :meth:`~object.__lt__`, :meth:`~object.__le__`, :meth:`~object.__gt__`, and
:meth:`~object.__ge__` (in general, :meth:`~object.__lt__` and
:meth:`~object.__eq__` are sufficient, if you want the conventional meanings of the
comparison operators).
The behavior of the :keyword:`is` and :keyword:`is not` operators cannot be
customized; also they can be applied to any two objects and never raise an
exception.
.. index::
operator: in
operator: not in
Two more operations with the same syntactic priority, :keyword:`in` and
:keyword:`not in`, are supported by types that are :term:`iterable` or
implement the :meth:`__contains__` method.
.. _typesnumeric:
Numeric Types --- :class:`int`, :class:`float`, :class:`complex`
================================================================
.. index::
object: numeric
object: Boolean
object: integer
object: floating point
object: complex number
pair: C; language
There are three distinct numeric types: :dfn:`integers`, :dfn:`floating
point numbers`, and :dfn:`complex numbers`. In addition, Booleans are a
subtype of integers. Integers have unlimited precision. Floating point
numbers are usually implemented using :c:type:`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 :data:`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 :mod:`fractions.Fraction`, for
rationals, and :mod:`decimal.Decimal`, for floating-point numbers with
user-definable precision.)
.. index::
pair: numeric; literals
pair: integer; literals
pair: floating point; literals
pair: complex number; literals
pair: hexadecimal; literals
pair: octal; literals
pair:
没有合适的资源?快使用搜索试试~ 我知道了~
python3.10.4官方文档
共1054个文件
html:530个
txt:488个
js:14个
需积分: 1 3 下载量 95 浏览量
2022-04-18
08:49:47
上传
评论
收藏 11.46MB ZIP 举报
温馨提示
python3.10.4官方文档html版
资源详情
资源评论
资源推荐
收起资源包目录
python3.10.4官方文档
(1054个子文件)
.buildinfo 230B
basic.css 13KB
pydoctheme.css 10KB
pygments.css 5KB
classic.css 4KB
default.css 28B
changelog.html 2.85MB
genindex-all.html 1.58MB
contents.html 662KB
stdtypes.html 555KB
os.html 533KB
multiprocessing.html 379KB
logging-cookbook.html 358KB
typeobj.html 358KB
datetime.html 357KB
unittest.mock.html 353KB
2.6.html 344KB
3.4.html 330KB
3.2.html 322KB
2.7.html 320KB
3.3.html 317KB
3.5.html 314KB
datamodel.html 308KB
ssl.html 305KB
genindex-P.html 298KB
typing.html 294KB
3.7.html 293KB
3.10.html 291KB
argparse.html 289KB
unittest.html 287KB
3.6.html 284KB
3.8.html 281KB
ast.html 271KB
ctypes.html 270KB
optparse.html 252KB
2.5.html 249KB
turtle.html 248KB
decimal.html 247KB
socket.html 236KB
functions.html 236KB
2.3.html 230KB
programming.html 227KB
apiref.html 219KB
importlib.html 217KB
unicode.html 212KB
3.9.html 201KB
re.html 199KB
test.html 195KB
stable.html 194KB
init.html 194KB
curses.html 193KB
expressions.html 192KB
newtypes_tutorial.html 190KB
urllib.request.html 189KB
sqlite3.html 184KB
asyncio-eventloop.html 183KB
doctest.html 183KB
collections.html 180KB
subprocess.html 179KB
sys.html 179KB
unittest.mock-examples.html 178KB
xml.etree.elementtree.html 173KB
init_config.html 172KB
2.4.html 166KB
mailbox.html 163KB
compound_stmts.html 159KB
pathlib.html 156KB
inspect.html 155KB
descriptor.html 155KB
functional.html 155KB
genindex-S.html 150KB
regex.html 150KB
itertools.html 148KB
enum.html 148KB
logging.html 148KB
pickle.html 147KB
configparser.html 147KB
exceptions.html 146KB
codecs.html 143KB
io.html 140KB
clinic.html 140KB
extending.html 137KB
2.2.html 137KB
glossary.html 135KB
2.0.html 133KB
logging.handlers.html 128KB
tkinter.ttk.html 123KB
import.html 123KB
controlflow.html 122KB
genindex-C.html 121KB
3.0.html 121KB
dis.html 120KB
ipaddress.html 119KB
threading.html 117KB
tracemalloc.html 117KB
contextlib.html 114KB
logging.html 113KB
statistics.html 113KB
asyncio-protocol.html 110KB
difflib.html 105KB
共 1054 条
- 1
- 2
- 3
- 4
- 5
- 6
- 11
98岁带病上岗
- 粉丝: 64
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0