Skip to content

Latest commit

 

History

History
773 lines (644 loc) · 24 KB

File metadata and controls

773 lines (644 loc) · 24 KB

Python教學

目錄

Python是什麼

如何學習Python

Python能做什麼

Python實用模組及功能

Python編碼解碼支援格式

Python文法結構

Python資料型態

Python運算子

Python內建函數

Python是什麼?

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

如何學習Python

到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能做什麼?

Python有很多別人寫好的模組和強大的內建模組,可以做很多事,有些模組跑很快因為他用C寫,在Python內運作。

Python能輕鬆開發應用程式、網路伺服器、客戶端; 訓練人工智慧、機器學習、數據分析; 編輯圖片、影片和其他檔案。

Python可以用PyScript在瀏覽器內運作,詳情見此(不可使用網路伺服器及客戶端,缺少_ssl):
https://pyscript.net/

Python實用模組及功能

網路:

讀取網頁、開網頁伺服器、發送封包、接收封包

客戶端、伺服器端、底層應用。
客戶端,爬蟲、嗅探使用,向伺服器寄出請求,並取得回應,像是網頁原始碼。
伺服器端,開伺服器等待用戶連接並做出相應行動。

客戶端、伺服器端、底層應用。
wrap socket with ssl.包裝ssl到socket上,如讓http變成https。

底層應用。
處理IP字符串

Http客戶端、伺服器端。

Ftp客戶端。

客戶端
爬蟲、抓包使用,向網路寄出請求,並取得回應,像是網頁原始碼。

客戶端
爬蟲、抓包使用,向網路寄出請求,並取得回應,像是網頁原始碼。

伺服器端
網頁伺服器,開伺服器等待用戶連接並做出相應行動。

伺服器端
網頁伺服器,伺服器框架,開伺服器等待用戶連接並做出相應行動。

伺服器端
網頁伺服器,伺服器框架,開伺服器等待用戶連接並做出相應行動。

實用工具
嗅探工具、封包收發測試、網路攻擊工具、偽裝IP工具、網域測試。

底層應用,字符串處理。
國際域名轉換。

客戶端
非同步網頁讀取,使用async。

客戶端
DNS掃描。

客戶端
非同步網頁讀取,使用gevent。

抓包處理

將抓到的封包、原始碼進行處理,通常搭配request模組使用。

控制瀏覽器

爬蟲的一種

編輯任何檔案

Python內建函數Open

音樂標頭編輯(MP3 ID3 Metadata)

照片、影片編輯,串流讀取

CSV編輯

JSON編輯

Excel編輯

WAV編輯

TOML讀取


人工智慧、大數據分析、機器學習

H5檔案讀取

陣列、數學與矩陣運算

資料圖形化

資料分析

人類語言分析(自然語言處理,人工智慧的分支,維基百科介紹)


時間

系統(系統資訊、程式資訊、檔案操作、標準輸入輸出...)

進程、線程

Async、Coroutine

壓縮編碼解碼

字元編碼識別

C函數使用

Python變數儲存

加密解密

快取工具

MIMETYPE處理

Google Api

更輕鬆地寫出class

顏色轉換(例如 rgb to hsl)

字符串處理、檢查、正規表達式

視覺化界面

打包成exe檔

運作Javascript

Windows Api(如windows檔案管理、控制Excel、訊息對話框)

加速Python的幾種方法

1. 改善程式結構

2. 將遞歸函數改成迴圈

3. 使用cpython或pypy

Python編碼解碼支援格式

Python文法結構

函數

def function_name(*args, **kargs): return value

詳細資料

迴圈

for i in iterObject: doSomething
while flag: doSomething

詳細資料

物件

class object_name(parent):
    pass

如果...做

if flag: #如果flag是True則做...
  doSomthing
elif flag1: #又或者如果flag1是True則做...
  doSomthing
else:  #又或者則做...
  doSomthing

嘗試...捕捉錯誤

try
  doSomthing
except Exception as e: #將預期到的錯誤視為e
  doSomthing
finally:  #到最後,無論如何都要做...
  doSomthing

刪除

del variable_name

Python資料型態

str -> string 字符串 "文字"

int -> integer 整數 1

list -> list 清單(類似陣列,但內容物型態不限) ["文字",1,...]

tuple -> tuple 類似list,但只讀不能更改 ("文字",1,...)

dict -> dictionary 字典(查表用) {"魚":fish,...}

float -> float 浮點數(小數) 1.111

bytes -> bytes 原始數據(只讀r) b'\xe6\x88\x91\xe6\x84\x9b\xe4\xbd\xa0'

bytearray -> bytearray 原始數據陣列(讀寫rw) bytearray(b'\xe6\x88\x91\xe6\x84\x9b\xe4\xbd\xa0')

bool -> boolean 布林值(True or False)

展開示例
>>> 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'>
>>>

Python運算子

假設x=2, y=7

Python算術運算子(Arithmetic Operators)

+ ⇨ 加 x+y=2+7=9

- ⇨ 減 x-y=2-7=-5

* ⇨ 乘 x*y=2*7=14

/ ⇨ 除 x/y=2/7=0.2857142857142857

// ⇨ 除完取整數(商) x//y=2//7=0

% ⇨ 除完取餘數 x%y=2%7=2

** ⇨ 次方 x**y=2**7=2*2*2*2*2*2*2=128

Python指定運算子(Assignment Operators)

運算子 示例 相當於 結果
= 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

Python比較運算子(關係運算子)(Comparison Operators)

== ⇨ x==2: True, x是2(x等於2)

!= ⇨ x!=2: False, x不是2(x不等於2)

>= ⇨ x>=2: True, x大於等於2

<= ⇨ x<=2: True, x小於等於2

> ⇨ x>2: False, x大於2

< ⇨ x<2: False, x小於2

Python邏輯運算子(Logical Operators)

and 兩邊條件都成立則返回True,否則False。 m = (1 and 0), m: False

or 兩邊條件至少一邊成立則返回True,否則False。 m = (1 or 0), m: True

not 不是、相反,True變False,False變True。 m = not (1 and 0), m: True

Python恆等運算子(Identity Operators)

is 完全相等

Python成員運算子(Membership Operators)

in 在什麼之中 m = ("1" in "hh1dd"), m: True

Python位元運算子(Bitwise Operators)(int only)

& ⇨ and 將數字轉為2進制並對每一位數進行and運算 10&2 ⇨ 1010 & 0010 = 0010 ⇨ 2

| ⇨ or 將數字轉為2進制並對每一位數進行or運算 10|2 ⇨ 1010 | 0010 = 1010 ⇨ 10

^ ⇨ xor 將數字轉為2進制並對每一位數進行xor運算(兩數只有一邊為True才返回True) 10^2 ⇨ 1010 ^ 0010 = 1000 ⇨ 8

~ ⇨ not 將數字轉為2進制並反轉再轉回數字(2的補數) ~10(正數) ⇨ ~1010 ⇨0、1互轉 0101 ⇨ 負數且2的補數 ⇨ 0101-1 ⇨0、1互轉 -0100 ⇨ -1011 ⇨ -11(負數轉正數則反向操作)

<< ⇨ 將數字轉為2進制並在右邊補數個0再轉回數字 10<<2 ⇨ 1010<<2 ⇨ 101000 ⇨ 40

>> ⇨ 將數字轉為2進制並在右邊減少數個位數再轉回數字 10>>2 ⇨ 1010>>2 ⇨ 10 ⇨ 2

Python內建函數

iterable: 可遍歷物件,arg: argument參數。

文字Unicode編碼處理

chr(int) ⇨ 將指定字元Unicode編碼轉為文字Unicode解說 https://www.readfog.com/a/1638084002220969984

ord(str) ⇨ 將指定文字轉為Unicode編碼

展開示例
>>> 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(arg) ⇨ 將變數轉為字符串型態

int(str, int base) ⇨ 將變數轉為整數型態,無條件捨去; 或是將十進位數字轉為其他數字

float(arg) ⇨ 將參數轉為float

list(iterable) ⇨ 將可遍歷變數轉為list型態

tuple(iterable) ⇨ 將可遍歷變數轉為tuple型態

set(iterable) ⇨ 將可遍歷變數轉為dict型態,或是建立字典

bytes(arg) ⇨ 將參數轉為bytes

bytearray(arg) ⇨ 將參數轉為bytearray

bool(arg) ⇨ 將參數轉為布林值有值或非為空為True,無值或空的或None為False

展開示例
>>> 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
>>>

其他

bin(int) ⇨ 將十進位數字轉為二進位

oct(int) ⇨ 將十進位數字轉為八進位

hex(int) ⇨ 將十進位數字轉為十六進位

open(str file_path, str mode, encoding=str encoding) ⇨ 開啟檔案(檔案路徑file_path, 模式mode, encoding=編碼方式)

print(*args) ⇨ 打印參數

len(arg) ⇨ 回傳參數長度

range(int start, int end, int 間距) ⇨ range(起始值, 終點值_不包含, 間隔)

enumerate(iterable, start=int) ⇨ 將可遍歷物件每個項目加上index

zip(*iterables, strict=False) ⇨ 將數個可遍歷物件壓縮成一個供遍歷使用

reversed(iterables) ⇨ 回傳可遍歷物件反轉

abs(int) ⇨ 回傳絕對值

round(int, ndigits=int) ⇨ 通常為四捨五入

pow(int, int) ⇨ 次方,pow(2, 5)=2**5=22222=32,pow(32,1/5)=2

max(*args) ⇨ 回傳最大值

min(*args) ⇨ 回傳最小值

id(arg) ⇨ 回傳變數id

iter(arg) ⇨ 將參數變成可遍歷物件回傳

next(iterable) ⇨ 回傳可遍歷物件的下個物件,若已到結尾則拋出錯誤

globals() ⇨ 回傳全域變數

locals() ⇨ 回傳區域變數

filter(function, iterable) ⇨ 將可遍歷物件iterable放進函數function內個別執行篩選得到一個新的list

map(function, iterable) ⇨ 將可遍歷物件iterable放進函數function內個別執行得到一個新的list

callable(arg) ⇨ 檢查指定參數arg是否可以呼叫調用

eval(str) ⇨ 將字符串視為程式碼執行並回傳值,不可賦值。

exec(str) ⇨ 將字符串視為程式碼執行並回傳是否成功。

dir(arg) ⇨ 取得變數、物件、模組所有可用方法。(dir())

type(arg) ⇨ 取得參數型態、構建新屬性類別。

help() ⇨ help info,取得模組資訊介紹

參考

其他各大網站