# 函數-進階用法 Advance Function ## 利用 * 拆開參數 ```python= def sum(a,b): return a+b print(sum(*[1,2])) # 利用 * 把 1跟2拆開放進函數 ``` ## 利用 * 寫不固定參數個數的函數 ```python= def print_all_number(*numbers): for n in numbers: print(n) print_all_number(1,2,3,4,5,6,7,8) ``` 混搭時,有星號的要在後面。 ## 利用 ** 拆參數進函數 ```python= def print_profile(name, number, score): print(f'Your name is: {name}.') print(f'Your number is: {number}.') print(f'Your score is: {score}.') kyle = {'name':'Kyle', 'number':40, 'score':99} print_profile(**kyle) ``` 注意,函數參數要跟dictionary相同。 ## 利用 ** 把參數寫進字典 ```python= def print_profile(**userInfo): print('Your name is:', userInfo.get('name', 'unknown')) print('Your number is:', userInfo.get('number', '阿摘')) print('Your score is:', userInfo.get('score', '未輸入')) # .get('', '這邊是如果找不到值') print_profile(name='Kyle', score=99) # Your name is: Kyle # Your number is: 阿摘 # Your score is: 99 ```