Python return Statement
A return keyword is used to end the function's execution and return the control to the caller statement of the function. The return keyword cannot be used outside of any method.
Syntax of Return in Python
The return statement is used to terminate the method execution or to return the control to the caller statement of the function. The caller statement can have single or multiple return values.
Let's understand the syntax of the return in python.
Syntax:
The return keyword can return a set of values in the tuple format, reference of a single object, single value, etc.
Let's use some examples to understand how to use the "return" keyword.
Example
In the below example, we created a class Student that contains a constructor that initializes the state of the class, i.e. variables name, age, and score. The method Returnname() returns the Student's name using the return statement.
Let's understand how a return statement is used to return the student's name.
Output:
The Raj is printed after calling the Returnname() method of the class.
Returning Multiple Values
Using an example, let's understand how to return multiple values using the return statement in python. The return statement can return multiple values with the help of the tuple data structure.
In the below example, we are returning multiple values in the arithmeticoperatons() method using the return statement using the symbol that converts the return values into a tuple data structure.
Let's understand this using an example.
Output:
The tuple is immutable, meaning its value cannot be modified once created.
Using Object
Let's understand how to use return in python to return an object of the class using an example.
In the below example, we are returning the reference of an object using the return statement in the call() method i.e return hello().
Output:
Using Tuple
In the below example, we are returning multiple values from the operation method in the tuple format.
Output:
The tuple is immutable, meaning its value cannot be modified once created.
Using the List
Let's understand how to return multiple values using the return statement in python.
The return statement can return multiple values with the help of the list data structure. Let's understand this using an example. In the example below, we are returning multiple values from the method operations in the list format.
Output:
The list is mutable, meaning its value can be modified once the list is created.
Using a Dictionary
Let's understand how to use a return statement in python to return the dictionary from the method using an example.
In the below example, we simply return a dictionary from the method Dict() and store it in the key variable.
Output:
Function Returning Another Function
Let's understand how to return one function from another function using the return statement in python using the below example.
In the below example, we are simply returning the first method in the second method using the return statement.
Output:
Return Function Name
We can return the function's name using the return statement in python.
Let's understand this using an example.
Output:
Using Data Class
Let's understand how to use the return statement in the data class. The data class modules efficiently allow for implementation.
Let's understand this using an example. In the example below, we are using a data class, and in the class, there is a function canvote() that returns a boolean value according to the parameter.
Output:
Conclusion
- The return in python is used to terminate the flow of execution of the particular method.
- The return in python is also used to return the values to the caller method.
- We can either use the return statement in the method or not.
- The return in python can return multiple values.