python 2.7 中文教程-3:非正式教程
来源:互联网 发布:淘宝助理上架教程 编辑:程序博客网 时间:2024/06/05 14:57
下面的例子中,输入和输出分别由>>>和...开始。
Python中的注释以#字符起始,到行尾。实例如下:
Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> the_world_is_flat = 1>>> if the_world_is_flat:... print "Be careful not to fall off!"... Be careful not to fall off!
将Python当做计算器
数值
解释器像简单的计算器:可以输入表达式,它会返回值。表达式语法很简单:运算符 + , - , * 和 / 与其它语言一样(例如Pascal或C);括号用于分组。例如:
>>> 2 + 24>>> 50 - 5*620>>> (50 - 5.0*6) / 45.0>>> 8 / 5.01.6
"/" 的返回类型取决于操作数。如果两个操作数都是int类型,执行地板除(参见https://docs.python.org/2 /glossary.html#term-floor-division)和返回int。如果操作数有浮点数,执行经典除法和并返回浮点数,浮点数的地板 除可以使用//。取余使用%:
>>> 17 / 3 # int / int -> int5>>> 17 / 3.0 # int / float -> float5.666666666666667>>> 17 // 3.0 # explicit floor division discards the fractional part5.0>>> 17 % 3 # the % operator returns the remainder of the division2>>> 5 * 3 + 2 # result * divisor + remainder17
"**"表示乘方:
>>> 5 ** 2 # 5 squared25>>> 2 ** 7 # 2 to the power of 7128
等号( '=' )用于给变量赋值:
>>> width = 20>>> height = 5*9>>> width * height900
同一值可以同时赋给几个变量:
>>> x = y = z = 0 # Zero x, y and z>>> x0>>> y0>>> z0
变量在使用前必须"定义"(赋值),否则会出错:
>>> nTraceback (most recent call last): File "<stdin>", line 1, in <module>NameError: name 'n' is not defined
支持浮点数,混合计算时会自动整型转为浮点数:
>>> 3 * 3.75 / 1.57.5>>> 7.0 / 23.5
交互模式中,最近表达式的值赋给变量 _ 。更方便连续计算把Python当作桌面计算器,例如:
>>> tax = 12.5 / 100>>> price = 100.50>>> price * tax12.5625>>> price + _113.0625>>> round(_, 2)113.06
此变量对于用户是只读的。
除了int和float,还有fractions(https://docs.python.org/2/library/fractions.html#fractions.Fraction)和decimal(https://docs.python.org/2/library/decimal.html#decimal.Decimal)。
下面的复数部分很少使用,通常可以不阅读。
支持复数,虚数带有后缀j或J,有非零实部的复数写为(real+imagj),或者用complex(real, imag)函数创建。
>>> 1j * 1J(-1+0j)>>> 1j * complex(0,1)(-1+0j)>>> 3+1j*3(3+3j)>>> (3+1j)*3(9+3j)>>> (1+2j)/(1+1j)(1.5+0.5j)
复数的实部和虚部总是记为两个浮点数。要从复数z中提取实部和虚部,使用z.real和 z.imag。
>>> a=1.5+0.5j>>> a.real1.5>>> a.imag0.5
浮点数和整数转换函数(float(), int()和long())不适用于复数。没有方法把复数转成实数。函数abs(z)用于取模(为浮点数)或z.real取实部:
>>> a=3.0+4.0j>>> float(a)Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: can't convert complex to float>>> a.real3.0>>> a.imag4.0>>> abs(a) # sqrt(a.real**2 + a.imag**2)5.0
字符串
字符串可以包含在单引号或双引号中。
>>> 'spam eggs''spam eggs'>>> 'doesn\'t'"doesn't">>> "doesn't""doesn't">>> '"Yes," he said.''"Yes," he said.'>>> "\"Yes,\" he said."'"Yes," he said.'>>> '"Isn\'t," she said.''"Isn\'t," she said.'
解释器按照字符串被输入的方式显示字符串,通常包含在单引号中,如果内容包含包含单引号,则包含在双引号中。
print会以更可视的格式显示:
>>> '"Isn\'t," she said.''"Isn\'t," she said.'>>> print '"Isn\'t," she said.'"Isn't," she said.>>> s = 'First line.\nSecond line.' # \n means newline>>> s'First line.\nSecond line.'>>> print s # with print, \n produces a new lineFirst line.Second line.
字符串前面添加'r'表示原始字符串,里面的反斜杠不会转义:
>>> print 'C:\some\name' # here \n means newline!C:\someame>>> print r'C:\some\name' # note the r before the quoteC:\some\name
跨行的字符串多使用三引号,即三个单引号或者三个双引号:
>>> print """\... Usage: thingy [OPTIONS]... -h Display this usage message... -H hostname Hostname to connect to... """Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to >>> print """... Usage: thingy [OPTIONS]... -h Display this usage message... -H hostname Hostname to connect to... """ Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to
注意第一个三引号后面有反斜杠,就不会输出第一个换行符。末尾的反斜杠表示续行。
字符串可用+操作符连接,用*重复:
>>> 3 * 'un' + 'ium''unununium'
相邻字符串文本会自动连接,它只用于字符串文本,不能用于字符串表达式和变量(需要使用加号)等:
>>> 'Py' 'thon''Python'>>> prefix 'thon File "<stdin>", line 1 prefix 'thon ^SyntaxError: EOL while scanning string literal>>> ('un' * 3) 'ium' File "<stdin>", line 1 ('un' * 3) 'ium' ^SyntaxError: invalid syntax>>> prefix + 'thon''Python'# 在拆分长字符串时很有用。>>> text = ('Put several strings within parentheses '... 'to have them joined together.')>>> text'Put several strings within parentheses to have them joined together.'
字符串下标又称索引和C类似 ,第一个字符索引为 0 。没有独立的字符类型,字符就是长度为 1 的字符串,也可以使用负数,-1表示倒数第一个,-2表示倒数第二个,以此类推。不存在的下标会报IndexError。
>>> word = 'Python'>>> word[0] # character in position 0'P'>>> word[5] # character in position 5'n'>>> word[-1] # last character'n'>>> word[-2] # second-last character'o'>>> word[-6]'P'>>> word[-16]Traceback (most recent call last): File "<stdin>", line 1, in <module>IndexError: string index out of range>>> word[16]Traceback (most recent call last): File "<stdin>", line 1, in <module>IndexError: string index out of range
字符串支持切片:由两个索引,中间是冒号。第一个索引表示起点,包含该元素,默认为0;第2个索引表示终点,不包含该元素,默认为字符串末尾。s[:i] + s[i:]等同于s。
>>> word[0:2] # characters from position 0 (included) to 2 (excluded)'Py'>>> word[2:5] # characters from position 2 (included) to 5 (excluded)'tho'>>> word[:2] + word[2:]'Python'>>> word[:4] + word[4:]'Python'>>> word[:2] # character from the beginning to position 2 (excluded)'Py'>>> word[4:] # characters from position 4 (included) to the end'on'>>> word[-2:] # characters from the second-last (included) to the end'on'
记住切片的工作方式:切片索引是在字符之间。左边第一个字符的索引为0,右界索引为字符串长度n 。例如:
+---+---+---+---+---+ | H | e | l | p | A | +---+---+---+---+---+ 0 1 2 3 4 5-5 -4 -3 -2 -1
第一行数字给出字符串正索引点值0...5 。第二行给出相应的负索引。切片是从 i 到 j 两个数值标示的边界之间的所有字符。
对于非负索引,如果两个索引都在边界内,切片长度就是两个索引之差。例如, word[1:3] 是 2 。
切片时,下标溢出不会报错。
>>> word[4:42]'on'>>> word[43:42]''
Python的字符串是不可变。向字符串文本的某一个索引赋值会引发错误:
>>> word[0] = 'J'Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: 'str' object does not support item assignment
通过联合(加号)可以简单高效的创建字符串。(注,jython中这种操作并不高效)。
>>> 'J' + word[1:]'Jython'>>> word[:2] + 'py''Pypy'
内置函数len()返回字符串长度:
>>> s = 'supercalifragilisticexpialidocious'>>> len(s)34
参考资料:
Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange https://docs.python.org/2/tutorial/introduction.html#strings
String Methods: https://docs.python.org/2/library/stdtypes.html#string-methods
String Formatting:https://docs.python.org/2/library/string.html#new-string-formatting
String Formatting Operations: https://docs.python.org/2/library/stdtypes.html#string-formatting
Unicode字符串
Python2.0增加了新的用来存储文本数据的类型:Unicode对象。它可以用于存储和操作Uounicode 数据(参见 http://www.unicode.org/),与现有的字符串兼容性良好,必要时能自动转换。
Unicode支持所有字符的表示,之前的ASCII只支持256个字符。更多Unicode相关的资料,参见:http://zh.wikipedia.org/wiki/Unicode。
创建Unicode字符串:
>>> u'Hello World !'u'Hello World !'
引号前的'u'表示Unicode 字符串,转义的方式可以创建其他字符:
>>> u'Hello\u0020World !'u'Hello World !'
转义序列\u0020表示插入编码为0x0020(空格)的Unicode 字符。
其他字符也会直接解释为对应的编码值。 许多西方国家使用的标准Latin-1编码的字符串和编码小于256的Unicode字符和在Unicode编码中的一样。
使用ur可以取消转义,r表示原始格式(raw)。
>>> ur'Hello\u0020World !'u'Hello World !'>>> ur'Hello\\u0020World !'u'Hello\\\\u0020World !'
如果你需要大量输入反斜杠(比如正则表达式),原始模式非常有用。
除了标准编码,Python还支持其他编码。
内置函数unicode()可以访问所有注册的Unicode编码(COders和DECoders),并支持Latin-1 、ASCII、UTF-8和UTF-16 之类的编码可以互相转换,后两个是变长编码。通常默认编码为 ASCII,此编码接受0到127 这个范围的编码,否则报错。Unicode字符串打印或写入到文件,或者使用str()转换时,使用默认编码进行转换操作。
encode()方法可以把Unicode字符串转换为特定编码的8bit字符串,参数为小写的编码名作为参数。
反之可以使用unicode()把其他编码转换为unicode。
>>> u"abc"u'abc'>>> str(u"abc")'abc'>>> u"äöü"u'\xe4\xf6\xfc'>>> str(u"äöü")Traceback (most recent call last): File "<stdin>", line 1, in <module>UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)>>> u"äöü".encode('utf-8')'\xc3\xa4\xc3\xb6\xc3\xbc'>>> unicode('\xc3\xa4\xc3\xb6\xc3\xbc', 'utf-8')u'\xe4\xf6\xfc'
列表
Python有一些复合数据类型,用于组合值。最常用的是 list(列表)),为中括号之间的逗号分隔的值。列表的元素可以是多种类型,但是通常是同一类型。
>>> squares = [1, 4, 9, 16, 25]>>> squares[1, 4, 9, 16, 25]
像字符串和其他序列类型,列表可支持切片和索引:
>>> squares[0] # indexing returns the item1>>> squares[-1]25>>> squares[-3:] # slicing returns a new list[9, 16, 25]
切片返回新的列表,下面操作返回列表a的浅拷贝:
>>> squares[:][1, 4, 9, 16, 25]
列表还支持连接:
>>> squares + [36, 49, 64, 81, 100][1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
字符串是不可改变的,列表是可变的。
>>> 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]
append()方法可以添加元素到尾部:
>>> cubes.append(216) # add the cube of 6>>> cubes.append(7 ** 3) # and the cube of 7>>> cubes[1, 8, 27, 64, 125, 216, 343]
也可以对切片赋值,此操作甚至可以改变列表的尺寸,或清空它:
>>> 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']>>> letters['a', 'b', 'C', 'D', 'E', 'f', 'g']>>> # now remove them>>> letters[2:5] = []>>> letters['a', 'b', 'f', 'g']>>> # clear the list by replacing all the elements with an empty list>>> letters[:] = []>>> letters[]
内置函数 len() 同样适用于列表:
>>> letters = ['a', 'b', 'c', 'd']>>> len(letters)4
支持嵌套列表(包含其它列表的列表),例如:
>>> 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'
- python 2.7 中文教程-3:非正式教程
- [Python教程]3.一份非正式的Python教程
- python教程之二-----一个Python的非正式介绍
- python twisted中文教程
- python 2.7 中文教程-1:简介
- python 2.7 中文教程-4:编程基础
- python 2.7 中文教程-5:数据结构
- CakePHP 中文教程3
- Swift中文教程3
- python 2.7 中文教程-2:开胃菜和解释器
- python 2.7 中文教程-6:模块和包
- 【Python】【教程】Python 教程
- 中文教程
- Python Twisted 中文教程
- Python Kivy 中文教程:安装(Windows)
- Python 2.7教程
- Python-OpenCV教程-3
- Python GTK+3 教程
- SpringMVC——Controller和视图之间数据的相互传递
- Android 的状态模式
- lua的metatable查找成员源码分析
- linux sed命令详解
- 如何让页面撑满整个浏览器屏幕
- python 2.7 中文教程-3:非正式教程
- 6月英语学习总结
- php实现qq第三方登录
- cocoa touch——UIPickerView
- LeetCode - 50. Pow(x, n)
- Eclipse调试JDK源代码~watch(监视)变量时报:<error(s)_during_the_evaluation>
- apache+tomcat+jk session共享配置
- 欢迎使用CSDN-markdown编辑器
- order by后面加常量是什么意思?