.. 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.13-docs-html.rar
需积分: 0 159 浏览量
更新于2023-02-16
收藏 9.53MB RAR 举报
Python 3.8.13 是 Python 语言的一个重要版本,其官方文档是学习和掌握这一版本的关键资源。"python-3.8.13-docs-html" 是这个版本的中文版开发文档,以HTML格式提供,对于中文使用者来说尤其便利,避免了语言障碍,使学习过程更加顺畅。
Python 是一种高级、通用的编程语言,以其简洁明了的语法和强大的功能而受到广大开发者喜爱。Python 3.8.13 文档包括了以下主要知识点:
1. **基础语法**:涵盖变量、数据类型(如整型、浮点型、字符串、列表、元组、字典、集合)、流程控制(条件语句、循环语句)、函数定义与调用、模块导入等。
2. **面向对象编程**:讲解类的定义、对象的创建、继承、多态、封装等概念,以及如何使用类和对象进行程序设计。
3. **异常处理**:介绍try-except-finally语句块,用于捕获并处理程序运行中的错误,提高程序的健壮性。
4. **标准库**:Python的标准库非常丰富,包括文件操作、网络通信、数据解析、时间日期处理、数学运算等模块,文档详细列出了每个模块的功能和用法。
5. **内置函数**:如len()、type()、print()、input()等,这些函数在日常编程中非常常用,了解它们的使用方式能提升编程效率。
6. **上下文管理器**:介绍with语句,用于资源的自动获取和释放,是Python中优雅编程的重要组成部分。
7. **函数式编程**:包括高阶函数、lambda表达式、map()、filter()、reduce()等,以及装饰器的使用,展示了Python的函数式编程特性。
8. **生成器**:Python的生成器可以实现延迟计算,节省内存,用于处理大数据流或无限序列。
9. **元编程**:如动态类型、反射机制、类装饰器等,让Python代码可以自我修改和扩展。
10. **异步编程**:Python 3.5引入了asyncio库,提供了异步I/O支持,允许并发执行多个任务,提高程序性能。
11. **新特性**:Python 3.8.13 版本可能引入了一些新的特性和改进,如赋值表达式(walrus operator :=)等,文档会详细介绍这些变化。
通过阅读和研究"python-3.8.13-docs-html",学习者不仅能了解Python的基础知识,还能深入理解高级特性和最佳实践,从而成为一名熟练的Python开发者。这个资源特别适合初学者和有经验的开发者,无论你是要入门Python,还是想要进一步提升技能,它都是不可或缺的学习资料。

一纸红尘轻似梦
- 粉丝: 6036
最新资源
- 微信小程序 支付demo python+flask.zip
- 公司激励管理制度.doc
- 192电磁振荡的周期和频率.ppt
- 垂直绿化施工组织方案.doc
- 造价员转正个人工作总结.doc
- 建设工程施工招标手续办理-secret.docx
- 土建、装饰、安装计算套项清单组价范例.doc
- 微信小程序 商城demo.zip
- 公路工程安全管理全套表格94页.doc
- 房地产可行性研究全攻略.doc
- 工程管理部监理资料管理办法.doc
- 【QC7大手法-精选讲议】=QC七大手法培训资料之讲议.ppt
- 白皮书:BIM给幕墙设计带来的技术变革.pdf
- 微信小程序开发过程中积累的一些代码.zip
- 万科施工招标合同-技术规范.doc
- MP-CU, colorui3.x 微信小程序原生版.zip