四、数据类型
Python有如下几种常用的数据类型:
- 基本数字类型:其中有长整型,布尔值,浮点值,复数
- 字符串类型
- 列表类型
- 元组类型
- 字典类型
其中字符串,列表和元组是序列型数据类型,所有序列数据类型都满足以下几种操作:
-
序号索引操作 seq[ index ] : 获得下标为index的元素
-
切片操作 seq[ index1 : index2 ] : 获得从下标index1 到index2之间的元素集合,其中不包括index2所指的元素
-
重复操作 seq * expression :将序列重复expression次
-
连接操作 seq1 + seq2 : 连接序列seq1和seq2
-
对象是否存在操作 obj in seq : obj是否存在于seq中
-
对象不存在操作 obj not in seq : 判断obj元素是否不存在seq中
序列类型有一些系统提供的BIF内建函数转换操作:
- list( iter ) : 把可迭代对象转为列表
- str( obj ) : 把obj对象转换成字符串
- unicode( obj ) : 把对象转成unicode字符串
- tuple( iter ) : 把一个可迭代对象转为元组
除此之外系统还提供了一些序列类型可用的内建操作函数:
- enumerate( iter ) : 接收可迭代对象作为参数,返回一个enumerate对象,这个对象是由iter每个元素的index和item值组成的元组。
- len( seq ) : 返回seq的长度
- max( iter, key = None ) : 返回iter或者(arg0...argn)中的最大值,如果指定了key,这个key必须是一个可以传给sort()方法用于比较的回调函数。
- min( iter , key = None ) : 同上取反
- reversed( seq ) : 接收一个序列作为参数然后返回一个逆序访问的迭代器
- sorted( iter, func = None , key = Name, reverse = False ) : 接受一个可迭代对象作为参数,返回一个有序的列表,可以增加回调函数关键字等可选参数。
- sum( seq , init = 0 ) : 返回seq和可选参数init的总和
- zip( [ iter0, iter1,....iterN]) : 返回一个列表,这个列表是这iterN个可迭代序列的第一个元素组成的元组。以此类推
1、基本数字类型直接略过
2、字符串类型
字符串类型的数据是不可变数据,即字符串生成了就不能变了,'abc'生成后就是它了,你不能通过赋值语句把'abc'改成'bbc',如果你实在想改,只能另生成一个'bbc'赋值给对象。原先的'abc'对象会因为引用为0而自动消失。
Python中字符串被定义为引号之间的字符集合,Python支持使用成对的单引号、双引号或者三引号来定义字符串,其中三引号和双引号可以用来包含特殊的字符串。常见的操作有 in操作,not in 操作, [ ] 索引操作,[ : ] 切片操作, + 字符串连接操作, * 字符串重复操作下面是一些代码展示:
>>> print 'Hi I am eric , I am learning Python!'
Hi I am eric , I am learning Python!
>>> print "Hi I am eric , I am learning Python!"
Hi I am eric , I am learning Python!
>>> print '''Hi I am eric , I am learning Python!'''
Hi I am eric , I am learning Python!
>>> print 'Hi I am "%s" , I am learning %s' %('eric','python programming languag
e')
Hi I am "eric" , I am learning python programming language
>>> a = 'python'
>>> a[0]
'p'
>>> a[-1]
'n'
>>> a[-2]
'o'
>>> a[-3]
'h'
>>> a[1:-1]
'ytho'
>>> a[1:]
'ython'
>>> a[0:-1:2]
'pto'
>>> b = ' is great!'
>>> a+b
'python is great!'
>>> a+' '+b
'python is great!'
>>> a+' '+b
'python is great!'
>>> a*2+b*3
'pythonpython is great! is great! is great!'
>>> '*'*15
'***************'
>>> c = ''' python
... is my favorite'''
>>> c
' python \nis my favorite'
>>> print c
python
is my favorite
>>> names = ( 'faye' , ' lenna' , 'Daylen')
>>> print names[0]
faye
>>> print names[3]
Traceback (most recent call last):
File "", line 1, in
IndexError: tuple index out of range
>>> len(names)
3
>>> names + ('eric' , 'tsinghua')
('faye', ' lenna', 'Daylen', 'eric', 'tsinghua')
>>> names += ('eric' , 'tsinghua')
>>> names
('faye', ' lenna', 'Daylen', 'eric', 'tsinghua')
>>> sub-names = names[0:-1:2]
File "", line 1
SyntaxError: can't assign to operator
>>> names[0:-1:2]
('faye', 'Daylen')
>>> name1,name2 = names[0:-1:2]
>>> name1,name2
('faye', 'Daylen')
>>>>>> names
('faye', ' lenna', 'Daylen', 'eric', 'tsinghua')
>>> names[::-1]
('tsinghua', 'eric', 'Daylen', ' lenna', 'faye')
>>> s = 'abcde'
>>> i = -1
>>> for i in range(-1,-len(s),-1):
... print s[:i]
...
abcd
abc
ab
a
>>> for i in [None]+range(-1,-len(s),-1):
... print s[:i]
...
abcde
abcd
abc
ab
a
>>>
>>> a = ' this is python!'
>>> 'this' in a
True
>>> 't' in a
True
>>> ' ' in a
True
>>> 'q' in a
False
>>>
常见的字符串操作函数有如下:
字符串的删除只能是将要删除的字符去除后再重新粘合原有的串,如果要清空字符串只能赋值一个空串,有关字符串的使用可以看下面的代码例子:
>>> a = 'hello world'
>>> a = a[:3] + a[4:]
>>> a
'helo world'
>>> a = ''
>>> a
''
>>> '%s %s' %('ppanish','Inquisition')
'ppanish Inquisition'
>>> s = ''.jion( ('spanish','inquistion','made') )
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'str' object has no attribute 'jion'
>>> s = ''.join( ('spanish','inquistion','made') )
>>> s
'spanishinquistionmade'
>>> s = ' '.join( ('spanish','inquistion','made') )
>>> s
'spanish inquistion made'
>>> 'There are %(howmany)d %(lang)s Quotation Symbols' %\
... {'lang':'Python','howmany':3}
'There are 3 Python Quotation Symbols'
>>> 'There are %d %s Quotation Symbols' %\
... (3,'Python')
'There are 3 Python Quotation Symbols'
>>> '%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%'
>>> 'dec:%d/oct:%#ohex:%#x' %(123,123,123)
'dec:123/oct:0173hex:0x7b'
>>> 'MM/DD/YY = %02d/%02d/%d' %(2,15,67)
'MM/DD/YY = 02/15/67'
>>>
>>> a = 'c:\windows\temp\readme.txt'
>>> a
'c:\\windows\temp\readme.txt'
>>> print a
eadme.txts emp
>>> repr(a)
"'c:\\\\windows\\temp\\readme.txt'"
>>> a = r'c:\windows\temp\readme.txt'
>>> a
'c:\\windows\\temp\\readme.txt'
>>> print a
c:\windows\temp\readme.txt
>>> repr(a)
"'c:\\\\windows\\\\temp\\\\readme.txt'"
>>>
>>> s = 'foobar'
>>> min(s)
'a'
>>> max(s)
'r'
>>> for i , t in enumerate(s):
... print i,t
...
0 f
1 o
2 o
3 b
4 a
5 r
>>> s, t = 'foo','bar'
>>> zip(s,t)
[('f', 'b'), ('o', 'a'), ('o', 'r')]
>>> isinstance('123',str)
True
>>> isinstance(u'123',str)
False
>>> isinstance(ur'123',str)
False
>>> isinstance(ru'123',str)
File "", line 1
isinstance(ru'123',str)
^
SyntaxError: invalid syntax
>>> chr(65)
'A'
>>> chr(66)
'B'
>>> ord('b')
98
>>> ord('B')
66
>>> quest = 'what is your favorite color?'
>>> quest.capitalize()
'What is your favorite color?'
>>>
>>> quest.center(40)
' what is your favorite color? '
>>>
>>> quest.count('or')
2
>>> quest.count('your')
1
>>> quest.count(' ')
4
>>> quest.endswith('blue')
False
>>> quest.endswith('color')
False
>>> quest.endswith('color?')
True
>>> quest.find('or',30)
-1
>>> quest.find('or',22)
25
>>> quest.find('or',10)
16
>>> quest.find('or',1)
16
>>> ':'.join(quest.split())
'what:is:your:favorite:color?'
>>> quest.replace('favorite color','quest')
'what is your quest?'
>>> quest.upper()
'WHAT IS YOUR FAVORITE COLOR?'
>>>
>>> print '''e dfds\tdfad\ndfs'''
e dfds dfad
dfs
>>> print 'e dfds\tdfad\ndfs'
e dfds dfad
dfs
>>> a = '''hi
... nihao'''
>>> a
'hi\nnihao'
到此为止,字符串模块的基本操作和函数都已经覆盖了,需要记忆的是有些操作是所有序列都存在的操作,例如切片啊,索引啊,最大最小啊,枚举啊,ZIP啊,重复,判断是否存在等。后面列出的那些字符串的操作函数是只有字符串才有的,要常练习才是。
最后附序列对象和内建函数对应的使用表: