---
tags: python
---
# 05_自訂函數
* 除內建函數以外,也可以將常用的功能包成一個函數
### 計算矩形面積
```python
def rect(width, height):
'''
This function will return the area of an rectangel
'''
area = width * height
return area
area = rect(2, 3)
print(area) # 6
```
### 從指定區間中取得指定數量隨機數
```python
from random import choice
# 參數可以做預設值
def get_rand(start=1, end=100, amount=4):
'''
By default, this function will retrun four unique random numbers in a range from 1 to 100.
'''
nums = [n for n in range(start, end +1)]
result = []
for n in range(amount):
rand = choice(nums)
result.append(rand)
nums.remove(rand)
return result
# 不加入參數arguments的情況下,函數也可以執行
get_rand() # [38, 95, 51, 72] 0到100中取4個不重複數字
```
### 自訂一個parse FASTA format的函數
```python
'''
The header/identifier of a sequence in a FASTA file usually starts with a ">". Subsequent lines are the actual sequence which may be a dna, rna, or protein sequence.
'''
def fasta_parse(raw):
raw = raw.split()
name_list, seq_list = [], []
seq = ''
for line in raw:
if line.startswith('>'):
# 第一個迴圈seq為空字串,不進行append
if seq:
seq_list.append(seq)
seq = ''
name_list.append(line.strip('>'))
continue
seq += line
# 迴圈結束後的最後一組seq要再append進seq_list才完整
seq_list.append(seq)
seqs = list(zip(name_list, seq_list))
return seqs
raw = '''
>one
ATCC
TGAA
>two
CGAA
CCGG
>three
ATCG
GTTA
'''
seqs = fasta_parse(raw)
print(seqs)
# [('one', 'ATCCTGAA'), ('two', 'CGAACCGG'), ('three', 'ATCGGTTA')]
```
> Written with [StackEdit](https://stackedit.io/).
<!--stackedit_data:
eyJoaXN0b3J5IjpbODQyMTA2MjIwXX0=
-->