###### tags: `Python` `code`
# Python
<style>
table,
thead,
tbody {
width: 100%;
word-break: normal;
}
table th:nth-child(1),
table td:nth-child(1) {
width: 25%;
}
table th:nth-child(3),
table td:nth-child(3) {
width: 20%;
}
.table + table th,
.table + table td {
width: initial;
}
.table + table td:not(:nth-child(1)) {
font-size: .875em;
}
.table + table th,
.table + table td:nth-child(1) {
text-align: center;
}
h2 {
background: linear-gradient(to bottom, transparent 0%,transparent 50%,#fff000 50%,#fff000 100%);
width: fit-content;
font-weight: 700 !important;
border: none !important;
}
.summary h2 {
background: transparent !important;
width: 100%;
border-top: 2px solid black !important;
border-bottom: 1px solid #ccc !important;
}
</style>
## 環境安裝
基本環境
[Python官網](https://www.python.org/)
>安裝時切記打開環境變數
編輯器推薦VScode
[VScode官網](https://code.visualstudio.com/)
若要將開發環境分割可用anaconda
[Anaconda官網](https://anaconda.org/)
---
## 基本輸入輸出
輸出(印出來)
```python=
print('hello')
```
>輸出:hello
字串連結
```pyhton=
print('he'+'llo')
```
>輸出:hello
print跳脫符號為 \'\\\'
| 符號 | 呈現效果 |
| ---- | --------------------- |
| \\' | 單引號' |
| \\" | 雙引號" |
| \\n | 換行 |
| \\t | 定位空格一個tab的空格 |
| \\ | 反斜線\ |
讀取輸入
```python=
i=input('輸入區:')
print(i)
```
>輸出:輸入區:
>輸入:HI
>輸出:HI
註解
```python=
#我是註解
# 按Ctrl+/可以快速註解
```
---
## 資料類型
字串
```python=
#str
s="字串"
```
整數
```python=
#int
i=520
```
浮點數
```python=
#float
f=0.03
```
布林
```python=
#bool
b=True
```
整數
```python=
int
```
查看資料類型
```python=
i=0
print(type(i))
```
>輸出:int
---
## 運算子
### 計算運算子
加
```python=
print(3+4)
```
>輸出:5
減
```python=
print(5-2)
```
>輸出:3
乘
```python=
print(4*8)
```
>輸出:32
除(浮點數)
```python=
print(9/2)
```
>輸出:4.5
除(結果為整數)
```python=
print(7//3)
```
>輸出:2
餘數
```python=
print(7%5)
```
>輸出:2
次方
```python=
print(2**10)
```
>輸出:1024
開根號
```python=
print(121**0.5)
```
>輸出:11
---
### 比較運算子
| 運算子 | 說明 | 範例 |
| ------ | -------- | ------------------------ |
| > | 大於 | x>y(判斷x是否大於y) |
| < | 小於 | x<y(判斷x是否小於y) |
| >= | 大於等於 | x>=y(判斷x是否大於等於y) |
| <= | 小於等於 | x<=y(判斷x是否小於等於y) |
| == | 等於 | x==y(判斷x是否等於y) |
| != | 不等於 | x!=y(判斷x是否不等於y) |
```python=
print(120>32.5)
```
>輸出:True
```python=
print("Cat"<"cat")
#大寫C編碼為67小寫c編碼為99
```
>輸出:True
---
### 邏輯運算子
===
## 字串處理
重複印出多個相同字串
```python=
print('hello'*3)
```
>輸出:hellohellohello
print結尾不換行(end='' 引號中間為字串結尾)
```python=
print('he',end='')
print('llo')
```
>輸出:hello

python中沒有字元只有字串,同時字串也是一個陣列(如上圖)
```python=
p='python'
```
印出第三個字
```python=
print(p[2])
```
>輸出:t
印出第2~4個字
```python=
print(p[1:4])
```
>輸出:yth
印出開頭到第3個字
```python=
print(p[:2])
```
>輸出:pyt
印出第4個字到結尾
```python=
print(p[3:])
```
>輸出:hon
印出最後一個字
```python=
print(p[-1])
```
>輸出:n
---
### 字串相關函數
回傳ASCII
```python=
ord('W')
```
>輸出:87
印出ASCII對應文字
```python=
s=ord(87)
print(s)
```
>輸出:W
顯示字串長度
```python=
l=len('python')
print(l)
```
>輸出:6
---
### 字串類別提供的函式(domename)
#### 字串分割組合
字串分割 split
s.split('分割用字元')
```python=
s1='A B C'
print(s1)
s2=s1.split(' ')
print(s2)
print(type(s2))
```
>輸出:A B C
>輸出:[A,B,C]
>輸出:list
字串連接 join
```python=
s1=''.join([a,p,p,l,e])
print(s1)
s2='+'.join([A,B,C])
print(s2)
```
>輸出:apple
>輸出:A+B+C
#### 大小寫轉換
```python=
s='Hello'
```
全改大寫
```python=
s1=s.upper()
print(s1)
```
>輸出:HELLO
全改小寫
```python=
s2=s.lower()
print(s2)
```
>輸出:hello
#### 字串檢測函數
此類回傳值皆為BOOL
s為字串
| 函式 | 說明 |
| ---------------- | ------------------ |
| s.isupper() | 是否全為大寫 |
| s.islower() | 是否全為小寫 |
| s.startswith(s1) | 開頭是否為s1 |
| s.endswith(s2) | 開頭是否為s2 |
| s.istitle() | 開頭是否為大寫 |
| s.isalpha | 是否全為英文字母 |
| s.isdigit | 是否全為數字 |
| s.isalnum | 是否全為英文或數字 |
| s.isspace | 是否全為空格 |
#### 字串搜尋函式
| 函式 | 說明 |
| ----------- | ---------------------- |
| s.count(a) | a在s字串中出現次數 |
| s.find() | a在s字串中首次出現位置 |
| s.rfind(s1) | a在s字串中最後出現位置 |
#### 字串編修函式
| 函式 | 說明 |
| ------------------ | -------------------------------------------------------- |
| s.replace(old,new) | 將old換成now |
| s.lstrip(ABC) | 從左側刪除字元執到不是指定字元為止(未指定則移除空白字元) |
| s.rstrip(ABC) | 同上但是改為右側 |
| s.strip(ABC) | 同上但是改為兩側 |
| s.center(n) | 致中(n為欄位數量) |
| s.ljust(n) | 靠左(n為欄位數量) |
| s.rjust(n) | 靠右(n為欄位數量) |
| s.zfill(n) | 靠右其餘補0(n為欄位數量) |
---
## 容器類資料
### list
list內部需為同一類變數
建立list
```python=
lis=[]
l1=[7,4,1,4]
print(lis)
print(l1)
```
>輸出:[]
>輸出:[7,4,1,4]
轉list
```python=
list()
```
輸出串列數字
完整版
range(起始數值,邊界數值(最終數值加1),遞增間隔)
簡寫
預設初始值為0 遞增間隔為1
range(起始數值,邊界數值(最終數值加1))
range(邊界數值(最終數值加1))
```python=
list(range(0,10,2))
```
>輸出:[0, 2, 4, 6, 8]
| 函式 | 說明 |
| -------- | ---------- |
| len(lst) | 串列長度 |
| max(lst) | 串列最大值 |
| min(lst) | 串列最小值 |
| (lst) | 串列總和 |
### list提供的函式(domename)
| 函式 | 說明 |
| ----------------------- | ------------------------------------------ |
| lis.addend(obj) | 在最後添加新物建obj |
| lis.clear() | 清除lis內容 |
| lis.count(obj) | 計算obj在lis出現的次數 |
| lis.extend(obj) | 將obj的元素依序添加到lis最後方 |
| lis.index(index) | index的索引號 |
| lis.insert(obj,index) | obj插入index號的位置 |
| lis.pop(index) | 回傳並移除index號的元素(預設最後一個) |
| lis.remove(index) | 移除index號的元素(預設最後一個) |
| lis.remove(obj) | 移除lis中的obj(前面第一個) |
| lis.reverse() | 將lis反向排列 |
| lis.sort(reverse=False) | 將lis由小到大排序(reverse=True 則由大到小) |
---
### tuple
tuple一旦建立則無法修改(可重新宣告)
tuple內部需不須為同一類變數
建立一個tuple
```python=
t=(6,8,'Mon')
print(t)
```
>輸出:(6,8,'Mon')
轉tuple
```python=
tuple()
```
| 函式 | 說明 |
| ---------------- | ------------------- |
| tpl.conut(value) | 回傳對應value的個數 |
| tpl.index(value) | 回傳對應value的索引 |
### set
set是一個沒有索引的集合內容不重複
建立一個set
```python=
s=(6,8,'Mon')
print(t)
```
>輸出:(6,8,'Mon')
轉set
```python=
set()
```
| 函式 | 說明 |
| --------------------------- | --------------------------------- |
| s1.add(x) | 將x加入集合 |
| s1.clear() | 清除s1所有內容 |
| s1.defference(s2) | 回傳存在s1不在s2的集合(差及s1-s2) |
| s1.symmetric_defference(s2) | 回傳s1 s2共有元素(差及) |
| s1.union(s2) | 回傳s1+s2的所有元素(聯集) |
| s1.discord(x) | 移除s1內的x(沒x則部動作) |
| s1.isdisjoint(s2) | 判斷s1和s2是否沒有相同的玩素 |
| s1.issubset(s2) | 判斷s1是否為s2的子集合 |
| s1.issuperset(s2) | 判斷s1是否為s2的父集合 |
| s1.pop() | 從s1隨機移除一個元素並回傳 |
| s1.remove(x) | 從s1移除x不存在則回傳錯誤訊息 |
| s1.remove(x) | 從s1移除x不存在則回傳錯誤訊息 |
### dict
dict是一個類似字典類的容器
建立一個dict
```python=
d={'A':33,'B':38}
print(d)
```
>輸出:{'A':33,'B':38}
| 函式 | 說明 |
| -------------------------- | ------------------------------------------------------------------ |
| d1.clear() | 清除d1內所有元素 |
| d1.get(key,default) | 回傳key對應值 若沒有key則回傳default 若沒填default則回傳None |
| d1.tiem() | 回傳所有物件以及數值以[(物件,數值),(物件,數值)]的方式顯示 |
| d1.keys() | 回傳所有物件 |
| d1.values() | 回傳所有數值 |
| d1.setdefault(key,default) | 回傳key對應值 若沒有key則將{key,default}添加到字典並回傳default |
| d1.updeta(d2) | 把d2字典內的內容更新進d1 |
| d1.pop(key,default) | 移除並回傳key 若沒有key則回傳default 若default未填寫則顯示錯誤訊息 |
| d1.popiton() | 隨機回傳一對元素 若為空字典則產生錯誤訊息 |
---
## if(判斷式)

判斷正負
```python=
num=int(input('輸入一個數字:'))
if(num>0):
print('你輸入的是正數')
elif(num<0):
print("你輸入的是負數")
else:
print("你輸入的是0")
```
>輸入:9
>>輸出:你輸入的是正數
>輸入:-3
>>輸出:你輸入的是負數
>輸入:0
>>輸出:你輸入的是0
---
## 迴圈
### for
語法
```python=
for 變數 in 可迭代物件:
敘述A
```
常見
```python=
for i in range(0,3):
print(i)
```
>輸出:0
>輸出:1
>輸出:2
```python=
for i in 'dog':
print(i)
```
>輸出:d
>輸出:o
>輸出:g
```python=
for i in [41,34,67]:
print(i)
```
>輸出:41
>輸出:34
>輸出:67
### while
```
n=3
while(n>0):
print(n)
n-=1
```
>輸出:3
>輸出:2
>輸出:1
### 迴圈特殊功能
`break`
跳出整個迴圈
`continue`
結束此次迴圈
`pass`
無內容時需使用(否則報錯)
---
<!-- ## 串列生成式
將一堆程式壓縮成一行
## 函式 -->