经过Java语法的学习和他人的经验,我决定使用官方文档来学习python,锻炼英文阅读能力的同时,也获取的是最准确的最有说服力的知识,在学习的过程中我会贴一些官方文档的原文便于查看理解,下面进入正题。
首先 + - * / 加减乘除没什么问题,使用除法时用‘/’时得到的是float类型的结果,使用‘//’是取整,使用‘%’是取余,此外使用‘**’是乘方,可以看代码:
>>> 2 ** 7 # 2 to the power of 7
128
>>> 17 / 3 # classic division returns a float
5.666666666666667
>>>
>>> 17 // 3 # floor division discards the fractional part
5
>>> 17 % 3 # the % operator returns the remainder of the division
2
变量没有赋值时使用会报错
>>> n # try to access an undefined variable
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined
上一个输出的值可以用'_'来使用
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06
>>> '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()可以省略引号,\n是换行但必须在print()里使用才有效,直接输出会被当作普通字符
>>> 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 line
First line.
Second line.
如果不想让\被理解成特殊字符,可以在字符串第一个引号前加一个r
>>> print('C:\some\name') # here \n means newline!
C:\some
ame
>>> print(r'C:\some\name') # note the r before the quote
C:\some\name
字符串常量可以展示在多行里,使用三个引号"""..."""或'''...'''
字符串可以用 + 来运算,用 * 来重复叠加
>>> # 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想让变量和常量相加,使用 + 来完成
>>> prefix + 'thon'
'Python'
字符串可以被索引化
>>> 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[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'
越界下标在截取子字符串时可以被优雅地使用
>>> word[4:42]
'on'
>>> word[42:]
''
Python字符串和Java中的String都是不可改变的,想要给字符串某一下标的字符赋值也是不允许的
>>> word[0] = 'J'
...
TypeError: 'str' object does not support item assignment
>>> word[2:] = 'py'
...
TypeError: 'str' object does not support item assignment
len()函数能返回字符串的长度
>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)
34
对于Lists和Strings有很多相似的地方,贴一下代码看一下
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
>>> squares[0] # indexing returns the item
1
>>> squares[-1]
25
>>> squares[-3:] # slicing returns a new list
[9, 16, 25]
>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
和字符串不一样的是List是可改变的
>>> cubes = [1, 8, 27, 65, 125] # something's wrong here
>>> 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]
部分改变也可以,甚至可以改变List的长度或直接清除
>>> 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
[]
嵌套也是可以的
>>> 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'
这些是最基本的语法,在使用中就会慢慢熟悉