# Python學習筆記
>數字大於2^63取餘會爆炸
## 基礎語法
### I/O
```python=
name=input("What is your name?")
print("my_name","Eric") #會以空白隔開 => my_name eric
print("a","b",sep="<->") #會以<->當作分隔 => a<->b
print("a","b",end=' ') #以空格當結尾,預設是換行 => a b
n, m = map(int, input().split(',', 2)) #輸入兩個用逗號隔開的整數
```
### Dictionary
大括號宣告,相當於C++的map
```python=
dic={'a':1,'b':2,'c':4}
print(dic['c']) #=>4
#print all keys
for i in dic:
print(i,end=' ')
#print all values
for i in dic.values():
print(i,end=' ')
```
### Set
大括號宣告,不會有重複元素,會自動排序,跟C++一樣不可用索引值取元素
```python=
s=set() #建立空集合
s={i for i in range(5)} #s={0,1,2,3,4}
s.add(5) #加入元素
s.remove(4) #刪除元素
print(s) #{0, 1, 2, 3, 5}
if 2 in s: #判斷2是否在set理 =>True
```
### List
中括號宣告
注意:在函式裡修改外面宣告的list時,本尊會被修改,也就是說函式呼叫時傳進去的便變數是list的話會是傳指標,但傳一般變數時是複製一個副本,無法更改到外面的本尊。
```python=
A=['a string',23,100.232,'o']
print(A[2]) #=>100.232
B=[[None for _ in range(10)] for _ in range(10)] #10x10 list
```
### tuple
小括號宣告,不能被修改
```python=
t=(1,2,3) #or t=1,2,3
print(t[1]) #=>2
```
### If else
記得冒號,「且」是and不是&&
```python=
a=5
if a<4:
print("a<4")
elif a>=4 and a<6:
print("a>=4 且 a<6")
else:
print("a>=6")
#=> a>=4 且 a<6
```
### Range
用來存區間整數
參數 : (起始,終點(不包括),間隔)
```python=
r=range(5,10) #5,6,7,8,9
print(r[4]) #=>9
r2 = range(5, 50, 5) #5,10,15,20
print(r2[3]) #=>20
```
### Loop
```python=
for i in range(2,10,3): #起始值,結束值,增加值
print(i,end=' ') #2 5 8
a=[3,1,2,3]
for i in a:
print(i,end=' ') #=>3 1 2 3
i=0
while(i<10):
print(i,end=' ')
i+=1
#=>0 1 2 3 4 5 6 7 8 9
```
### String
split可將字串拆成陣列,該分隔字元會被刪除,拆解後封裝成list
join可組合陣列,中間可輸入連接詞
```python=
s="hello world"
arr=s.split('o')
print(arr) #=>['hell', ' w', 'rld']
s2='---'.join(arr)
print(s2) #=>hell--- w---rld
```
### Others
```python=
a=[6,3,2,1,3,2,5]
print(len(a)) #7
print(max(a)) #6
print(a.count(3)) #3出現2次輸出2
a.insert(2,100) #在index=2的位置插入100
a.remove(2) #移除第一個2
a.reverse() #反轉
print(a) #[5, 2, 3, 1, 100, 3, 6]
b=[1,2,3,4,5]
del b[1:3] #刪除index1到index3前一個的所有元素 => b=[1, 4, 5]
a.append(b) #將b塞到a的最後面 => a=[5, 2, 3, 1, 100, 3, 6, [1, 4, 5]]
print(a[-1][2]) #-1代表最後一個元素, =>5
a.clear() #清空
c=[]
for i in range(10):
c.append(i)
print(c) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
d=[i for i in range(10)]
print(d) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#只取小於5的奇數
e=[i for i in d if i<=5 and i&1]
print(e) #[1, 3, 5]
```
### Class
__init__函式會在宣告此Class型別變數時自動執行初始化
__str__函式會在print時執行,返回一個字串當作print輸出的內容
```python=
class Student:
def __init__(self,id,seat):
self.id=id
self.seat=seat
def __str__(self):
return f'My ID is {self.id}, and my seat number is {self.seat}.'
def change_seat(self,newSeat):
self.seat=newSeat
student=Student("848392","3A2B")
student.change_seat("8A7B")
print(student) #My ID is 848392, and my seat number is 8A7B.
```
### Numpy
用於進階數學運算
```python=
import numpy as np
```
#### 基本宣告
Zeros及ones為陣列元素的初始值,且括號裡的數字是大小,不是元素
```python=
a=np.array([1,2,3]) # 一維
b=np.array([[1,2,3],[4,5,6]]) #二維
c=np.zeros(5) #[0,0,0,0,0]
d=np.zeros((2,3)) #2×3的陣列,初始化為0
e=np.ones((3,4,5)) #3×4×5的陣列,初始化為1
```
#### 加減乘除
numpy的加減乘除為同一個元素位置的運算
一般list則為整個list運算(只有+號)
```python=
a=np.array([1,2,3])
b=np.array([2,3,4])
print(a+b) #[3 5 7]
c=[1,2,3]
d=[2,3,4]
print(c+d) #[1, 2, 3, 2, 3, 4]
```
#### 矩陣相乘
```python=
a=np.array([[1,2],
[2,3]])
b=np.array([[3,4],
[4,5]])
print(a.dot(b)) #[[11 14]
# [18 23]]
```
#### 內積完相加
```python=
a = np.array([3, 5, 6])
b = np.array([2, 6, 1])
print(a @ b) #: 6+30+6=42
```
### 其他
```python=
a=[1,2,3,4,5]
b=a[-1] #最後一個元素
c=a[::-1] #反向讀取 [5,4,3,2,1]
d=a[3::-1] #從index=3開始反向讀取 [4,3,2,1]
```
## 常用指令
### 安裝函式庫
```
pip install [name]
```
### 查看python已安裝函式庫
```
pip list
```
### 安裝函式庫報錯
如果報錯顯示是在原碼中的語法錯誤,可以用以下方法解決:

#### Step1: 下載函式庫
首先到PyPI這個網站搜尋要下載的函示庫,進入頁面後點擊左側的Download files,把壓縮檔下載下來,接著解壓縮。
#### Step2: 更改錯誤
上面報錯內容為Setup.py檔案裡的40行少了一個括號,更改完後存檔。
#### Step3: 安裝
進入到該函式庫存放位置的跟目錄(預設為下載)並在路徑欄中輸入cmd進入終端,並用-e(從本機安裝)安裝該函式庫。
```
pip install -e [file name]
```
注意:這裡的file name是下載下來的資料夾名稱,不是該函式庫的名稱。
**接著就大功告成了!!!**