来源:官方文档:https://docs.python.org/3.6/tutorial/index.html
都是拷下来自己看的(小白上路的笔记),官方文档更详细...
1,数字:解释器充当一个简单的计算器,您可以在其上键入表达式,它将写入值。
1.1 加减乘除没什么好讲的,贴点示例看看就行
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5 # division always returns a floating point number
1.6
有一个不一样的:// 双斜杠可以去掉余数
(/
)总是返回一个浮点数。要进行分区并获得整数结果(丢弃任何小数结果),您可以使用//
运算符; 计算你可以使用的余数%
:
>>> 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
>>> 5 * 3 + 2 # result * divisor + remainder
17
还有这个:** 两个星号表示求几次方
>>> 5 ** 2 # 5 squared
25
>>> 2 ** 7 # 2 to the power of 7
128
2.字符串
2.1 Python还可以操作字符串,这可以通过多种方式表达。它们可以用单引号('...'
)或双引号("..."
)括起来,结果相同, \
可用于转义引号
>>> '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," they said.'
'"Yes," they said.'
>>> "\"Yes,\" they said."
'"Yes," they said.'
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
2.2 print()打印没什么讲的,如果让反斜杠正常打印,可以通过在第一个引号之前添加原始字符串来使用原始字符串 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
2.3字符串文字可以跨越多行。一种方法是使用三引号: """..."""
或'''...'''
。行尾自动包含在字符串中,但可以通过\
在行尾添加a来防止这种情况(差不多就是解决回车就换行提交的尴尬)
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
2.4 字符串可以做乘法,差不多就是这个意思
>>> # 3 times 'un', followed by 'ium'
>>> 3 * 'un' + 'ium'
'unununium'
2.5 两个或多个彼此相邻的字符串文字(即引号之间的字符串)会自动连接(神奇)。用 + 也可以连接,后面就不讲了。
>>> 'Py' 'thon'
'Python'
2.6 想要断开长字符串时,此功能特别有用:(他说很有用)
>>> text = ('Put several strings within parentheses '
... 'to have them joined together.')
>>> text
'Put several strings within parentheses to have them joined together.'
2.7 字符串可以被索引(下标) ,从0开始。
>>> 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] # 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[42] # the word only has 6 characters
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
Python字符串无法更改 - 它们是不可变的。因此,分配给字符串中的索引位置会导致错误:
>>> 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
3.列表,数组(好像没区分开)
>>> 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]
>>> 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'
如果你看到这里,我会告诉你原来复制粘贴原来还挺累人的....