Python是個高階語言,使用直譯器。Python的優勢是簡單明瞭能輕鬆開發APP,劣勢是執行速度過慢。(直譯器是邊翻譯邊執行,如Javascript語言; 編譯器是編譯完後再執行,如C語言。)
Python官方網站,含下載、文檔、關於頁面:
https://www.python.org/
Python官方文檔
https://docs.python.org/3/
Python官方模組
https://docs.python.org/3/library/index.html
想下載Python和編輯Python檔案可看這裡
https://github.com/wayne931121/python_tutorial/blob/main/安裝Python.md
到W3school學習入門文法(右上角地球符號能用Google翻譯調整語言)
https://www.w3schools.com/python/
到Python官方文檔教學頁面學習入門文法(左上角能調整語言)
https://docs.python.org/3/tutorial/index.html
到Google Colab中運作Python,適用於未安裝Python的裝置
https://colab.research.google.com/
Python有很多別人寫好的模組和強大的內建模組,可以做很多事,有些模組跑很快因為他用C寫,在Python內運作。
Python能輕鬆開發應用程式、網路伺服器、客戶端; 訓練人工智慧、機器學習、數據分析; 編輯圖片、影片和其他檔案。
Python可以用PyScript在瀏覽器內運作,詳情見此(不可使用網路伺服器及客戶端,缺少_ssl):
https://pyscript.net/
讀取網頁、開網頁伺服器、發送封包、接收封包
客戶端、伺服器端、底層應用。
客戶端,爬蟲、嗅探使用,向伺服器寄出請求,並取得回應,像是網頁原始碼。
伺服器端,開伺服器等待用戶連接並做出相應行動。
客戶端、伺服器端、底層應用。
wrap socket with ssl.包裝ssl到socket上,如讓http變成https。
底層應用。
處理IP字符串
Http客戶端、伺服器端。
Ftp客戶端。
客戶端
爬蟲、抓包使用,向網路寄出請求,並取得回應,像是網頁原始碼。
客戶端
爬蟲、抓包使用,向網路寄出請求,並取得回應,像是網頁原始碼。
伺服器端
網頁伺服器,開伺服器等待用戶連接並做出相應行動。
伺服器端
網頁伺服器,伺服器框架,開伺服器等待用戶連接並做出相應行動。
伺服器端
網頁伺服器,伺服器框架,開伺服器等待用戶連接並做出相應行動。
實用工具
嗅探工具、封包收發測試、網路攻擊工具、偽裝IP工具、網域測試。
底層應用,字符串處理。
國際域名轉換。
客戶端
非同步網頁讀取,使用async。
客戶端
DNS掃描。
客戶端
非同步網頁讀取,使用gevent。
爬蟲的一種
人類語言分析(自然語言處理,人工智慧的分支,維基百科介紹)
def function_name(*args, **kargs): return valuefor i in iterObject: doSomething
while flag: doSomethingclass object_name(parent):
passif flag: #如果flag是True則做...
doSomthing
elif flag1: #又或者如果flag1是True則做...
doSomthing
else: #又或者則做...
doSomthingtry
doSomthing
except Exception as e: #將預期到的錯誤視為e
doSomthing
finally: #到最後,無論如何都要做...
doSomthingdel variable_name展開示例
>>> a = "text"
>>> type(a)
<class 'str'>
>>>
>>> a[0]
't'
>>> a[1]
'e'
>>> a[-1]
't'
>>> a[0]="Y"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>>
>>> a = list(a)
>>> a
['t', 'e', 'x', 't']
>>> a[0]
't'
>>> a[1]
'e'
>>> a[-1]
't'
>>> a[0:2]
['t', 'e']
>>> a[0]="Y"
>>> a
['Y', 'e', 'x', 't']
>>> a[0]
'Y'
>>> "".join(a)
'Yext'
>>> "HEY".join(a)
'YHEYeHEYxHEYt'
>>> ".".join(a)
'Y.e.x.t'
>>> a = "".join(a)
>>> a
'Yext'>>> a = 1
>>> type(1)
<class 'int'>
>>> a + 1
2
>>> a
1
>>> a = a+1
>>> a
2
>>> a += 10
>>> a
12
>>>>>################# Tuple #################
>>> (0,1,2)
(0, 1, 2)
>>> type((0,1,2))
<class 'tuple'>
>>> (0,1,2)[0]
0
>>> a = (0,1,2)
>>> a[0]
0
>>> a[0]=5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>>############### List ####################
>>> a = [0,1,2]
>>> a
[0, 1, 2]
>>> a[0]
0
>>> a[0]=5
>>> a
[5, 1, 2]
>>> type([0,1,2])
<class 'list'>
>>> type(a)
<class 'list'>
>>>>>> {"TT":1, 0:"OO", True: print}
{'TT': 1, 0: 'OO', True: <built-in function print>}
>>> type({"TT":1, 0:"OO", True: print})
<class 'dict'>
>>> {"TT":1, 0:"OO", True: print}[True]
<built-in function print>
>>> {"TT":1, 0:"OO", True: print}[True]("123")
123
>>> {"TT":1, 0:"OO", True: print}[0]
'OO'
>>> {"TT":1, 0:"OO", True: print}["TT"]
1>>> type(135.54)
<class 'float'>>>> bytes("YY牛Z", encoding="utf-8")
b'YY\xe7\x89\x9bZ'
>>> b"123"
b'123'
>>> type(b"123")
<class 'bytes'>>>> bytearray("YY牛Z", encoding="utf-8")
bytearray(b'YY\xe7\x89\x9bZ')
>>> bytearray(b'YY\xe7\x89\x9bZ')
bytearray(b'YY\xe7\x89\x9bZ')
>>> type(bytearray(b'YY\xe7\x89\x9bZ'))
<class 'bytearray'>>>> True
True
>>> False
False
>>> type(True)
<class 'bool'>
>>> a = False
>>> type(a)
<class 'bool'>
>>>| 運算子 | 示例 | 相當於 | 結果 |
|---|---|---|---|
| = | x=2 | x=2 | x=2 |
| += | x+=10 | x=x+10 | x=12 |
| -= | x-=10 | x=x-10 | x=-8 |
| *= | x*=10 | x=x*10 | x=20 |
| /= | x/=10 | x=x/10 | x=0.2 |
| //= | x//=10 | x=x//10 | x=0 |
| %= | x%=10 | x=x%10 | x=2 |
| **= | x**=10 | x=x**10 | x=1024 |
| &= | x&=10 | x=x&10 | x=2 |
| |= | x|=10 | x=x|10 | x=10 |
| ^= | x^=10 | x=x^10 | x=8 |
| >>= | x>>=10 | x=x>>10 | x=0 |
| <<= | x<<=10 | x=x<<10 | x=2048 |
~ ⇨ not 將數字轉為2進制並反轉再轉回數字(2的補數) ~10(正數) ⇨ ~1010 ⇨0、1互轉 0101 ⇨ 負數且2的補數 ⇨ 0101-1 ⇨0、1互轉 -0100 ⇨ -1011 ⇨ -11(負數轉正數則反向操作)
iterable: 可遍歷物件,arg: argument參數。
chr(int) ⇨ 將指定字元Unicode編碼轉為文字Unicode解說 https://www.readfog.com/a/1638084002220969984
展開示例
>>> ord("我")
25105
>>> chr(25105)
'我'
>>> bytes("我", encoding="utf-8")
b'\xe6\x88\x91'
>>> int("e6",16)
230
>>> int("88",16)
136
>>> int("91",16)
145
>>> bin(230)
'0b11100110'
>>> bin(136)
'0b10001000'
>>> bin(145)
'0b10010001'
>>> bin(230).split("1110")[1]
'0110'
>>> "10".join(bin(136).split("10")[1:])
'001000'
>>> "10".join(bin(145).split("10")[1:])
'010001'
>>> int("0110"+"001000"+"010001", 2)
25105
>>> chr(25105)
'我'
>>>展開示例
>>> str(123)
'123'
>>> str([1,2.3])
'[1, 2.3]'
>>> str(True)
'True'
>>> 1+"P"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> str(1)+"P"
'1P'>>> int("5")
5
>>> "5"+"5"
'55'
>>> int("5")+int("5")
10
>>> int("ffffff", 16)
16777215
>>> int("10000000000", 2)
1024
>>> int(True)
1
>>> int(False)
0
>>> int(0.5)
0
>>> int(1.5)
1>>> float(1)
1.0>>> list("YRTYAAAdd__5546}}")
['Y', 'R', 'T', 'Y', 'A', 'A', 'A', 'd', 'd', '_', '_', '5', '5', '4', '6', '}', '}']
>>> list("YRTYAAAdd__5546}}")[0]
'Y'
>>> list("YRTYAAAdd__5546}}")[1]
'R'
>>> list("YRTYAAAdd__5546}}")[-1]
'}'>>> tuple([1,2,3,4,5,6])
(1, 2, 3, 4, 5, 6)>>> set([1,2,3])
{1, 2, 3}>>> bytes("我", encoding="utf-8")
b'\xe6\x88\x91'
>>> bytes("我", encoding="utf-8")[0]=1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'bytes' object does not support item assignment
>>> bytearray("我", encoding="utf-8")[0]=1
>>> bytearray("我", encoding="utf-8")
bytearray(b'\xe6\x88\x91')
>>> a = bytearray(b'\xe6\x88\x91') #as same as: a=bytearray("我", encoding="utf-8")
>>> a
bytearray(b'\xe6\x88\x91')
>>> a[0]
230
>>> a[1]
136
>>> a[2]
145
>>> len(a)
3
>>> a[0]=1
>>> a
bytearray(b'\x01\x88\x91')
>>> a[0]
1>>> bool
<class 'bool'>
>>> bool(0)
False
>>> bool(1)
True
>>> bool(104516547452)
True
>>> bool(None)
False
>>> bool("")
False
>>> bool(b"")
False
>>> bool("fggfdhghh5643fcxg")
True
>>> bool("RRR")
True
>>> bool(bool)
True
>>>