Python3基础-语言内置
节选自Python文档的Library Reference部分,位置:
Python » 3.4.3 Documentation » The Python Standard Library »
Built-in Functions 内置函数
部分例如:
函数 | 例子 |
---|---|
bin() | bin(5) => ‘0b101’ |
chr() | chr(27721) => ‘汉’ |
dict() | |
eval() | x=3, eval(‘x+1’) => 4 |
hex() | |
id() | |
int() | |
len() | |
list() | |
ord() | ord(‘汉’) => 27721 |
range() | |
repr() | repr(100) => ‘100’ |
str() | str(100) => ‘100’ |
tuple() | |
type() | type((6,)) => <class ‘tuple’> |
Built-in Constants 内置常量
- True
- False
- None
- NotImplemented
- 等
Built-in Types 内置类型
Truth Value Testing真值测试
下面的值被当作false看待:
None
False
- zero of any numeric type, for example,
0
,0.0
,0j
. - any empty sequence, for example,
''
,()
,[]
. - any empty mapping, for example,
{}
. - 等
Boolean Operations布尔操作
- or
- and
- not
Comparisons比较
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 |
Numeric Types — int, float, complex 三种数值类型
三种数值类型: integers整数, floating point numbers浮点数, and complex numbers复数. In addition, Booleans are a subtype of integers.
The constructors int()
, float()
, and complex()
can be used to produce numbers of a specific type.
Bitwise Operations on Integer Types
作用于整数上的位运算:
Operation | Result |
---|---|
x l y | bitwise or of x and y |
x ^ y | bitwise exclusive or of x and y |
x & y | bitwise and of x and y |
x << n | x shifted left by n bits |
x >> n | x shifted right by n bits |
~x | the bits of x inverted |
Iterator Types 迭代器类型
略
Sequence Types — list, tuple, range 三种序列类型
class list([iterable])
Lists may be constructed in several ways:
- Using a pair of square brackets to denote the empty list:
[]
- Using square brackets, separating items with commas:
[a]
,[a, b, c]
- Using a list comprehension:
[x for x in iterable]
- Using the type constructor:
list()
orlist(iterable)
class tuple([iterable])
Tuples may be constructed in a number of ways:
- Using a pair of parentheses to denote the empty tuple:
()
- Using a trailing comma for a singleton tuple:
a,
or(a,)
- Separating items with commas:
a, b, c
or(a, b, c)
- Using the tuple() built-in:
tuple()
ortuple(iterable)
class range(start, stop[, step])
例:
>>> list(range(0, 10, 3))
[0, 3, 6, 9]
Text Sequence Type — str 文本序列类型
- Single quotes: ‘allows embedded “double” quotes’
- Double quotes: “allows embedded ‘single’ quotes”.
- Triple quoted: ”’Three single quotes”’, “”“Three double quotes”“”
Binary Sequence Types — bytes, bytearray, memoryview 三种二进制序列类型
略
Set Types — set, frozenset 两种集合类型
略
Mapping Types — dict 映射类型
略
Built-in Exceptions 内置异常
略