python 3.6學習--基礎1:數字,字符串,數組,列表的基本使用

來源:官方文檔: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'

 如果你看到這裏,我會告訴你原來複制粘貼原來還挺累人的....

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章