.. 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:`__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:`__lt__`, :meth:`__le__`, :meth:`__gt__`, and :meth:`__ge__` (in
general, :meth:`__lt__` and :meth:`__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: binary; literals
Numbers are created by numeric litera
没有合适的资源?快使用搜索试试~ 我知道了~
python 3.8.3 中文文档
共1086个文件
html:563个
txt:480个
png:16个
需积分: 0 2 下载量 187 浏览量
2023-11-15
10:21:57
上传
评论
收藏 10.27MB ZIP 举报
温馨提示
python 3.8.3 中文文档
资源推荐
资源详情
资源评论
收起资源包目录
python 3.8.3 中文文档 (1086个子文件)
.buildinfo 230B
basic.css 12KB
classic.css 4KB
pygments.css 4KB
pydoctheme.css 3KB
default.css 28B
ajax-loader.gif 673B
changelog.html 1.88MB
genindex-all.html 1.45MB
contents.html 599KB
stdtypes.html 475KB
os.html 474KB
multiprocessing.html 360KB
logging-cookbook.html 346KB
unittest.mock.html 344KB
datetime.html 343KB
2.6.html 339KB
typeobj.html 320KB
3.4.html 319KB
3.2.html 314KB
2.7.html 314KB
3.3.html 305KB
3.5.html 303KB
ssl.html 300KB
3.7.html 281KB
argparse.html 274KB
unittest.html 271KB
3.8.html 270KB
3.6.html 270KB
genindex-P.html 270KB
ctypes.html 269KB
datamodel.html 265KB
2.5.html 246KB
optparse.html 244KB
turtle.html 235KB
decimal.html 229KB
2.3.html 225KB
socket.html 218KB
functions.html 213KB
apiref.html 203KB
programming.html 200KB
importlib.html 195KB
unicode.html 194KB
re.html 191KB
newtypes_tutorial.html 187KB
curses.html 182KB
urllib.request.html 178KB
doctest.html 177KB
unittest.mock-examples.html 172KB
expressions.html 171KB
typing.html 170KB
sys.html 169KB
asyncio-eventloop.html 168KB
sqlite3.html 167KB
init.html 166KB
test.html 162KB
xml.etree.elementtree.html 161KB
collections.html 161KB
2.4.html 160KB
subprocess.html 159KB
mailbox.html 158KB
functional.html 150KB
regex.html 145KB
genindex-S.html 144KB
pickle.html 142KB
enum.html 138KB
inspect.html 138KB
configparser.html 136KB
logging.html 135KB
extending.html 135KB
2.2.html 135KB
codecs.html 133KB
pathlib.html 132KB
itertools.html 132KB
exceptions.html 130KB
clinic.html 129KB
2.0.html 128KB
glossary.html 125KB
io.html 123KB
init_config.html 120KB
tkinter.ttk.html 117KB
3.0.html 117KB
logging.handlers.html 116KB
import.html 116KB
dis.html 115KB
genindex-C.html 108KB
ipaddress.html 108KB
threading.html 106KB
logging.html 106KB
asyncio-protocol.html 106KB
wsgiref.html 101KB
tracemalloc.html 100KB
cmdline.html 100KB
controlflow.html 99KB
genindex-R.html 99KB
xml.dom.html 98KB
contextlib.html 98KB
http.cookiejar.html 96KB
difflib.html 96KB
tarfile.html 96KB
共 1086 条
- 1
- 2
- 3
- 4
- 5
- 6
- 11
资源评论
yangcangong
- 粉丝: 6
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于matlab实现本份代码能对图像进行gabor滤波处理,结合指纹方向图以及指纹沟壑频率特性,对指纹图像进行增强.rar
- 基于matlab实现RBM神经网络实现了手写数字体识别的GUI程序.rar
- 基于matlab实现蝙蝠算法优化相关向量机建模对数据进行建模和预测.rar
- 基于matlab实现编写的禁忌搜索算法,解决了TSP问题,对初学者有重要的参考价值.rar
- 基于matlab实现SOH关于IMU进行姿态求解的C代码,里面包含了两套代码,分别是重力约束法求解和梯度下降法求解.rar
- 1_简单电子邮件客户端.zip
- 基于matlab实现powell优化搜索算法,适合于多参数优化且目标函数中不包含参数的情况.rar
- 基于matlab实现PID神经网络前向和反向学习算法的matlab的原程序代码.rar
- 基于matlab实现nsga-2的多目标优化算法,有注解.rar
- AIR-AP1815-K9-ME-8-5-182-0.zipFor 1815 1830 1840 1850 2700 3700
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功