Python function 函式用法與範例

本篇介紹 Python function 函式用法與範例,function 函式要搭配 def 關鍵字來一起使用,使用 function 函式的目的是能將重複的程式邏輯包裝起來,提高程式碼的重複利用性,另一種比較少人知道的提高程式閱讀性,在大型專案裡會將複雜的邏輯包裝成函式,換成一個易懂的函式名稱,這樣即使不了解實做細節也大概知道這函式是要作什麼事,以下將介紹 Python 函式的用法與教學。

以下的 Python function 函式用法範例分為這幾部份介紹,

  • 最基本的函式(function)的用法,無傳入參數
  • 帶入一個參數進函式裡
  • 帶入多個參數進函式裡
  • 參數預設值
  • 函式沒有回傳值
  • 函式回傳 str 字串
  • 函式回傳 list 串列
  • 函式回傳 dict 字典
  • 函式回傳 tuple 元組
  • 函式回傳 set 集合

那就開始學習 Python function 函式的用法吧!

最基本的函式(function)的用法,無傳入參數

Python 使用 function 函式要在前面寫 def 關鍵字,表示接下是函式,假設我要寫一個 myprint 函式裡面什麼事也不做,那程式會長這樣,裡面什麼東西都沒有的話要加上一個 pass 語句,

1
2
def myprint():
pass

那我要在 myprint 函式印出 hello world 話,沒有帶入參數也沒有回傳值,範例如下,

1
2
def myprint():
print("hello world")

帶入一個參數進函式裡

那我要在 myprint 帶入一個 str 類型的參數,並且用 print 把改變數給印出來,

1
2
3
4
def myprint(s):
print(s)

myprint("hello world")

程式輸出如下,

1
hello world

帶入多個參數進函式裡

那我改用其他變數類型呢?用兩個整數好了!多個參數之間是使用 , 逗號隔開,那我們試著寫一個回傳兩數最大值的 max 函式,回傳數值要用 return 這語法。

1
2
3
4
5
def max(a, b):
return a if a > b else b

m = max(3, 5)
print(m)

程式輸出如下,

1
5

參數預設值

假設我有一個函式叫 add 可以作兩數相加的事情,但如果今天我想要只傳入一個參數沒有第二個參數時,第二個自動預設值是 10,就可以使用參數預設值這個功能,用法範例如下,

1
2
3
4
5
6
def add(a, b = 10):
return a + b

print(add(5,3))
print(add(5))
print(add(3))

結果如下,

1
2
3
8
15
13

函式沒有回傳值

在 Python 中如果函式沒有回傳值,Python 直譯器會自動回傳 return None,Python 的 None 相當於 C 語言中的 NULL,我們來看看下列範例沒有回傳值的時候,把它印出來看看,

1
2
3
4
5
def myprint():
print("hello world")

return_value = myprint()
print(return_value)

結果如下,真的是 None,

1
2
hello world
None

函式回傳 str 字串

在 Python 中如果函式要回傳 str 字串的話,

1
2
3
4
5
6
def myfunc():
return "hello world"

return_value = myfunc()
print(return_value)
print(type(return_value))

結果如下,

1
2
hello world
<class 'str'>

函式回傳 list 串列

在 Python 中如果函式要回傳 list 串列的話,

1
2
3
4
5
6
def myfunc():
return [1,2,3]

return_value = myfunc()
print(return_value)
print(type(return_value))

結果如下,

1
2
[1, 2, 3]
<class 'list'>

函式回傳 dict 字典

在 Python 中如果函式要回傳 dict 字典的話,

1
2
3
4
5
6
def student():
return {1:'tom', 2:'john', 3:'alice'}

return_value = student()
print(return_value)
print(type(return_value))

結果如下,

1
2
{1: 'tom', 2: 'john', 3: 'alice'}
<class 'dict'>

函式回傳 tuple 元組

在 Python 中如果函式要回傳 tuple 元組的話,

1
2
3
4
5
6
def myfunc():
return (1,2,3)

return_value = myfunc()
print(return_value)
print(type(return_value))

結果如下,

1
2
(1, 2, 3)
<class 'tuple'>

那如果不加小括號呢?

1
2
3
4
5
6
def myfunc():
return 1,2,3

return_value = myfunc()
print(return_value)
print(type(return_value))

結果如下,寫跟上個例子少了小括號也是 tuple,這部分要記得容易搞混唷!

1
2
(1, 2, 3)
<class 'tuple'>

那如果要回傳不同資料型態的 tuple 呢?

1
2
3
4
5
6
7
8
9
10
11
12
def student():
return 1, 'tom'

return_value = student()
print(return_value)
print(type(return_value))

id, name = student()
print(id)
print(name)
print(type(id))
print(type(name))

結果如下,

1
2
3
4
5
6
(1, 'tom')
<class 'tuple'>
1
tom
<class 'int'>
<class 'str'>

函式回傳 set 集合

在 Python 中如果函式要回傳 set 集合的話,

1
2
3
4
5
6
def myfunc():
return {1,2,3}

return_value = myfunc()
print(return_value)
print(type(return_value))

結果如下,

1
2
{1, 2, 3}
<class 'set'>

以上就是 Python 函式的基本觀念與用法的介紹,透過本篇教學希望能夠讓你對 Python 函式有個初步的了解,並且可以自己練習寫出一個函式來,下一篇會來介紹 str 字串的用法

其它相關文章推薦
Python 新手入門教學懶人包
Python 讀取 txt 文字檔
Python 寫檔,寫入 txt 文字檔
Python 讀取 csv 檔案
Python 寫入 csv 檔案
Python 讀寫檔案
Python 產生 random 隨機不重複的數字 list
Python PyAutoGUI 使用教學
Python OpenCV resize 圖片縮放