---
# System prepended metadata

title: Python - Day 1
tags: [Python]

---

# Python - Day 1
###### tags: `Python`
## 資料型態

* 數字 Number
    * int, float
    ```python=
    3456  #整數
    3.5 #float
    ```
* 字串 String
    * 任意的文字內容
    ```python=
    "test" #string
    ```
* 布林值 Bool
    * 表達正確(true)或錯誤(false)
    ```python=
    True #bool 
    False 
    ```
* 可變列表 list
    * 有順序、可變動的資料集合
    ```python=
    [3,4,5] #list(有順序)
    ["Hello" , "World"] #中括號加上用逗號隔開
    ```
* 固定列表 Tuple
    * 有順序、不可變動的資料集合
    ```python=
    (3,4,5) #tuple(有順序)
    ("Hello" , "World") #小括號加上用逗號隔開
    ```
* 集合 set
    * 無順序的資料集合
    ```python=
    {3,4,5} #set(無順序)
    {"Hello" , "World"} #大括號加上用逗號隔開
    ```
* 字典
    * 鍵值對(Key-Value Pair)的集合
    ```python=
    {"Apple" : "頻果" , "data" : "資料" } #dictionary
    #{ 鍵 ： 值 }的配對
    ```
* 變數
    * 可用來存放資料的自訂名稱
    ```python=
    #Variable
    data = 1 
    #變數名稱 = 資料
    ```
    
    
## 基本運算
### 四則運算
```python=
x = 3 + 6
print (x)

x = 3 - 6
print (x)

x = 3 * 6
print (x)

x = 3 / 6    #小數除法
print (x)

x = 3 // 6   #整數除法
print (x)

x = 2 ** 3   #次方
print (x)

x = 2 ** 0.5 #根號
print (x)

x = 7 % 2    #取餘數
print (x)

```

### 字串
1. 內部字元編號由0開始
2. 字串基本運算
```python=
s = "Hello"
print ( s )

s = 'Jimmy'
print ( s )

s = "hell\'0\'"
print ( s )

s = "hell0" + "World" #字串串接
print ( s )

s = "hell0" "World"   #字串串接
print ( s )

s = "Hello World\n"   #跳行
print ( s )

s = '''Hello 

World'''  #跳行(三個單引號 或 三個雙引號)(用來寫多行程式)
print ( s )

s = r"Raw string. \n\r\b as u see, escape characters don't work."
print ( s ) # output: Raw string. \n\r\b as u see, escape characters don't work.
# it's useful when using regular expression

s = u"\u00dcnic\u00f6de" # unicode
print ( s ) # output: Ünicöde

s = "Hello" * 3 #產生三個Hello
print ( s ) 
```
3. 字串切片
```python=
# syntax:
# str[start_pos:end_pos:step]

s = "Hello" 
print ( s[1:4] ) 
#冒號前是切分開頭
#冒號後是切分結尾(不包含該數字)

s = "Hello" 
print ( s[:4] ) 
#冒號前沒有給就是從0(第一個字元開始)
#冒號後是切分結尾(不包含該數字)

s = r"Arcane"
print ( s[1:-1] ) # output: rcan
# the negative number means "count from the end"

s = r" E##M##T "
print ( s[1:-1:3] ) # output: EMT
# it's equal to the c code: for(int i = 1; i <= 7; i+=3) putchar(s[i])

s = r"123456789"
print ( s[::-1] ) # output: 987654321

# note that if the step < 0, the default value of start_pos is len(s)-1
```

### 有序列表 - List
1. 資料可變
2. 內部索引值由0開始

```python=
grades = [12, 60, 15, 70, 90 ]
print ( grades )
print ( grades[0] )

grades[0] = 55 #更改列表中的資料
print ( grades )

print ( grades[1:4] ) #取得特定資料
#冒號前是取值開頭(第一個字元開始)
#冒號後是取值結尾(不包含該數字)

grades[1:4] = [] #刪除資料(變空白)
#冒號前是取值開頭(第一個字元開始)
#冒號後是取值結尾(不包含該數字)
print ( grades ) 

grades = grades + [12, 33] #列表串接
print ( grades ) 

print ( len (grades) ) #取得列表長度

data = [[2,0],[3,0]]    #巢狀列表
print ( data[0] )       #[2,0]
print ( data[1][0] )    #[3]
data [0][0:1] = [5,5,5] #列表
print ( data[0] )       #[5,5,5,0]
```
### 有序列表 - Truple
1. 資料不可變
2. 操作與list類似

```python=
data = (3,4,5)    #固定列表(小括號)
print ( data )    
data [0] = 5      #錯誤tuple不可更動
print ( data )    #[5,5,5,0]
```

### 集合 set
1. 無順序之分
2. 有 (in) / (not in)語法
```python=
s1 = { 3, 4, 5 }
print ( 3 in s1 )  #in 判斷字元是否在s1中
print ( 10 in s1 )
print ( 10 not in s1 )
```
3. 集合運算
```python=
s1 = {3, 4, 5}
s2 = {4, 5, 6, 7}
s3 = s1 & s2 #&(交集)
print ( s3 )
s3 = s1 | s2 #|(聯集)重複元素不重複放
print ( s3 )
s3 = s1 - s2 #-(差集)對集合做減法
print ( s3 )
s3 = s1 ^ s2 #^(反交集)取不重疊的元素
print ( s3 )
```
4. 字串拆解成集合
```python=
s = set( "Hello" )
print ( "H" in s )
```

### 字典
1. 鍵(key)與值(value)的組合
```python=
dic = { "apple" : "蘋果" , "bug" : "蟲"}
```
2. 可修改
```python=
dic = { "apple" : "蘋果" , "bug" : "蟲"}
dic [ "apple" ] = "小蘋果"  #修改「值」
print (dic["apple"]) 
```
3. (in/not in) operation
```python=
print ( "test" in dic )     #in / not in operation
print ( "test" not in dic )
```
4. 刪除(del)操作 (以key做標籤)
```python=
print ( dic )
del dic [ "apple" ] #刪除 ( 以key做標籤 )
print ( dic )
```
5. 以list建置字典 (for ... in 為固定語法)
```python=
dic = {x:x*2 for x in [3,4,5]} #(for ... in 為固定語法)
print ( dic )
```