Python Return Statement Tutorial - Python Tutorial | Logic Practice
Python Course / Functions / Python Return Statement Tutorial

Python Return Statement Tutorial

BLUF: This lesson on Python Return Statement Tutorial provides a comprehensive guide to understanding and implementing this concept in Python. Whether you're a beginner or looking to refresh your knowledge, you'll find clear explanations and interactive code examples here.
Key Concept: Python Return Statement Tutorial

Mastering Python Return Statement Tutorial is essential for building efficient Python applications. Focus on the syntax and the best practices highlighted in this tutorial.

Introduction

In Python, the return statement serves the purpose of providing a value from a function. This statement is exclusive to functions, meaning it cannot be utilized outside the confines of a Python function. A return statement consists of the return keyword followed by the value intended to be returned.

Syntax of return statement:

Example

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

Program 1

Example

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

Output

Program 2

Example

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))

Output.

Returning Multiple Values

In the Python programming language, it is possible for a user to return several values from a function. Below are the different approaches to achieve this.

  1. Utilizing an Object: This approach bears resemblance to the techniques used in C / C++ and Java. A user can define a class designed to encapsulate multiple values within a function and subsequently return an instance of that class.
Example

class a:
	def __init__(self):
		self.omg = "logicpractice 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)
  1. Utilizing Tuple: A tuple resembles a list, yet there exists a notable distinction between the two. In a tuple, the values of the objects remain immutable, whereas in a list, the object values are mutable and can be altered.
Example

def test():
	omg = "logicpractice 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)
  1. Utilizing a list: A list functions similarly to an array that can dynamically adjust its size. Within a list, users have the ability to store a variety of items under one single variable.
Example

def test():
	omg = "logicpractice"
	i = 122
	return [omg, i];
# Driver code to test the above method
list = test()
print(list)
  1. Utilizing dictionaries: In Python, a dictionary is an unordered collection of items that serve the purpose of storing data values, functioning similarly to a hash or map.
Example

def test():
	a = dict();
	a['omg'] = "logicpractice"
	a['i'] = 122
	return a
# Driver code to test the above method
a = test()
print(a)
  1. Employing Data Class (Python 3.7+)
Example

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)

Output

Function returning another function

In the Python programming language, functions are treated as first-class objects. This means that it is possible for a user to return a function from within another function.

In the program shown below, the function firstadd yields the function secondadd as its output.

Example

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))

Output

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience