Python return语句

介绍

Python return语句用于从函数中返回一个值。用户只能在函数中使用 return 语句。它不能在 Python 函数之外使用。return 语句包括 return 关键字和其后将返回的值。

return语句的语法:


def funtion_name():
statements
.
.
.
return [expression]

程序 1


def adding(x, y):
    i = x + y
    return i
result = adding(16, 25)
print(f'Output of adding(16, 25) function is {result}')

输出

程序 2


def adding(a, b):
    # this function is return the value of (a + b)
    return a + b
def boolean_function(a):
    # this function is return the Boolean value
    return bool(a)
# calling function
flag = adding(2, 3)
print("Output of first function is {}".format(flag))
flag = boolean_function(9 < 5)
print("\nOutput of second function is {}".format(flag))

输出。

返回多个值

在 Python 编程语言中,用户可以从一个函数中返回多个值。以下是各种方法。

1。使用对象:这个方法类似于 C / C ++ 和 Java 。用户可以创建一个类来保存函数中的多个值,并返回该类的一个对象。


class a:
    def __init__(self):
        self.omg = "javatpoint is the best website to learn"
        self.i = 122
# This function will return an object of the class a
def test():
    return a()
# Driver code to test the above method
z = test()
print(z.omg)
print(z.i)

输出

2。使用 Tuple:Tuple 类似于一个列表,但是 Tuple 和 list 之间略有不同。在元组中,对象值不能更改,而列表中的对象值可以更改。


def test():
    omg = "javatpoint is the best website to learn"
    i = 122
    return omg, i; 
    # Return tuple, we could also.  
# Driver code to test the above method.
omg, i = test() 
# Assign return tuple
print(omg)
print(i)

输出

3。使用列表:列表类似于动态调整大小的数组。在列表中,用户可以将所有内容存储在一个变量中。


def test():
    omg = "javatpoint"
    i = 122
    return [omg, i];
# Driver code to test the above method
list = test()
print(list)

输出

4。使用字典:在 Python 语言中,字典是非结构化项目的集合,用于存储哈希值或映射等数据值。


def test():
    a = dict();
    a['omg'] = "javatpoint"
    a['i'] = 122
    return a    
# Driver code to test the above method
a = test()
print(a)

输出

5。使用数据类(Python 3.7+)


from dataclasses import dataclass
@dataclass
class Book_list:
    bookname: str
    cost: float
    quantity_of_book_available: int = 0     
    # This function is used to calculate the total cost of the books    
    def total_cost_of_book(self) -> float:
        return self.cost * self.quantity_of_book_available       
book = Book_list("Python programming language.", 499, 10)
i = book.total_cost_of_book()
# print the total cost
print(i)
# print the details of the book
print(book)

输出

函数返回另一个函数

在 Python 编程语言中,函数是对象的形式。因此,用户可以从另一个函数返回一个函数。

在下面的程序中,第一个函数返回第二个函数。


def first_add(x):
    def second_add(y):
        return x + y
    return second_add
i = first_add(20)
print("The value of x + y is", i(10))
# second function
def outer_func(x):
    return x * 5
def func():
    # return the value in the different function
    return outer_func
# storing the function in z
z = func()
print("\nThe value of x * y is", z(10))

输出


原创文章,作者:简单一点,如若转载,请注明出处:https://www.506064.com/n/129571.html

(0)
简单一点的头像简单一点
上一篇 2024-10-03
下一篇 2024-10-03

相关推荐

  • 如何正确清理python对象(如何用python进行数据清洗)

    本文目录一览: 1、python如何清理内存 2、[Python]不小心将python内置的对象赋值,如何清除? 3、Python 中有方法可以直接删除一个对象吗 4、如何进行处理…

    编程 2024-10-04
  • Python和C++学哪个好?

    一、学习曲线 1、Python学习曲线较浅 Python相对于C++而言,语法简单,容易上手,并且几乎没有学习门槛,即使是没有编程经验的新手也能够迅速入门。Python采用的是动态…

    编程 2024-10-04
  • Python与Redis的连接

    一、Python连接Redis集群 在实际应用中,为了应对高并发量的访问请求,我们通常需要使用Redis集群来提高应用的性能。下面介绍Python连接Redis集群的方式: imp…

    编程 2024-10-04
  • c语言编织图形,c++图形化编程

    本文目录一览: 1、C语言编写输出图形 2、用c语言编写心形图案 3、C语言编写一个图形程序 4、如何用c语言编写图形 C语言编写输出图形 第一行0个空格 第二行1个空格 第三行2…

    编程 2024-10-14
  • 半导体测试的详细阐述

    一、半导体测试入门 半导体是指导电性介于导体与绝缘体之间的材料,半导体材料的各项物理性质并不稳定,而且在不同的工作温度、电场、磁场下势必产生各种不同的分布,这也就决定了半导体元件的…

    编程 2024-10-04
  • 包含使用java窃取sina大片的词条

    本文目录一览: 1、java如何调用sina微博接口 2、JAVA编程利用Object getContent()方法获得某一URL地址(如新浪,搜狐,网易 3、请问怎样用Java获…

    编程 2024-10-03
  • Linux查看PID详解

    一、Linux查看PID命令 Linux下最基本的查看PID命令是ps命令,该命令提供的信息非常丰富,能够查看所有进程的状态及占用CPU、内存等情况。查看当前所有运行的进程: ps…

    编程 2024-10-10
  • 正则表达式:使用JavaScript对网页进行搜索与替换

    正则表达式是一门独特的语言,它用于匹配字符串中的模式。它由各种元字符和普通字符组成。它可以在各种编程语言中使用,包括JavaScript。 一、使用正则表达式进行简单的匹配 在Ja…

    编程 2024-10-04
  • cmd编译python文件(在cmd里运行python文件)

    本文目录一览: 1、怎么用cmd运行python 2、尝试编译Python文件失败,因为问题,怎么解决 3、怎么在cmd运行python 4、如何在windows下编译执行pyth…

    编程 2024-10-03
  • java使用自定义(Java使用自定义的linux命令)

    本文目录一览: 1、如何使用java自定义类 2、java关于自定义类及其应用 3、java 自定义类型 4、java自定义类及使用 5、Java中如何调用函数和自定义函数 如何使…

发表回复

登录后才能评论