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)
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP