Return Values in Python
After a calculation or processing inside a Python function, the return statement is used to send the result outside the function.
return is —
the result prepared by the function
return that result to the caller.
1. What is return?
return is a Python keyword that sends a value out of a function.
def add(a, b):
return a + b
Here, the function returns the result of the addition.
2. Calling a Function with return
def add(a, b):
return a + b
result = add(10, 20)
print(result)
Output:
30
We can store the returned value in the result variable.
3. return vs print()
| return | print() |
|---|---|
| Sends a value outside the function | Displays a value on the screen |
| The returned value can be stored in a variable | Mainly used for display |
| Can be used for further calculations | Does not provide a result directly for calculations |
4. Example using print()
def add(a, b):
print(a + b)
add(10, 20)
Output:
30
Here, the result was only displayed on the screen.
5. Example using return
def add(a, b):
return a + b
result = add(10, 20)
print(result)
Output
30
Here, the result was returned and stored in a variable.
6. Using Returned Value in Another Calculation
def add(a, b):
return a + b
result = add(10, 20)
final_result = result * 2
print(final_result)
Output:
60
7. Returning a String
def welcome(name):
return "Welcome " + name
message = welcome("Amit")
print(message)
Output:
Welcome Amit
8. Returning Boolean Value
A function can also return True or False.
def is_even(number):
return number % 2 == 0
print(is_even(10))
print(is_even(7))
Output:
True
False
9. Returning Multiple Values
A Python function can also return more than one value.
def calculate(a, b):
addition = a + b
subtraction = a - b
return addition, subtraction
add_result, sub_result = calculate(20, 10)
print("Addition:", add_result)
print("Subtraction:", sub_result)
Output:
Addition: 30
Subtraction: 10
10. Returning a List
def get_courses():
courses = [
"Python",
".NET",
"Java",
"Data Science"
]
return courses
courses = get_courses()
print(courses)
Output:
['Python', '.NET', 'Java', 'Data Science']
11. Returning a Dictionary
def get_student():
student = {
"name": "Amit",
"course": "Python",
"city": "Pune"
}
return student
student = get_student()
print(student)
Output:
{'name': 'Amit', 'course': 'Python', 'city': 'Pune'}
12. return Stops Function Execution
When Python encounters a return statement, function execution stops at that point.
def test():
print("Start")
return 100
print("End")
result = test()
print(result)
Output:
Start
100
"End" was not printed because function execution stopped after the return statement.
13. return Without a Value
If you write only return in a function, Python returns None.
def test():
return
result = test()
print(result)
Output:
None
14. CIIT Fee Example 📩🖥️
A return value is useful for calculating the final fee based on the course fee and discount.
def calculate_fee(fee, discount):
discount_amount = fee * discount / 100
final_fee = fee - discount_amount
return final_fee
final_fee = calculate_fee(35000, 10)
print("Final Fee:", final_fee)
Output:
Final Fee: 31500.0
15. Student Result Example 📩🖥️
def calculate_percentage(marks, total):
percentage = (marks / total) * 100
return percentage
percentage = calculate_percentage(450, 500)
print("Percentage:", percentage)
Output:
Percentage: 90.0
16. return with if Statement
def check_result(marks):
if marks >= 40:
return "Pass"
return "Fail"
print(check_result(75))
print(check_result(30))
Output:
Pass
Fail
17. Using One Function's Return in Another Function
def add(a, b):
return a + b
def double(number):
return number * 2
result = add(10, 20)
final_result = double(result)
print(final_result)
Output:
60
18. Common Mistakes
# Wrong approach
def add(a, b):
print(a + b)
result = add(10, 20)
print(result)
Output:
30
None
Here, the function did not return a value. It only printed the result, so None was stored in result.
# Correct approach
def add(a, b):
return a + b
result = add(10, 20)
print(result)
Output
30
19. Interview Points
- What does the return statement do in Python?
- What is the difference between return and print()?
- Can a Python function return multiple values?
- Does code execute after a return statement?
- What is returned if a function does not have a return statement?
- Can a function return a list or dictionary?
CIIT Learning Point
The return statement makes functions powerful and reusable. Returned values can be stored in variables and used for further calculations, conditions, and other functions.
CIIT Practice Tasks
-
Addition:
Create anadd(a, b)function that returns the addition. -
Maximum Number:
Create afind_max(a, b)function that returns the maximum of the two numbers. -
Even/Odd:
Create acheck_number(number)function that returns "Even" or "Odd". -
Student Result:
Create acalculate_percentage(marks, total)function that returns the percentage. -
Course Fee:
Create acalculate_fee(fee, discount)function that returns the final fee after applying the discount. -
Multiple Values:
Create a function that returns the student's name, course, and city as multiple values.
Summary :
In Python, the return statement sends the result from a function back to the caller. The returned value can be stored in a variable and used for further calculations, conditions, and other functions. Python functions can return a single value as well as multiple values, lists, and dictionaries.