# python簡介與基礎語法
## 什麼是 Python?
Python 是一種高階程式語言,強調程式碼的可讀性、簡潔的語法。
相比於 C++、Java,Python 能讓開發者用更短(簡易)的程式碼做到一樣的結果。
### Python 的特色有哪些?
✔ 好理解:Python 是一種解釋型語言,瞭解結構之後,非常好理解。
✔ 好偷懶:Python 可以輕鬆使用各式各樣的函式,短短的程式碼輕鬆搞定。
✔ 好維護:Python 的架構很明確,非常容易維護。
### Python 的缺點有哪些?
✔ 速度慢:Python 為了功能,犧牲效能。
✔ 強制縮排:自有的縮排規則,新手很容易忽略而導致錯誤。
[出處](https://www.blink.com.tw/board/post/78610/)
總而言之,python比C/C++、JAVA高階,更貼合自然語言,語法更為簡單
並且有極大量的函式庫,在寫任何功能都可以很方便的使用
## 環境設置
一樣我們先下載python本體,然後選一個IDE安裝
[本體安裝教學](https://medium.com/@ChunYeung/%E7%B5%A6%E8%87%AA%E5%AD%B8%E8%80%85%E7%9A%84python%E6%95%99%E5%AD%B8-1-%E5%A6%82%E4%BD%95%E5%AE%89%E8%A3%9Dpython-126f8ce2f967)
IDE:[Pycharm安裝教學](https://medium.com/@ChunYeung/%E7%B5%A6%E8%87%AA%E5%AD%B8%E8%80%85%E7%9A%84python%E6%95%99%E5%AD%B8-1-%E5%A6%82%E4%BD%95%E5%AE%89%E8%A3%9Dpython-126f8ce2f967)
不過課堂因素我們先使用[colab](https://colab.research.google.com/)
點進去有介紹
ps:我會看看要學校電腦的狀況再決定之後社課要用什麼
## 基礎語法
### 變數宣告
python中變數宣告時無須指定型態,直接付值即可
另外python也不需要分號
```python=
a=100
print(a)
```
如果想要知道該變數為何種型態
請使用
```python=
type()#變數放括號裡 另外 python的註解是由 # 字號當開頭的
```
```python=
a=100
print(type(a))
```
##### 練習
請試著建立 字串 與 浮點數
### list
list 在使用在使用上與C/C++中的array有許多相似之處
並且也是python中常常用來儲存連續性的資料
建立時以中括號將元素包起來,並且能將不同型態的資料放在一起
```python=
list_example=['hellow',123,'one two three']
```
而如果想知道list中有幾個元素請使用
```python=
len()#list放括號裡
```
#### 元素存取
而要存取其中的某一筆資料,直接在後面加上index即可
```python=
list_example=['hellow',123,'one two three']
print(list_example[0])
#輸出 hellow
```
另外如果index 為-1即為list中最後一個元素
```python=
list_example=['hellow',123,'one two three']
print(list_example[-1])
#輸出 one two three
```
##### 練習
試試看 在以下程式碼中間加一行使輸出為 hellow
```python=
list_example=['hellow',123,'one two three']
#加在這
print(list_example[0][0])
```
#### list 取範圍
list中還有一個特性就是能指定範圍
並且使用中括號[a:b]
即為index a~b 的部分
如
```python=
list_example=['0',1,'2',3,'4',5,'6']
print(list_example[0:4])
#輸出:['0', 1, '2', 3]
```
並且還有其他特定位置的用法
如:
(省略起始)
[:9]=[0:9]
會從頭開始
(省略結尾)
[1:]=[1:len(list)]
會到list的結尾
#### 新增元素
##### 1.append(object)
append 能將物體新增在list 的結尾
```python=
list_example=['0',1,'2',3]
list_example.append('4')
print(list_example)
#輸出['0', 1, '2', 3, '4']
```
##### 2.insert(int,object)
insert能將元素插入指定位置
insert(你要插入的index,你要插入的元素)
```python=
list_example=['0',1,'2',3]
list_example.insert(2,'4')
print(list_example)
#輸出:['0', 1, '4', '2', 3]
```
##### 3.extend(list)
extend 能將將一個list的內容接在另一個list上
```python=
list_1=[1,2,3]
list_2=[4,5,6]
list_1.extend(list_2)
print(list_1)
```
#### 移除元素
##### 1.remove(object)
remove 可以移除指定資料
```python=
list_example=['0',1,'2',3]
list_example.remove('0')
list_example.remove(1)
print(list_example)
```
##### 2.pop(int)
pop可以根據輸入的index位置來移除元素
如果空白的話則會移除最後一個
```python=
list_example=['0',1,'2',3]
list_example.pop(0)
print(list_example)
```
另外pop是能回傳直的,如
```python=
list_example=['0',1,'2',3]
return_pop=list_example.pop(0)
print(return_pop)
```
##### 練習
我想想...