Python3_ Untitled _1
Here are some of the places I need to be aware of when I look at a document
Control address: https://docs.pythontab.com/python/python3.5/index.html
#几种运算
>>> 17/3 # Classic division returns a float
5.666666666666667
>>>
>>>//3 # Floor division discards the fractional part
5
>>> 3 # The operator returns the remainder of the division
2
>>> 5 * 3 + 2 # result * divisor + remainder
17
>>> 5 * * 2 # 5 squared
25
#变量在使用前必须 "Definition" (Assignment), otherwise an error occurs:
>>> # Try to access an undefined variable
... n
Traceback (most recent):
File "<stdin>", line 1, in <module>
Nameerror:name ' n ' is not defined
#交互模式中, the value of the most recent expression is assigned to the variable _
#round (x [, N]): x--numeric expression, n--shows the number of decimal digits, where x needs to be rounded, and the default value is 0.
>>> tax = 12.5/100
>>> Price = 100.50
>>> Price * Tax
12.5625
>>> Price + _
113.0625
>>> Round (_, 2)
113.06
#字符串
>>> word = ' Python '
>>> Word[0] # character in position 0
P
>>> Word[5] # character in position 5
N
>>> word[-0]#-0 is 0.
P
>>> 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[4:42] #尾端越界, select the actual length
' On '
>>> word[42:] #首端越界, empty
‘‘
#python的字符串是不可变的
>>> word[0] = ' J '
...
TypeError: ' str ' object does not support item assignment
>>> word[2:] = ' py '
...
TypeError: ' str ' object does not support item assignment
#列表的元素可以不为同一类型
>>> a = [4 ', ' 56 ']
>>> A
[1, 2, 3, ' 4 ', ' 56 ']
#列表可索引, Slice, ' + '
>>> squares[0] # indexing returns the item
1
>>> Squares[-1]
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]
>>> Cubes[3] = 64
>>> cubes
[1, 8, 27, 64, 125]
#列表切片可以赋值
>>> letters = [' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ']
>>> Letters[2:5] = []
>>> Letters
[' A ', ' B ', ' F ', ' G ']
#列表可嵌套
>>> a = [1,2,3,[1,2,3]]
>>> A
[1, 2, 3, [1, 2, 3]]
#让输出不换行
>>> 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,
Python3_ Untitled _1