+

How do I return an object from a function in Python?

The return statement causes the python function to exit and return a value to the caller. The purpose of functions in general is to accept input and return something. The return statement, once executed, immediately stops the execution of the function, even if it is not the last statement in the function.

Functions that return values ​​are sometimes called fruitful functions. 

 def sum (a, b): return a + b sum (5,16) 

Output





 21 

Everything in Python, almost everything is an object. Lists, dictionaries, tuples are also Python objects. The code below shows a Python function that returns a Python object; dictionary

 # This function returns a dictionary def foo (): d = dict (); d [`str`] =" Tutorialspoint " d [` x`] = 50 return d print foo () 

Output

 {` x`: 50, `str`:` Tutorialspoint`} 



Get Solution for free from DataCamp guru