# 常用方法

## elif

等於其他語言中的「else if」

## 類似三元運算子

```python
flag = True if x.isClick() else False
```

{% code title="等同" %}

```python
if x.isClick():
  flag = True
else:
  flag = False
```

{% endcode %}

## \~ (波浪符號)

「反、NOT」之意。

{% code title="可處理「減去」資料" %}

```python
data = [ 'a', 'b' ]
data_dup = [ 'b' ]
data[~data_dup] # =[ 'a' ]
```

{% endcode %}

## either

### Union

Union\[X, Y] means either X or Y.

{% code title="EX" %}

```python
Union[str, None] #不是空值就是String
```

{% endcode %}

### Optional

```python
Optional[X] #等同 Union[X, None]
Optional[str]="AAA" #不填入時會是預設值"AAA"
```

## None

是空值等同null

### is None

判斷物件是否為「空」

```python
if o is None:
if o is not None:
```

{% hint style="warning" %}
類型為NoneType，但用isinstance會無法判斷，「空」的東西沒有實際上的類型，有點類似JS的undefined
{% endhint %}

## 可換行文字

```python
"""XXX""" #等同JS中`XXX`
```

{% hint style="info" %}
不指定給變數時可當「多行註解」使用
{% endhint %}

## len = length

```python
len(object)
```

## Type

```python
type(x) #x的類型
```

## isinstance

判斷物件類型，搭配type(o)使用

isinstance(o, type)

{% code title="EX" %}

```python
isinstance(row[i_row], datetime)
```

{% endcode %}

## 算數

```python
x // y	#X除以Y，只取整數解
x % y	#求X除以Y的餘數
x ** y	#X的Y次方
```

## lambda

用於宣告一個簡易函式，基本只有一行的處理，想多行需提出宣告為def。

方便使用於函數變數需要是簡易函式時。

{% code title="語法結構" %}

```python
lambda [參數]: [函式本體]
```

{% endcode %}

{% code title="EX" %}

```python
>>> sorted(('Justin', 'openhome', 'momor'), key = lambda name: name[-1]) 
['openhome', 'Justin', 'momor']
>>> max = lambda n1, n2: n1 if n1 > n2 else n2
>>> max(10, 5)
10
```

{% endcode %}

## 切片(索引值的使用)

```
seq[start:end:step]
```

seq為任意序列(list、tuple，也可以是string)

start = 取值起始位置，預設0。\
end = 取值結束位置(不包含)，預設seq長度。\
step = 切片資料列的步長(取值間格)，預設1。

```python
a = [1, 2, 3, 4, 5]

print(a[-1]) #倒數第一個
>>> 5

print(a[::-1]) #倒敘
>>> [5, 4, 3, 2, 1]
```

> 書.page69(圖示很清楚)

## linspace（分N等份）

```python
import numpy as np
print(np.linspace(1, 10, 10))
> [ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]

print(np.linspace(0.1, 1.0, 10))
> [0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ]
```

> 參考：<https://vimsky.com/zh-tw/examples/usage/numpy-linspace-python.html>

## 程式碼太長時...

如果程式碼太長寫成一行不便於閱讀，可以使用\對程式碼進行折行。

```python
is_leap = year % 4 == 0 and year % 100 != 0 or \
          year % 400 == 0
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dinoin.gitbook.io/shi-py-bu-shi-pi/chang-yong-fang-fa.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
