.. 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
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Python3.8官方开发文档(中文版)压缩包解压后,双击打开index.html文件,就能进入文档首页,然后就可以随意访问开发文档,不用再怕python官方网站访问不了了 Python由荷兰数学和计算机科学研究学会的吉多·范罗苏姆 于1990 年代初设计,作为一门叫做ABC语言的替代品。 Python提供了高效的高级数据结构,还能简单有效地面向对象编程。Python语法和动态类型,以及解释型语言的本质,使它成为多数平台上写脚本和快速开发应用的编程语言, 随着版本的不断更新和语言新功能的添加,逐渐被用于独立的、大型项目的开发。
资源详情
资源评论
资源推荐
收起资源包目录
Python3.8官方开发文档(中文版) (1036个子文件)
.buildinfo 230B
basic.css 12KB
pydoctheme.css 10KB
pygments.css 5KB
classic.css 4KB
default.css 28B
changelog.html 2.06MB
genindex-all.html 1.45MB
contents.html 612KB
stdtypes.html 485KB
os.html 478KB
multiprocessing.html 373KB
logging-cookbook.html 349KB
datetime.html 349KB
unittest.mock.html 349KB
2.6.html 346KB
3.4.html 330KB
typeobj.html 324KB
3.2.html 322KB
2.7.html 321KB
3.3.html 317KB
3.5.html 315KB
ssl.html 301KB
3.7.html 295KB
3.8.html 290KB
3.6.html 284KB
argparse.html 281KB
datamodel.html 276KB
unittest.html 275KB
genindex-P.html 272KB
ctypes.html 270KB
2.5.html 251KB
optparse.html 251KB
turtle.html 241KB
decimal.html 233KB
2.3.html 230KB
socket.html 224KB
functions.html 218KB
apiref.html 216KB
programming.html 204KB
unicode.html 200KB
importlib.html 197KB
re.html 195KB
newtypes_tutorial.html 190KB
curses.html 183KB
urllib.request.html 180KB
expressions.html 179KB
unittest.mock-examples.html 177KB
doctest.html 177KB
typing.html 172KB
asyncio-eventloop.html 172KB
sqlite3.html 171KB
init.html 170KB
sys.html 170KB
collections.html 166KB
test.html 166KB
2.4.html 166KB
subprocess.html 165KB
xml.etree.elementtree.html 165KB
mailbox.html 159KB
functional.html 154KB
enum.html 150KB
regex.html 149KB
pickle.html 147KB
genindex-S.html 146KB
inspect.html 142KB
configparser.html 140KB
2.2.html 139KB
extending.html 139KB
codecs.html 139KB
logging.html 139KB
pathlib.html 138KB
itertools.html 135KB
clinic.html 135KB
exceptions.html 133KB
2.0.html 133KB
glossary.html 130KB
io.html 126KB
init_config.html 124KB
3.0.html 121KB
import.html 120KB
logging.handlers.html 119KB
dis.html 118KB
tkinter.ttk.html 117KB
logging.html 111KB
threading.html 110KB
genindex-C.html 110KB
ipaddress.html 109KB
asyncio-protocol.html 109KB
controlflow.html 104KB
tracemalloc.html 104KB
cmdline.html 103KB
wsgiref.html 103KB
contextlib.html 102KB
genindex-R.html 101KB
string.html 101KB
xml.dom.html 100KB
difflib.html 100KB
tarfile.html 100KB
http.cookiejar.html 99KB
共 1036 条
- 1
- 2
- 3
- 4
- 5
- 6
- 11
ReyX-雷克斯
- 粉丝: 8
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0