
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to return an object from a function in Python?
In Python, we can return an object from a function, using the return keyword, just like any other variable.
The statements after the return will not be executed. The return keyword cannot be used outside the function. If the function has a return statement without any expression, then the special value None is returned. In the following example, the function returned the sum of two numbers -
def Sum(a,b): return a+b my_var1 = 23 my_var2 = 105 result_1 = Sum(my_var1, my_var2) # function call print(result_1)
Following is an output of the above code -
128
Return Value in Void Function
The function that does not perform any operations and the body of the function is empty is known as a void function. To avoid IndentationError, we use the pass keyword. The void function returns None.
def Empty(): pass obj=Empty() print("Return type of void function :",obj)
Following is an output of the above code -
Return type of void function : None
Returning Dictionary
Everything is an object. Lists, dictionaries, and tuples are Python objects. In the following example, we have defined a dictionary, my_dict() function, which returns a dictionary object -
def my_dict(): d=dict() d['Telangana']='Hyderabad' d['Tamilnadu']='Chennai' return d obj=my_dict() print(obj)
Following is an output of the above code:
{'Telangana': 'Hyderabad', 'Tamilnadu': 'Chennai'}