python教程: python 介绍
1、python 命令行解释提示符下
输入control + p 命令提示符向上查找
输入control + n 命令提示符向下查找
2、在交互模式中,最后打印的表达式的值被赋予给变量_
3、在字符串第一个引号前添加r字符,可以避免通过\转义字符
print r'c:\some\name'
4、使用三个引号包含的字符串可以跨越多行
“””…””"
‘’’…’''
注:字符串的首行将自动包含行的结尾换行符,通过在行首添加\可以避免
print """\
usage: thingy [options]
-h display this usage message
-h hostname hostname to connect to
"""
5、字符串连接 (+, *)
'abc' + 'def'# 字符串连接,可以连接变量
'abc' * 3 # 字符串重复
'py' 'thon'# 两个字符串字面值自动连接,不包括变量或表达式
# 字符串连接
>>> text = ('put several strings within parentheses '
'to have them joined together.')
>>> text
'put several strings within parentheses to have them joined together.'
6、字符串索引
字符串的下标从0开始索引,字符串是没有分割字符的类型,一个字符是一个简单的长度为1字符串
>>> word = 'python'
>>> word[0] # character in position 0
'p'
7、负数从字符串右侧开始计数
>>> word[-1] # last character
'n'
注:-0相当于0,负数从-1开始
8、字符串支持切片,索引获取单个字符,切片获取子字符串
>>> word[0:2] # characters from position 0 (included) to 2 (excluded)
'py'
>>> word[2:5] # characters from position 2 (included) to 5 (excluded)
'tho'
注:切片的开始参数总是被包含,结尾总是被排除的。
9、字符串切片默认值,第一个索引省去默认为0,第二个索引省去默认为切片的长度;
>>> word[:2] # character from the beginning to position 2 (excluded)
>>> word[4:] # characters from position 4 (included) to the end
'on'
>>> word[-2:] # characters from the second-last (included) to the end
10、最简单理解字符串切片原理是记住字符之间的位置,左侧的字符是0,右侧的字符是n就是索引n:
+---+---+---+---+---+---+
| p | y | t | h | o | n |
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1
11、使用较大的索引将会出现如下错误
>>> word[42] # the word only has 7 characters
traceback (most recent call last):
file "<stdin>", line 1, in <module>
indexerror: string index out of range
12、python字符串是不可以被修改的,给字符串索引位置赋值将会出现如下错误!
>>> word[0] = 'j'
...
typeerror: 'str' object does not support item assignment
>>> word[2:] = 'py'
# 如果需要,你可以创建一个新的字符串。
13、python 2.0以后引入了新的存储文本的数据类型,unicode对象。他可以很好的存储、维护unicode数据并提供自动转换。
unicode常被用来解决国际化。
14、unicode字符串的建立
>>> u'hello world !'
u'hello world !'
# 字符串前面的小写的u是被支持用来创建unicode字符的,如果你想使用特殊字符,请参考unicode-escape。例如:
>>> u'hello\u0020world !'
注:\u0020表示unicode字符0x0020(空格)
15、原始模式字符串,字符串引号前添加'ur'前缀,python使用raw-unicode-escape编码。如果使用了不对等条件的反斜线将退出\uxxxx转换。
>>>
>>> ur'hello\u0020world !'
>>> ur'hello\\u0020world !'
u'hello\\\\u0020world !'
主:原始模式是非常实用的,例如:正则表达式需要原始模式
除了python标准的编码,python提供了完整的方式从一个已知的编码建立unicode编码。
16、内建函数unicode()提供访问所有已注册的unicode编码(c0ders and decoders).一些已知的编码latin-1, ascii, utf-8, and utf-16能被转换。
字符2个变量长度的编码存储unicode字符在一个或者更多的字节。默认编码通常被设置为ascii, 通过传递字符区间在0-127,传递其他字符将被拒绝并产生一
个错误,当一个unicode字符打印、写到一个文件、或通过str()转换,转换将空间使用默认编码。
>>> u"abc"
u'abc'
>>> str(u"abc")
'abc'
>>> u"ü"
u'\xe4\xf6\xfc'
>>> str(u"ü")
file "<stdin>", line 1, in ?
unicodeencodeerror: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
通过使用指定编码可以转换一个unicode字符串到一个8位的字符串,unicode提供一个encode()方法带一个编码名字的参数,小写的字母编码是被推荐的。
>>> u"ü".encode('utf-8')
'\xc3\xa4\xc3\xb6\xc3\xbc'
17、如果你有一个指定编码的数据想要产生一个相等的unicode字符串,你可以使用unicode()方法,第二个参数带上编码名称。
>>> unicode('\xc3\xa4\xc3\xb6\xc3\xbc', 'utf-8')
18、列表
列表通过在尖括号之间使用逗号分隔值,列表可以包含不同类型的值,但同行是相同的。
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
19、列表和字符串一样属于序列类型,同样可以被索引和切片
>>> squares[0] # indexing returns the item
1
>>> squares[-1]
25
>>> squares[-3:] # slicing returns a new list
[9, 16, 25]
20、返回一个新列表的拷贝
>>> squares[:]
21、列表支持操作符
>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
22、不像字符串,列表可以改变自己的内容
>>> cubes = [1, 8, 27, 65, 125] # something's wrong here
>>> 4 ** 3 # the cube of 4 is 64, not 65!
64
>>> cubes[3] = 64 # replace the wrong value
>>> cubes
[1, 8, 27, 64, 125]
23、列表可以使用append()方法在结尾添加元素
>>> cubes.append(216) # add the cube of 6
>>> cubes.append(7 ** 3) # and the cube of 7
[1, 8, 27, 64, 125, 216, 343]
24、列表的切片可以被赋值,甚至可以改变列表的长度和清空列表
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> # replace some values
>>> letters[2:5] = ['c', 'd', 'e']
>>> # now remove them
>>> letters[2:5] = []
['a', 'b', 'f', 'g']
>>> # clear the list by replacing all the elements with an empty list
>>> letters[:] = []
[]
25、内建的len()函数可以被应用到列表
>>> letters = ['a', 'b', 'c', 'd']
>>> len(letters)
4
26、列表可以嵌套列表(建立一个列表包含其他列表)
>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'
# /doc/tutorial/introduction.html