笔记-pyton内置数据类型
笔记-pyton内置数据类型
1. 简介
The principal built-in types are numerics, sequences, mappings, classes, instances and exceptions.
2. 操作
2.1. 真值测试
所有对象都可以做真值测试,可以在if 或while的条件语句中,也可以用布尔操作。
对象做真值测试优先调用__bool__返回真则测试为真,否则为假;
没有定义bool的话调用__len__ 返回为真则测试为真。
大部分内置对象真值测试方法如下:
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)
2.2. boolena operations- and,or,not
要注意的是or 和and是short-circuit operator,会返回第一个符合条件的条件式值;
not的运算级别比非布尔运算低,not a == b 等效于not ( a == b ),而a == not b语法错误。
2.3. 比较运算
除非定义__eq__()方法,否则类的非相同实例的比较结果是不等。
总之,一般不要比较不同的类。
2.4. int,float,complex
|
Operation |
Result |
Notes |
Full documentation |
|
x + y |
sum of x and y |
||
|
x - y |
difference of x and y |
||
|
x * y |
product of x and y |
||
|
x / y |
quotient of x and y |
||
|
x // y |
floored quotient of x and y |
(1) |
|
|
x % y |
remainder of x / y |
(2) |
|
|
-x |
x negated |
||
|
+x |
x unchanged |
||
|
abs(x) |
absolute value or magnitude of x |
||
|
int(x) |
x converted to integer |
(3)(6) |
|
|
float(x) |
x converted to floating point |
(4)(6) |
|
|
complex(re, im) |
a complex number with real part re, imaginary part im. im defaults to zero. |
(6) |
|
|
c.conjugate() |
conjugate of the complex number c |
||
|
divmod(x, y) |
the pair (x // y, x % y) |
(2) |
|
|
pow(x, y) |
x to the power y |
(5) |
|
|
x ** y |
x to the power y |
(5) |
2.5. integer类型的比特操作
只有整数可以进行比特运算。
|
Operation |
Result |
Notes |
|
x | y |
bitwise or of x and y |
(4) |
|
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 |
取反 |
2.6. 整数类型的附加方法
2.7. 序列
通用操作
|
Operation |
Result |
Notes |
|
x in s |
True if an item of s is equal to x, else False |
(1) |
|
x not in s |
False if an item of s is equal to x, else True |
(1) |
|
s + t |
the concatenation of s and t |
(6)(7) |
|
s * n or n * s |
equivalent to adding s to itself n times |
(2)(7) |
|
s[i] |
ith item of s, origin 0 |
(3) |
|
s[i:j] |
slice of s from i to j |
(3)(4) |
|
s[i:j:k] |
slice of s from i to j with step k |
(3)(5) |
|
len(s) |
length of s |
|
|
min(s) |
smallest item of s |
|
|
max(s) |
largest item of s |
|
|
s.index(x[, i[, j]]) |
index of the first occurrence of x in s (at or after index i and before index j) |
(8) |
|
s.count(x) |
total number of occurrences of x in s |
mutable和immutable
|
Operation |
Result |
Notes |
|
s[i] = x |
item i of s is replaced by x |
|
|
s[i:j] = t |
slice of s from i to j is replaced by the contents of the iterable t |
|
|
del s[i:j] |
same as s[i:j] = [] |
|
|
s[i:j:k] = t |
the elements of s[i:j:k] are replaced by those of t |
(1) |
|
del s[i:j:k] |
removes the elements of s[i:j:k] from the list |
|
|
s.append(x) |
appends x to the end of the sequence (same as s[len(s):len(s)] = [x]) |
|
|
s.clear() |
removes all items from s (same as dels[:]) |
(5) |
|
s.copy() |
creates a shallow copy of s (same as s[:]) |
(5) |
|
s.extend(t) or s += t |
extends s with the contents of t (for the most part the same as s[len(s):len(s)]= t) |
|
|
s *= n |
updates s with its contents repeated ntimes |
(6) |
|
s.insert(i, x) |
inserts x into s at the index given by i(same as s[i:i] = [x]) |
|
|
s.pop([i]) |
retrieves the item at i and also removes it from s |
(2) |
|
s.remove(x) |
remove the first item from s where s[i]is equal to x |
(3) |
|
s.reverse() |
reverses the items of s in place |
(4) |
3. binary sequence types-bytes,bytearray,memoryview
常使用的是bytes和bytearray,memoryview主要是为了实现在不复制的情况下访问二进制对象(使用buffer protcol)。
bytes是不可变的,bytearray是可变的。
bytearray支持常规序列操作。
有两个方法fromhex(),hex()知道即可
The methods on bytes and bytearray objects don’t accept strings as their arguments, just as the methods on strings don’t accept bytes as their arguments. For example, you have to write:
a = "abc"
b = a.replace("a", "f")
and:
a = b"abc"
b = a.replace(b"a", b"f")
其它操作都与list比较相似,不啰嗦。
3.1. memoryviews
memoryview对象允许python代码访问对象的内部数据,当然,这个对象必需支持buffer procotol。
class memoryvies(obj)
创建一个memoryview对象,它引用obj。obj必需支持buffer protocol,就目前而言,内置的类型可以使用memoryview的有bytes和bytearray。
A memoryview supports slicing and indexing to expose its data. One-dimensional slicing will result in a subview:
>>>
>>> v = memoryview(b'abcefg')
>>> v[1]
98
>>> v[-1]
103
>>> v[1:4]
<memory at 0x7f3ddc9f4350>
>>> bytes(v[1:4])
b'bce'
简单点就是,str和bytearray的切片操作会产生新的切片str和bytearry并拷贝数据,使用memoryview之后不会。
不使用memoryview
>> a = 'aaaaaa'
>> b = a[:2] # 会产生新的字符串
>> a = bytearray('aaaaaa')
>> b = a[:2] # 会产生新的bytearray
>> b[:2] = 'bb' # 对b的改动不影响a
>> a
bytearray(b'aaaaaa')
>> b
bytearray(b'bb')
使用memoryview
>> a = 'aaaaaa'
>> ma = memoryview(a)
>> ma.readonly # 只读的memoryview
True
>> mb = ma[:2] # 不会产生新的字符串
>> a = bytearray('aaaaaa')
>> ma = memoryview(a)
>> ma.readonly # 可写的memoryview
False
>> mb = ma[:2] # 不会会产生新的bytearray
>> mb[:2] = 'bb' # 对mb的改动就是对ma的改动
>> mb.tobytes()
'bb'
>> ma.tobytes()
'bbaaaa'
4. set types – set, frozenset
A set object is an unordered collection of distinct hashable objects.
有两种内置集合类型,set and frozenset。
frozenset是不可变的set;
通用操作,与数学上一样的,交并差
|
len(s) |
|
|
x in s |
|
|
x not in s |
|
|
isdisjoint(other) |
是否有交集 |
|
issubset(other) set <=other |
discard(elem) 如果存在,则删除
5. dict
常规的没什么好讲的,赋值,取值,赋值(默认值),fromkeys(),get()pop(),popitem(),update(),values(),keys(),items()
d = dict.fromkeys(range(15),8)
5.1. dict view obj
The objects returned by dict.keys(), dict.values() and dict.items() are view objects. They provide a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the view reflects these changes.
基本上当做一个可迭代对象使用就可以了。
6. context manager types
python 的with 语句支持一个概念:上下文管理
有两个方法:
contextmanager.__enter__()
contextmanager.__exit__(exc_type, exc_val, exc_tb)
Exit the runtime context and return a Boolean flag indicating if any exception that occurred should be suppressed. If an exception occurred while executing the body of the with statement, the arguments contain the exception type, value and traceback information. Otherwise, all three arguments are None.
在with语句中的代码产生的异常首先会传递给exit方法,如果它返回True,则异常不会继续传播。这也是with的目的,无论异常与否,都会执行__exit__()。
一个简单案例:
class TraceBlock(object):
def message(self, arg):
print('running '
+ arg)
def __enter__(self):
print('starting
with block')
return self
def __exit__(self,
exc_type, exc_value, exc_tb):
if exc_type is None:
print('exited
normally\n')
else:
print('raise an
exception! ' + str(exc_type))
return False # Propagate
if __name__ == '__main__':
with TraceBlock()
as action:
action.message('test1')
print('reached')
with TraceBlock()
as action:
action.message('test2')
raise TypeError
print('not
reached')
7.
其它内置类型
7.1.
modules
模块字典:__dict__ module’s symbol table
它可以直接修改,但不建议,m.__dict__={}是非法的。
7.2.
methods and functions
Function objects are created by function
definitions. The only operation on a function object is to call it: func(argument-list).
Methods are functions that are called using the
attribute notation. There are two flavors: built-in methods (such as append() on lists) and
class instance methods. Built-in methods are described with the types that
support them.
可以理解为mehtods是可以使用属性记号调用的functions。
另外有个概念bound method需要注意,简单来说,调用绑定方法时会自动传入self参数。
print(type(None))
>>> <class 'NoneType'>
7.3.
type
可以参考元类。
7.4.
NULL
This object is returned by functions that
don’t explicitly return a value. It supports no special operations. There is
exactly one null object, named None (a built-in name). type(None)() produces
the same singleton.
It is written as None.
7.5.
Special Attributes
The
implementation adds a few special read-only attributes to several object types,
where they are relevant. Some of these are not reported by thedir() built-in
function.
object.__dict__
A dictionary or other mapping object used
to store an object’s (writable) attributes.
instance.__class__
The class to which a class instance
belongs.
class.__bases__
The tuple of base classes of a class
object.
definition.__name__
The name of the class, function, method,
descriptor, or generator instance.
definition.__qualname__
The qualified name of the class,
function, method, descriptor, or generator instance.
New in version 3.3.
class.__mro__
This attribute is a tuple of classes that
are considered when looking for base classes during method resolution.
class.mro()
This method can be overridden by a
metaclass to customize the method resolution order for its instances. It is
called at class instantiation, and its result is stored in __mro__.
class.__subclasses__()
Each class keeps a list of weak references
to its immediate subclasses. This method returns a list of all those references
still alive. Example:
>>> int.__subclasses__()
[<class 'bool'>]
笔记-pyton内置数据类型的更多相关文章
- Python笔记004-Python最基本内置数据类型和运算符
第二章(1)Python编程基础概念 1. 最基本内置数据类型和运算符 每个对象都有类型,Python 中最基本的内置数据类型: 1. 整数 整数,2345 ,10 ,50 2. 浮点型 小数,3.1 ...
- Python中内置数据类型list,tuple,dict,set的区别和用法
Python中内置数据类型list,tuple,dict,set的区别和用法 Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个内置数据类型功不可没,他们即是list, ...
- python计算非内置数据类型占用内存
getsizeof的局限 python非内置数据类型的对象无法用sys.getsizeof()获得真实的大小,例: import networkx as nx import sys G = nx.Gr ...
- Python内置数据类型之Dictionary篇
1.查看函数XXX的doc string. Python的函数是有属性的,doc string便是函数的属性.所以查看函数XXX的属性的方法是模块名.XXX.__doc__ 2.模块的属性 每个模块都 ...
- Python内置数据类型总结
python的核心数据类型:(很多语言之提供了数字,字符串,文件数据类型,其他形式的数据类型都以标准库的形式表示 也就是用之前需要import ) ,但是python有很多都是内置的,不需要impor ...
- Hive内置数据类型
Hive的内置数据类型可以分为两大类:(1).基础数据类型:(2).复杂数据类型.其中,基础数据类型包括:TINYINT,SMALLINT,INT,BIGINT,BOOLEAN,FLOAT,DOUBL ...
- 二、JAVA基本数据类型:内置数据类型,引用类型
变量的值存储在内存中,内存管理系统通过变量的类型分配存储空间,且该空间只能存储该类型数据,通过定义不同的变量,在内存中储存不同类型的数据. JAVA的两大数据类型 1. 内置数据类型 2.引用数据类型 ...
- Python的四个内置数据类型list, tuple, dict, set
Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个内置数据类型功不可没,他们即是list, tuple, dict, set.这里对他们进行一个简明的总结. List ...
- Python作业---内置数据类型
实验2 内置数据类型 实验性质:验证性 一.实验目的 1.掌握内置函数.列表.切片.元组的基本操作: 2.掌握字典.集合和列表表达式的基本操作. 二.实验预备知识 1.掌握Python内置函数的基/本 ...
随机推荐
- css多行文本溢出显示省略号
HTML: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <met ...
- 用Android studio进行 OpenCV 开发的第一个项目
我的天! 折腾了好久终于搭建成功了第一个项目. 项目环境: Windows 7 家庭普通版 64位 Android studio 1.5.1 OpenCV-2.4.9-android-sdk 基于 ...
- ActionMethod_DMI_动态方法调用
Action执行的时候并不一定要执行execute方法可以在配置文件中配置Action的时候用method=来指定执行那个方法,也可以在url地址中动态指定(动态方法调用DMI)(推荐) 动态方法调用 ...
- Spring+SpringMVC+Mybatis+Shiro环境搭建之IDEA下搭建Maven项目
运行IntelliJ IDEA 2016.3.2(64)编译器新建项目 在弹出的窗体中选择maven,然后勾选要建的maven模板--这里选webApp 然后填入相应的maven项目组信息(Gro ...
- 查看Linux网卡地址,网络地址
查看网络地址 ip a 或ip addr show 或ifconfig,此指令在部分linux系统中不支持
- ARM实验4—按键轮询实验
key_poll按键轮询实验 实验内容: 通过FS_4412开发板上的按键控制LED灯并打印信息. 实验目的: 熟悉开发环境的使用. 掌握猎户座4412处理器的GPIO接口, 实验平台: FS4412 ...
- 【js基础修炼之路】- 手把手教你实现bind
手写bind前我们先回顾一下bind有哪些特性,以便更好的理解bind和实现bind. bind的特性 var obj = { a: 100, say(one, two) { console.log( ...
- DEM精度评价自动化系统的成果展示
程序员:左正康 完成时间:2013/12/3 系统开发背景: 原始的DEM精度评价方法:采用ArcGIS结合Excel的方式完成DEM的精度评价.具体操作是:利用ArcGIS工具箱中的创建TIN,T ...
- Poj(2312),坦克大战,BFS的变形
题目链接:http://poj.org/problem?id=2312 挺有趣的一道题目,然而很容易WA,我就WA了一次,虽然我Debug的时候已经知道哪里出问题了,就是比如说我搜到B和E时,从B搜第 ...
- OOM导致的备库raylog损坏导致主从复制异常
问题发现告警数据库出现复制中断,延迟超过100秒 问题排查复制信息检查,通过’show slave status\G’命令可以查看复制线程详细的工作状态,对于判断复制中断的原因有一些指导性意义.当时的 ...