Python return Statement

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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:

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more