.. 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:
没有合适的资源?快使用搜索试试~ 我知道了~
python-3.9.14-docs-html (1).zip
共1053个文件
html:530个
txt:488个
js:14个
需积分: 9 0 下载量 70 浏览量
2022-11-11
15:03:30
上传
评论
收藏 11.19MB ZIP 举报
温馨提示
python-3.9.14-docs-html (1).zip
资源推荐
资源详情
资源评论
收起资源包目录
python-3.9.14-docs-html (1).zip (1053个子文件)
.buildinfo 230B
basic.css 12KB
pydoctheme.css 10KB
pygments.css 5KB
classic.css 4KB
default.css 28B
changelog.html 2.74MB
genindex-all.html 1.52MB
contents.html 638KB
stdtypes.html 553KB
os.html 493KB
multiprocessing.html 376KB
logging-cookbook.html 358KB
datetime.html 352KB
2.6.html 349KB
unittest.mock.html 348KB
typeobj.html 345KB
3.4.html 337KB
2.7.html 328KB
3.2.html 326KB
3.5.html 321KB
3.3.html 321KB
ssl.html 302KB
3.7.html 302KB
datamodel.html 300KB
3.6.html 290KB
3.8.html 290KB
genindex-P.html 288KB
argparse.html 285KB
unittest.html 275KB
ctypes.html 269KB
2.5.html 252KB
typing.html 252KB
optparse.html 251KB
newtypes_tutorial.html 249KB
turtle.html 241KB
decimal.html 240KB
socket.html 231KB
ast.html 230KB
2.3.html 230KB
functions.html 226KB
programming.html 222KB
apiref.html 218KB
3.9.html 212KB
unicode.html 210KB
importlib.html 206KB
re.html 196KB
expressions.html 188KB
curses.html 186KB
urllib.request.html 184KB
sqlite3.html 180KB
init.html 180KB
test.html 180KB
unittest.mock-examples.html 176KB
doctest.html 176KB
asyncio-eventloop.html 175KB
sys.html 175KB
subprocess.html 170KB
collections.html 168KB
2.4.html 166KB
xml.etree.elementtree.html 164KB
mailbox.html 160KB
extending.html 159KB
descriptor.html 156KB
functional.html 154KB
genindex-S.html 151KB
enum.html 151KB
regex.html 149KB
pathlib.html 148KB
pickle.html 147KB
configparser.html 144KB
codecs.html 142KB
logging.html 141KB
glossary.html 140KB
2.2.html 139KB
inspect.html 138KB
itertools.html 138KB
exceptions.html 137KB
init_config.html 136KB
2.0.html 133KB
clinic.html 129KB
io.html 128KB
logging.handlers.html 123KB
3.0.html 121KB
import.html 119KB
tkinter.ttk.html 118KB
ipaddress.html 117KB
genindex-C.html 114KB
logging.html 113KB
tracemalloc.html 113KB
threading.html 111KB
dis.html 111KB
asyncio-protocol.html 109KB
cmdline.html 105KB
controlflow.html 105KB
genindex-R.html 104KB
contextlib.html 103KB
wsgiref.html 103KB
string.html 101KB
tarfile.html 101KB
共 1053 条
- 1
- 2
- 3
- 4
- 5
- 6
- 11
资源评论
qq_74117255
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Screenshot_20240528_103010.jpg
- 基于Python的新能源承载力计算及界面设计源码 - HAINING-DG
- 基于Java的本科探索学习项目设计源码 - 本科探索
- 基于Javascript和Python的微商城项目设计源码 - MicroMall
- 基于Java的网上订餐系统设计源码 - online ordering system
- 基于Javascript的超级美眉网络资源管理应用模块设计源码
- 基于Typescript和PHP的编程知识储备库设计源码 - study-php
- Screenshot_2024-05-28-11-40-58-177_com.tencent.mm.jpg
- 基于Dart的Flutter小提琴调音器APP设计源码 - violinhelper
- 基于JavaScript和CSS的随寻订购网页设计源码 - web-order
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功