Home » Python Tutorial » Python Return Statement with Examples

Python Return Statement with Examples

The Python return statement is used with the function. To return some value from the function, the python return statement is used. The line of code after the return statement will not execute. In case nothing is specified to the return statement, then no value is returned by a function.

Syntax

Def functionname():
	Statement 1
	Statement 2
		.
		.
		.
	Statement n
	return [expression]

Example

def mult(a,b):
   c = a*b
   return c

a = int(input("Enter first number"))
b = int(input("Enter second number"))
result = mult(a,b)
print("Multiplication of {} and {} is {}".format(a,b,result))

Output

Enter first number5
Enter second number6
Multiplication of 5 and 6 is 30

Returning a python return multiple values.

In python, the statement return in python provides a feature of returning multiple values at a time. We have not seen this before in other programming languages like c, c++, etc. The example is given below.

Example

def demo():
   print("This function will return multiple values at a time.")
   return 1,2,3,4,5,6

a,b,c,d,e,f = demo()
print("Values returned by function are - {} {} {} {} {} {}".format(a,b,c,d,e,f))

Output

This function will return multiple values at a time.
Values retuned by function are - 1 2 3 4 5 6

Returning an object of a class in python.

Same as c++, java, and other object-oriented languages, It is possible to create a class and return an object in python. Let’s understand this with an example.

Example

class student:
   name = "Pawan Misal"
   Std = "TY"
   Roll_no = "60"

def info():
   return student

obj = info()
print(obj.name)
print(obj.Std)
print(obj.Roll_no)

Output

Pawan Misal
TY
60

In the above example, the Python function returned the class student. Using its object we can access the values declared inside it.

Returning a list in python.

The List is a data structure in python. Python interpreter allows us to return a list as a return value of a function. Let’s see how it works.

Example

def demo():
   mylist = [1,2,3,4,5,6,7,8,9]
   return mylist

result = demo()
print("Returned value of function is : {}".format(result))

Output

Returned value of function is : [1, 2, 3, 4, 5, 6, 7, 8, 9]

Returning a tuple in python

Same as a list in python, It is possible to return a tuple as a returned value of a function. The example is given below.

Example

def demo():
   mytuple = (1, "one", 'a', 232.443)
   return mytuple

result = demo()
print("Returned value of function is : {}".format(result))

Output

Returned value of function is : (1, 'one', 'a', 232.443)

Returning a dictionary in python.

The Python dictionary is just like a hash map. In function, if we want to return a dictionary, then python allows you to do so. Also, accessing values is very simple. Let’s understand this with an example.

Example

def demo():
   mydict = {"name":"Pawan", "Year":"Last", "Roll_no": 60}
   return mydict
# accessing values of dictionary
result = demo() #function call
print("Returned value of function is : {}".format(result))

for i in result:
    print("{} of student is {}".format(i,result[i]))

Output

Returned value of function is : {'name': 'Pawan', 'Year': 'Last', 'Roll_no': 60}
name of student is Pawan
Year of student is Last
Roll_no of student is 60

Function returning a function in python

The function in python is treated as an object that’s why we can return it with a return statement in python.

Example

def outerfunction():

   def innerfuncion():
       str = "Pradtutorials"
       return str
   return innerfuncion()

result = outerfunction()
print("Value returned by function is :",result)

Output

Value returned by function is : Pradtutorials
Did you know?
1. Python Queue
2. Fibonacci Series In Python
3. Reverse a String in Python
4. Bubble Sort in Python
5. Slicing in Python
6. Recursion in Python
7. Matrix In Python
8. Binary Search in Python
9. Polymorphism in Python
10. Python return

Pin It on Pinterest