[译]The Python Tutorial#3. An Informal Introduction to Python
来源:互联网 发布:网络视频如何下载 编辑:程序博客网 时间:2024/06/04 19:51
3. An Informal Introduction to Python
在以下示例中,输入和输出以提示符(>>>
和...
)的出现和消失来标注:如果想要重现示例,提示符出现时,必须输入提示符之后的所有内容。不以提示符开头的行是解释器的输出。需要注意的是示例中行内从属提示符意味着必须多输入一个空行,用来终止多行命令。
手册中的许多示例,包括以交互提示符输入的,都包含注释。Python的注释以#
字符开头,直至物理行尾结束。注释可以出现在行首,或者跟在空白符或代码后面,但是不能出现在字符串中。字符串中的#
字符仅仅是#
字符。由于注释是用来阐述代码的,并不被Python所解释,重写示例时可以忽略它们。
一些示例:
# this is the first commentspam = 1 # and this is the second comment # ... and now a third!text = "# This is not a comment because it's inside quotes."
3.1 Using Python as a Calculator
尝试一些简单的Python命令。启动解释器并等待主提示符>>>
出现。(不会等待太久)
3.1.1 Numbers
解释器可以作为简单计算器使用:在解释器中输入表达式,解释器会输出值。表达式语法很简单:操作符+, - * /
跟其他语言意义一样(如C跟Pascal);括号(()
)用于分组。例如:
>>> 2 + 24>>> 50 - 5*620>>> (50 - 5*6) / 45.0>>> 8 / 5 # division always returns a floating point number1.6
整数(如2, 4, 20
)类型是int
,带有小数的数(如5.0, 1.6
)类型是float。后面会有更多关于数字类型的介绍。
除(/
)总是返回浮点数。操作符//
是向下取整除法,返回一个整数结果(舍弃小数部分结果),操作符%
用于求余数:
>>> 17 / 3 # classic division returns a float5.666666666666667>>>>>> 17 // 3 # floor division discards the fractional part5>>> 17 % 3 # the % operator returns the remainder of the division2>>> 5 * 3 + 2 # result * divisor + remainder17
Python中可以使用操作符**
计算幂[1]:
>>> 5 ** 2 # 5 squared25>>> 2 ** 7 # 2 to the power of 7128
等号(=
)用于将值赋给变量。赋值操作之后,在下一个交互提示符之前不会有任何输出:
>>> width = 20>>> height = 5 * 9>>> width * height900
如果变量没有定义(没有被赋值),尝试使用会得到一个错误:
>>> n # try to access an undefined variableTraceback (most recent call last): File "<stdin>", line 1, in <module>NameError: name 'n' is not defined
Python提供了浮点数的完整支持;在复合类型操作中,整型会被提升为浮点型:
>>> 4 * 3.75 - 114.0
交互模式中,最后打印的表达式会被赋值给变量_
。意味着Python作为桌面计算器使用时,易于连续计算。例如:
>>> tax = 12.5 / 100>>> price = 100.50>>> price * tax12.5625>>> price + _113.0625>>> round(_, 2)113.06
该变量对于用户应该被当做只读的。不要对其显式赋值——否则将会创建同名的独立局部变量,该变量屏蔽内建变量的魔法效果。
除int
和float
之外,Python也支持其他数字类型,比如Decimal
和Fraction
。Python也有对复数(complex numbers)的内建支持,使用j
或者J
标识复数的虚部(如3+5j
)。
3.1.2 Strings
除了数字,Python也提供了多种表示字符串的方式。字符串可以使用单引号('...'
)或者双引号(“...”
)围起来,效果是一样的[2]。\
用于转义:
>>> 'spam eggs' # single quotes'spam eggs'>>> 'doesn\'t' # use \' to escape the single quote..."doesn't">>> "doesn't" # ...or use double quotes instead"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 # without print(), \n is included in the output'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
字符串可以使用操作符+
串联起来,使用操作符*
使其重复:
>>> # 3 times 'un', followed by 'ium'>>> 3 * 'un' + 'ium''unununium'
两个或者更多相邻的字符串字面常量会自动拼接起来:
>>> 'Py' 'thon''Python'
以上只对只有字符串字面常量有效,对变量或者表达式无效:
>>> prefix = 'Py'>>> prefix 'thon' # can't concatenate a variable and a string literal ...SyntaxError: invalid syntax>>> ('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.'
字符串支持索引操作,第一个字符串索引为0。Python中没有单独的字符类型;一个字符仅仅是长度为1的字符串:
>>> 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'
注意由于-0与0相同,所以负数索引从-1开始。
除了索引,字符串也支持切片。使用索引获得个别字符,使用切片获取字符子串:
>>> word[0:2] # characters from position 0 (included) to 2 (excluded)'Py'>>> word[2:5] # characters from position 2 (included) to 5 (excluded)'tho'
注意切片操作的返回值包含起始索引,不包含结束索引。使得s[:i] + s[:i]
总是等于s
:
>>> word[:2] + word[2:]'Python'>>> word[:4] + word[4:]'Python'
切片操作指定了非常有用的默认值;省略第一个索引默认为0,省略第二个索引默认当前切片字符串长度。
>>> 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的字符串最后一个字符的右边索引为n:
+---+---+---+---+---+---+ | P | y | t | h | o | n | +---+---+---+---+---+---+ 0 1 2 3 4 5 6-6 -5 -4 -3 -2 -1
第一行数据给出了字符串索引0…6的位置;第二行数字给出了其对应负索引。从i到j的切片包含边i和边j之间所有的字符。
对于非负索引,如果两个索引都在界限内,切片长度是索引之差。例如,word[1:3]
的长度为2.
使用超过界限的索引会抛出异常:
>>> word[42] # the word only has 6 charactersTraceback (most recent call last): File "<stdin>", line 1, in <module>IndexError: string index out of range
然而,切片操作时超出界限的切片索引会被优雅处理:
>>> word[4:42]'on'>>> word[42:]''
Python字符串不能被改变——他们是不可变的。因此,企图对字符串索引位置赋值会抛出异常:
>>> word[0] = 'J' ...TypeError: 'str' object does not support item assignment>>> word[2:] = 'py' ...TypeError: 'str' object does not support item assignment
如果需要不同的字符串,应该新建一个:
>>> 'J' + word[1:]'Jython'>>> word[:2] + 'py''Pypy'
built-in函数len()返回字符串长度:
>>> s = 'supercalifragilisticexpialidocious'>>> len(s)34
参见:
Text Sequence Type — str
字符串是序列的一种,支持序列所支持的一般操作。
String Methods
字符串支持大量基础转换和搜索的方法
Formatted string literals
字符串字面常量拥有嵌入式表达式
Format String Syntax
使用str.format()格式化字符串的相关信息
printf-style String Formatting
详细描述了使用%
格式化字符串的旧字符串格式化操作
3.1.3 Lists
Python支持许多用来组织其他值的复合数据类型。最通用的是列表,包含在中括号中,以逗号分隔的一列值(项)就是列表。列表可以包含不同类型的数据,但在使用时通常包含相同类型数据。
>>> squares = [1, 4, 9, 16, 25]>>> squares[1, 4, 9, 16, 25]
与字符串相似(与其他所有buit-in序列类型相似),列表支持索引和切片操作:
>>> squares[0] # indexing returns the item1>>> squares[-1]25>>> squares[-3:] # slicing returns a new list[9, 16, 25]
所有的切片操作返回包含请求元素的新列表。这意味着下列切片操作返回对列表的浅复制:
>>> 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[]
built-in函数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'
3.2 First Steps Towards Programming
当然,可以使用Python完成比2+2更加复杂的任务。例如,可以写一段生成斐波那契数列的代码:
>>> # Fibonacci series:... # the sum of two elements defines the next... a, b = 0, 1>>> while b < 10:... print(b)... a, b = b, a+b...112358
以上示例介绍了几个新特性:
- 第一行包含了多重赋值:变量
a
和b
同时得到值0和1。最后一行再次使用多重赋值,证明了在右边的所有表达式在赋值操作发生之前首先计算值。右边的表达式从左到右计算值。 - 只要条件(这里是
b < 10
)为true,while循环就会执行。Python中,与C相似,任何非零整数值都是true;零是false。条件也可以是字符串或者列表,事实上可以是任何序列;非零长度序列是true,空序列是false。示例中使用的测试是一个简单的比较。标准的比较操作符与C相同:< (小于),>(大于), ==(等于), <= (小于等于), >=(大于等于)以及 !=(不等于)。 - 缩进了循环体:缩进是Python聚合语句的方式。在交互模式中,必须输入tab或者几个空格来缩进每一行。实践中,可以使用文本编辑器来准备更加复杂的输入;所有好用的文本编辑器都提供了自动缩进功能。交互模式中输入复杂语句后,必须输入空行来指示输入完成(因为解释器无法判断输入的最后一行)。记住在同一基础块中的每一行语句必须缩进相同的数量。
- print()函数输出参数的值。不同于输出想要输出的表达式(就像上述计算器示例中那样),该函数可以处理多个参数,浮点数以及字符串。输出的字符串没有引号包围,多个参数之间会插入空格。因此可以使用其优雅地格式化输出,如下示例:
>>> i = 256*256>>> print('The value of i is', i)The value of i is 65536
关键字参数end可以用来避免输出中的换行,或者使用不同的字符串结束输出:
>>> a, b = 0, 1>>> while b < 1000:... print(b, end=',')... a, b = b, a+b...1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
Footnotes
[1] 由于**
比-
拥有更高的优先级,-3**2
会被解释为-(3**2)
并且结果是-9
。如要避免优先级问题而得到结果9
,可以使用(-3) ** 2
[2] 与其他语言不同,特殊字符如\n
在单引号('...'
)和双引号("..."
)意义相同。不同之处在于在单引号之中不需要转义双引号"
,但是需要转义单引号\'
,反之亦然。
- [译]The Python Tutorial#3. An Informal Introduction to Python
- The Python Tutorial 3.2-3 An Informal Introduction to Python(Python简介)
- The Python Tutorial 3——An Informal Introduction to Python
- Topic 3:An Informal Introduction to Python
- An Introduction to Python Lists
- An Introduction to Python Lists
- Intel SSE Tutorial : An Introduction to the SSE Instruction Set
- An Introduction to Interactive Programming in Python
- An Introduction to Machine Learning with Python
- An Introduction to Interactive Programming in Python - Week zero
- An Introduction to Interactive Programming in Python - Week one
- An Introduction to Interactive Programming in Python - Week two
- An Introduction to Interactive Programming in Python - Week three
- An Introduction to Interactive Programming in Python(week 6)
- An Introduction to Interactive Programming in Python(week 7)
- An Introduction to Interactive Programming in Python week 9
- The Python Tutorial - Modules
- Python document-The python tutorial
- 欢迎使用CSDN-markdown编辑器
- 三种常用构图方式
- mapreduce优化
- Python模块(套件)安装
- 小程序学习记录
- [译]The Python Tutorial#3. An Informal Introduction to Python
- PHP SPL 四种常用的数据结构
- 给 Android 开发者的 RxJava 详解
- 使用 vue-cli 可以快速创建 vue 项目
- 显示图像
- BZOJ 1901: Zju2112 Dynamic Rankings 树状数组套主席树
- kafka问题分析
- web页面的回流,认识与避免
- .net下时间与时间戳之间的转换