Python中的return語句
Python中的return語句是一個非常有用的語句,用於將程式的流程從函式返回到函式呼叫者。關鍵字return用於編寫return語句。
由於Python中的一切都是物件,因此返回值可以是任何物件,例如數字型(int、float、double)或集合型(list、tuple、dictionary),或者使用者定義的函式和類,或包。
return語句具有以下特性:
return語句不能在函式外部使用。
return語句之後編寫的任何程式碼都稱為死程式碼,因為它永遠不會被執行。
return語句可以隱式或顯式地傳遞任何值,如果沒有給出值,則返回None。
語法
以下是Python中return語句的語法:
def some_function(parameters): return <expression> print(some_function)
示例
以下是return語句的簡單示例:
def welcome(str): return str + " from TutorialsPoint" print(welcome("Good morning"))
輸出
以下是上述程式碼的輸出:
Good morning from TutorialsPoint
return語句在多種情況下都很有用,以下部分將討論return語句的不同用例以及示例。
Python中return語句的用法
函式是任何程式語言的核心,因為它們允許程式碼模組化,從而降低程式複雜性。函式可以在自身內部顯示結果,但這會使程式變得複雜,因此最好將所有函式的結果傳遞到一個公共位置。
在這種情況下,return語句很有用,因為它會終止當前正在執行的函式,並將程式的控制權傳遞給呼叫該函式的語句。
示例
在下面的程式碼中,sum_fibonacci函式用於計算斐波那契數列中前15項的和。計算出和之後,它會列印並將和傳遞給sum_result變數。這是為了表明在函式內部列印和使用return語句返回值會給出相同的輸出。
# Defining function to calculate sum of Fibonacci series def sum_fibonacci(terms): first_term = 0 second_term = 1 sum_series = 0 # Finding the sum of first 15 terms of Fibonacci series for i in range(0, terms): sum_series = sum_series + first_term next_term = first_term + second_term first_term = second_term second_term = next_term # Printing the sum inside the function print("Sum of Fibonacci series inside the function is = {}".format(sum_series)) # Returning the sum using return statement return sum_series # Invoking the sum_fibonacci function sum_result = sum_fibonacci(15) print("Sum of Fibonacci series outside the function is = {}".format(sum_result))
輸出
輸出顯示,使用print語句在函式內部得到的和與使用return語句在函式外部得到的和相等。
Sum of Fibonacci series inside the function is = 986 Sum of Fibonacci series outside the function is = 986
使用return語句返回函式
在Python中,函式是一等公民,這意味著它們可以儲存在變數中,或者可以作為引數傳遞給另一個函式。由於函式是物件,因此return語句可以用來從另一個函式中返回一個函式作為值。返回函式或將函式作為引數的函式稱為高階函式。
示例
在這個示例中,finding_sum函式包含另一個函式——add。首先呼叫finding_sum函式,並將第一個數字作為引數接收。add函式接收第二個數字作為引數,並將兩個數字的和返回給finding_sum函式。然後,finding_sum函式將add函式作為值返回給sum變數。
# Defining function to return sum of two numbers # Function to get the first number def finding_sum(num1): # Function to get the second number def add(num2): return num1 + num2 # return sum of numbers to add function return add # return value present in add function to finding_sum function sum = finding_sum(5) print("The sum of the two numbers is: {}".format(sum(10)))
輸出
程式的輸出給出兩個數字5和10的和。
The sum of the two numbers is: 15
使用return語句返回None
Python中的函式總是返回值,即使return語句沒有顯式編寫。因此,Python沒有過程,在其他程式語言中,過程是沒有return語句的函式。如果return語句沒有返回值或從函式中省略,則Python將隱式返回預設值None。
只有當程式包含多個return語句時,才應考慮顯式呼叫return None,以便讓其他程式設計師知道函式的終止點。
示例
下面的程式完美地說明了使用return None。在這個程式中,check_prime()函式用於檢查列表中是否包含任何素數。如果列表包含素數,則列印列表中存在的全部素數。但是,如果列表中沒有素數,則返回None,因為該程式包含多個return語句,因此顯式呼叫None。
def check_prime(list): prime_list = [] for i in list: counter = 0 for j in range(1, i): if i % j == 0: counter = counter + 1 if counter == 1: prime_list.append(i) if len(prime_list): return prime_list else: return None list = [4, 6, 8, 10, 12] print("The prime numbers in the list are: {}".format(check_prime(list)))
輸出
輸出列印None,因為列表中沒有素數。
The prime numbers in the list are: [4]
使用return語句返回多個值
Python中的return語句也可以使用“,”來分隔值,從單個函式中返回多個值。當需要對相同資料集執行多次計算而無需更改原始資料集時,此功能特別有用。return語句的結果是值的元組。
示例
在這個例子中,使用了statistics庫的內建函式來計算均值、中位數和眾數,這些值使用單個return語句返回,展示瞭如何從一個函式中返回多個值。
import statistics as stat # Defining function to perform different statistical calculations def finding_stats(data): return stat.mean(data), stat.median(data), stat.mode(data) # returning multiple values list_numbers = [5, 7, 13, 17, 17, 19, 33, 47, 83, 89] print("The mean, median and mode of the data is: {}".format(finding_stats(list_numbers)))
輸出
輸出給出資料集的均值、中位數和眾數,型別為元組。
The mean, median and mode of the data is: (33, 18.0, 17)