1.1 、str与repr:将值转化为字符串,str与int、long一样,是一种类型,而repr仅仅是个函数
1.2、input与raw_input的区别
input会假设用户输入的是合法的python表达式(或多或少有些鱼repr相反的意思)
raw_input会把所有的输入当做原始数据(raw data)
1.3、长字符串:如果需要写一个非常非常长的字符串,就需要用三引号 ''' 或者"""
原始字符串:以r标记 print r'c:\nowhere',不能在原始字符串的最后加\,如:>>>print r"This is illegal\"//非法 >>>print r"This is illegal" "\\"
Unicode 字符串:unicode字符串每个字符占16个字节,u"hello, world!"
2、通用序列操作
2.1、 >>>a = 'hello'
>>>a[-1]
'o'
2.2、如果只对于输入的某个数字感兴趣
>>>forth = raw_input("year:")[3]
year:2005
'5'
2.3、分片:
2.3.1、优雅的捷径
>>>a = [1,2,3,4,5,6,7,8,9,10]
>>> a[3:6]
[4,5,6]
>>>a[-1]
10
>>>a[-3:-1]
[8,9]
>>>a[:3]
[1,2,3]
>>>a[:]
[1,2,3,4,5,6,7,8,9,10]
2.3.2、更大的步长
>>> a[::3]
[1, 4, 7, 10]
>>> a[::-2]
[10, 8, 6, 4, 2]
>>> a[2:8:2]
[3, 5, 7]
>>> a[2:8:3]
[3, 6]
2.4、序列相加:同一类型的序列可以相加
>>> [1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
>>> 'hello' + 'world'
'helloworld'
>>> 'hello' + [1,3]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'list' objects
2.5、序列乘法
>>> 'python'*5
'pythonpythonpythonpythonpython'
>>> a =[None]*10
>>> a
[None, None, None, None, None, None, None, None, None, None]
2.6、成员资格
>>> a = 'hezhonglei'
>>> 'h' in a
True
>>> 't' in a
False
>>> raw_input("what is your name?:") in "zhulili"
what is your name?:zhu
True
2.7、长度、最大值、最小值
>>> num = [1,2,3]
>>> len(num)
3
>>> max(num)
3
>>> min(num)
1
3、列表:Python的“苦力”(字符串不能像列表一样被修改,所以有时候根据字符串创建列表的必要的(list函数))
3.1、基本的列表操作
>>> names = ['he','zhong','asdf','lei']
>>> names
['he', 'zhong', 'asdf', 'lei']
>>> del names[2]
>>> names
['he', 'zhong', 'lei']
3.2、分片赋值
>>> name = list('pythdfas')
>>> name[-4:] = list('on')
>>> name
['p', 'y', 't', 'h', 'o', 'n']
>>> name = list("pyon")
>>> name[2:2] = list("th")
>>> name
['p', 'y', 't', 'h', 'o', 'n']
3.3、列表方法
3.3.1、append:在列表末尾追加新的元素
>>> name = list('pytho')
>>> name.append('n')
>>> name
['p', 'y', 't', 'h', 'o', 'n']
3.3.2、count:计算某个元素在列表中出现的次数
>>> name = 'zhulili'
>>> name.count('l')
2
3.3.3、extend:在列表的末尾一次性添加另一个列表的多个值(可以用分片来实现)
>>> name = list('pyth')
>>> name.extend('on')
>>> name
['p', 'y', 't', 'h', 'o', 'n']
3.3.4、index:从列表中找出某个值第一个匹配项的索引位置
>>> name = ['hello', 'world']
>>> name.index('hello')
0
>>> name.index('hell')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.index(x): x not in list
3.3.5、insert:将对象插入到列表中(可以用分片来实现)
>>> numbers = [1,2,3,5,6]
>>> numbers.insert(3,'four')
>>> numbers
[1, 2, 3, 'four', 5, 6]
3.3.6、pop:从列表中移除一个元素,默认是最后一个元素,并返回该元素的值
>>> x = [1,2,3]
>>> x.pop()
3
3.3.7、remove:移除列表中第一个匹配的项
>>> x = ['to', 'be', 'or', 'not', 'to','be']
>>> x.remove('be')
>>> x
['to', 'or', 'not', 'to', 'be']
>>> x.remove('fds')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
3.3.8、reverse:将列表中的元素反向存放
>>> x = [1,2,3]
>>> x.reverse()
>>> x
[3, 2, 1]
3.3.9、sort:用于在原位置对列表进行排序
>>> x = [5,3,4]
>>> x.sort()
>>> x
[3, 4, 5]
>>> y = x.sort()
>>> y
>>> print y
None
>>> x = [4,6,2,1,7,9]
>>> y = x[:]
>>> y.sort()
>>> x
[4, 6, 2, 1, 7, 9]
>>> y
[1, 2, 4, 6, 7, 9]
>>> x = [4,6,2,1,7,9]
>>> y = x
>>> y.sort()
>>> x
[1, 2, 4, 6, 7, 9]
>>> y
[1, 2, 4, 6, 7, 9]
3.3.10、sorted:获取已排序好的副本
>>> x = [4,6,2,1,7,9]
>>> y = sorted(x)
>>> x
[4, 6, 2, 1, 7, 9]
>>> y
[1, 2, 4, 6, 7, 9]
3.3.11、为sort提供特定的compare函数
>>> x = [4,6,2,1,7,9]
>>> x.sort(cmp)
>>> x
[1, 2, 4, 6, 7, 9]
3.3.12、关键字参数:key
>>> x = ['fdshfdsfds', 'safds','fsadfdsfdsfdsf']
>>> x.sort(key = len)
>>> x
['safds', 'fdshfdsfds', 'fsadfdsfdsfdsf']
3.3.13、reverse:逆序参数
>>> x = [4,6,2,1,7,9]
>>> x.sort(reverse=True)
>>> x
[9, 7, 6, 4, 2, 1]
4、元组,不可变序列
元组和列表一样,也是一种序列,唯一的不同是元组不能修改
4.1、创建元组
>>> 1,2,3
(1, 2, 3)
>>> ()
()
>>> 42,
(42,)
>>> 3*(42+2)
132
>>> x*(42+2,)
>>> 3*(42+2,)
(44, 44, 44)
4.2、tuple:通过序列构造元组
>>> a=tuple("pychon")
>>> a
('p', 'y', 'c', 'h', 'o', 'n')
小结:序列:序列是一种数据结构,它包含的元素都进行了编号(从0开始)。典型的序列包括列表,
字符串和元组, 其中列表是可变的(可以进行修改),而元组和字符串是不可变的
(一旦创建了就是固定的)。通过分片操 作可以访问序 列的一部分,其中分片需要两个
索引号来指出分片的起始位置和结束位置。要想改变列表, 则要对相应的位置进行 赋值,
或者使用赋值语句重写整个分片。