How is the return statement used in Python?
In Python, the return statement is used to return a value from a function. When a function is called, the return statement can send a value back to the caller and end the function’s execution. Here is an example of how the return statement is used:
def add_numbers(a, b):
result = a + b
return result
sum = add_numbers(3, 5)
print(sum) # 输出:8
In the example above, the function add_numbers takes two parameters, a and b, calculates their sum, and returns the result. Calling add_numbers(3, 5) will give a result of 8, which is then assigned to the variable sum and printed out.