序列类型操作符
序列操作符 |
作用 |
seq[ind] |
获得下标为ind的元素 |
seq[ind1:ind2] |
获得下标从ind1到ind2间的元素集合 |
seq*expr |
序列重复expr次 |
seq1+seq2 |
连接序列seq1和seq2 |
obj in seq |
判断obj元素是否包含在seq中 |
obj not in seq |
判断obj元素是否不包含在seq中 |
切片索引
>>> s = 'abcde' >>> i = -1 >>> for i in range(-1, -len(s), -1): ... print s[:i] ... abcd abc ab a
字符串连接+
>>> '%s %s' % ('Spanish', 'Inquisition') 'Spanish Inquisition' >>> s = ' '.join(('Spanish', 'Inquisition', 'Made Easy')) >>> s 'Spanish Inquisition Made Easy' >>> >>> ('%s%s' % (s[:3], s[20])).upper() 'SPAM'
格式化操作符
字符串格式化符号
格式化字符 |
转换方式 |
%c |
转换成字符(ASCII码值,或者长度为一的字符串) |
%r |
优先用repr()函数进行字符串转换 |
%s |
优先用str()函数进行字符串转换 |
%d / %i |
转成有符号十进制数 |
%% |
输出% |
。。。。 |
格式化操作符辅助指令
符号 |
作用 |
* |
定义宽或者小数精度 |
- |
用做左对齐 |
+ |
在正数前面显示空格 |
在正数前面显示空格 |
|
# |
在八进制数前面显示零('0'),在十六进制前面显示'0X'或者'0x'(取决于用的是x还是X) |
0 |
显示的数字前面填充'0'而不是默认的空格 |
% |
'%%'输出一个单一的'%' |
(var) |
映射变量(字典参数) |
m.n |
m是显示的最小总宽度,n是小数点后的位数 |
>>> "%x" % 108 '6c' >>> "%X" % 108 '6C' >>> "%#X" % 108 '0X6C' >>> "%#x" % 108 '0x6c' # 整型和字符串输出 >>> "%+d" % 4 '+4' >>> "%+d" % -4 '-4' >>> "we are at %d%%" % 100 'we are at 100%' >>> 'Your host is: %s' % 'earth' 'Your host is: earth' >>> 'Host: %s\tPort: %d' % ('mars', 80) 'Host: mars\tPort: 80' >>> num = 123 >>> 'dec: %d/oct: %#o/hex: %#X' % (num, num, num) 'dec: 123/oct: 0173/hex: 0X7B' >>> >>> "MM/DD/YY = %02d/%02d/%d" % (2, 15, 67) 'MM/DD/YY = 02/15/67' >>> >>> w, p = 'web', 'page' >>> 'http://xxx.yyy.zzz/%s/%s.html' % (w, p) ' # 将把字典类型的参数提供给格式化操作符 >>> 'There are %(howmany)d %(lang)s Quotation Symbols' % {'lang': 'Python', 'howmany': 3} 'There are 3 Python Quotation Symbols'
列表类型操作符和列表解析
>>> [ i * 2 for i in [8, -2, 5] ] [16, -4, 10] >>> [ i for i in range(8) if i%2 == 0 ] [0, 2, 4, 6]
内建函数
1.1 标准类型函数cmp()
cmp()是比较两个同类对象的函数
1.2 序列类型函数
1.2.1 len()
对字符串来说len()返回字符串的长度,就是字符串包含的字符个数。对于列表或者元组来说,它会像你想象的那样返回列表或者元组的元素个数,容器里面的每个对象被作为一个项来处理。
1.2.2 max()和min()
1.2.3 sorted()和reversed()
>>> s = ['They','stamp','them','when',"they're",'small'] >>> for t in reversed(s): ... print t ... small they're when them stamp They >>> for t in reversed(s): ... print t, ... small they're when them stamp They >>> sorted(s) ['They', 'small', 'stamp', 'them', "they're", 'when']
1.2.4 enumerate()和zip()
# enumerate() >>> albums = ['tales','robot','pyramid'] >>> for i,album in enumerate(albums): ... print i,album ... 0 tales 1 robot 2 pyramid # zip() >>> fn = ['ian','stuart','david'] >>> ln = ['bairnson','elliott','paton'] >>> for i,j in zip(fn,ln): ... print ('%s %s' % (i,j)).title() ... Ian Bairnson Stuart Elliott David Paton
1.2.5 sum()
>>> a=[6,4,5] >>> sum(a) 15 >>> sum(a,5) 20 >>> a=[6.,4.,5.] >>> sum(a) 15.0
1.2.6 list()和tuple()
2.1 列表类型的内建函数
列表类型支持的所有方法: >>> dir(list) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] |
3.1 列表的特殊特性
3.1.1 堆栈
push表示把一个对象添加到堆栈中,pop表示出栈
演示实例:
4.1 元组操作符和内建函数
4.1.1 标准类型操作符、序列类型操作符和内建函数
1、创建、重复、连接操作
>>> t = (['xyz',123], 23, -103.4) >>> t (['xyz', 123], 23, -103.40000000000001) >>> t * 2 (['xyz', 123], 23, -103.40000000000001, ['xyz', 123], 23, -103.40000000000001) >>> t = t + ('free', 'easy') >>> t (['xyz', 123], 23, -103.40000000000001, 'free', 'easy')
2、成员关系操作、切片操作
>>> 23 in t True >>> 123 in t False >>> t[0][1] 123 >>> t[1:] (23, -103.40000000000001, 'free', 'easy')
3、内建函数
>>> str(t) "(['xyz', 123], 23, -103.40000000000001, 'free', 'easy')" >>> len(t) 5 >>> max(t) 'free' >>> min(t) -103.40000000000001 >>> cmp(t, (['xyz', 123], 23, -103.40000000000001, 'free', 'easy')) 0 >>> cmp(t, (['xyz', 123], 23, -103.4, 'free', 'easy')) 0 >>> list(t) [['xyz', 123], 23, -103.40000000000001, 'free', 'easy']
4、操作符
>>> (4,2) < (3,5) False >>> (2,4) < (3,-1) True >>> (2,4) == (3,-1) False >>> (2,4) == (2,4) True
5.1 元组的特殊特性
5.1.1 不可变性元组
标准不可变类型:数字、字符串、元组字符串
改变元组或字符串的方法:
>>> s = 'first' >>> s = s + ' second' >>> s 'first second' >>> t = ('third','fourth') >>> t ('third', 'fourth') >>> t = t + ('fifth','sixth') >>> t ('third', 'fourth', 'fifth', 'sixth')
6.1 相关模块
6.1.1 拷贝Python对象、浅拷贝和深拷贝
>>> person = ['name',['savings',100.00]] >>> hubby = person[:] >>> hubby ['name', ['savings', 100.0]] >>> wifey = list(person) >>> wifey ['name', ['savings', 100.0]] >>> [id(x) for x in person, hubby, wifey] [140009725080496, 140009725061312, 140009725080640] >>> hubby[0] = 'joe' >>> wifey[0] = 'jane' >>> hubby,wifey (['joe', ['savings', 100.0]], ['jane', ['savings', 100.0]]) >>> hubby[1][1] = 50.00 >>> hubby,wifey (['joe', ['savings', 50.0]], ['jane', ['savings', 50.0]])