.. 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:expr:`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:
没有合适的资源?快使用搜索试试~ 我知道了~
超多语言和框架的文档大集合
共2000个文件
html:1181个
txt:572个
md:129个
需积分: 0 0 下载量 150 浏览量
2023-09-02
11:44:57
上传
评论
收藏 56.01MB 7Z 举报
温馨提示
这是超多语言和框架的文档大集合,有python,flask,c++,node.js,javascript,html5,go等
资源推荐
资源详情
资源评论
收起资源包目录
超多语言和框架的文档大集合 (2000个子文件)
styles.css 99KB
frontend.minebcc.css 33KB
main.minebcc.css 23KB
style.css 17KB
style.css 17KB
basic.css 15KB
unit.minebcc.css 11KB
pocoo.css 8KB
pygments.css 5KB
codewalk.css 5KB
csse91f.css 2KB
tabs.css 2KB
hljs.css 2KB
hljs.css 2KB
googlefonts.css 582B
flask.css 471B
final37cf.go 2KB
part337cf.go 1KB
part237cf.go 877B
part137cf.go 731B
all.html 6.97MB
changelog.html 3.63MB
api.html 1.09MB
syscall@go1.19.html 912KB
elf@go1.19.html 636KB
fs.html 625KB
stdtypes.html 621KB
os.html 609KB
crypto.html 533KB
types@go1.19.html 487KB
net@go1.19.html 479KB
buffer.html 471KB
typeobj.html 423KB
multiprocessing.html 410KB
n-api.html 396KB
http@go1.19.html 393KB
datetime.html 378KB
stream.html 377KB
typing.html 375KB
unittest.mock.html 363KB
2.6.html 348KB
ast@go1.19.html 344KB
3.4.html 336KB
ssl.html 329KB
2.7.html 328KB
http2.html 327KB
3.2.html 325KB
spec.html 323KB
3.5.html 321KB
3.3.html 320KB
unittest.html 320KB
image@go1.19.html 318KB
3.11.html 317KB
weekly-2.html 316KB
argparse.html 312KB
3.7.html 305KB
3.10.html 302KB
errors.html 302KB
ast.html 296KB
ctypes.html 296KB
3.6.html 292KB
process.html 290KB
3.8.html 289KB
util.html 283KB
big@go1.19.html 278KB
http.html 276KB
decimal.html 272KB
functions.html 270KB
turtle.html 267KB
socket.html 264KB
mod.html 263KB
optparse.html 256KB
2.5.html 251KB
os@go1.19.html 249KB
sqlite3.html 248KB
sql@go1.19.html 247KB
time@go1.19.html 246KB
reflect@go1.19.html 244KB
apiref.html 236KB
init.html 234KB
go1-22.html 233KB
2.3.html 230KB
bytes@go1.19.html 223KB
re.html 223KB
test.html 222KB
dwarf@go1.19.html 222KB
curses.html 219KB
init_config.html 217KB
importlib.html 215KB
urllib.request.html 211KB
asyncio-eventloop.html 210KB
3.9.html 208KB
testing@go1.19.html 204KB
x509@go1.19.html 204KB
sys.html 203KB
tls@go1.19.html 203KB
subprocess.html 201KB
stable.html 200KB
doctest.html 199KB
windows@go1.19.html 197KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
莫颜JDC
- 粉丝: 1
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功