Python return statement

Python return statement

Introduction

The Python return statement is used to return a value from a function. The user can only use the return statement in a function. It cannot be used outside of the Python function. A return statement includes the return keyword and the value that will be returned after that.

Syntax of return statement:

Program 1

Output

Python return statement

Program 2

Output.

Python return statement

Returning Multiple Values

In the Python programming language, a user can return multiple values from a function. The following are the various methods for this.

1. Using Object: This method is similar to C / C ++ and Java. A user can create a class to hold multiple values in a function and return an object of that class.

Output

Python return statement

2. Using Tuple: The tuple is similar to a list, but there is a slight difference between tuple and list. In the tuple, the object values cannot be changed, while the object value can be changed in the list.

Output

Python return statement

3. Using list: The list is similar to the dynamically sized array. In the list, the user can store everything in a single variable.

Output

Python return statement

4. Using dictionary: In the Python language, a dictionary is a collection of unstructured items that are used to store data values such as hash or map.

Output

Python return statement

5. Using Data Class (Python 3.7+)

Output

Python return statement

Function returning another function

In the Python programming language, a function is in the form of an object. Therefore, the user can return a function from another function.

In the below program, The first_add function returns the second_add function.

Output

Python return statement


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263516.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论