+ 3
Return
Can someone explain me "return" in python
10 Antworten
+ 6
Jakob ,
here is a short code sample that demonstrates the use of `return`:
https://sololearn.com/compiler-playground/c8w79FUr1j7u/?ref=app
+ 6
A function with return sends a value back to its caller.
It is useful when you need the data for other expressions and computations in the program
Whereas print prints the data on the screen when the function is called
Also in relation to
Function Termination: When a function executes a return statement, it ends immediately. If a function only has print statements, it continues executing until it reaches the end of the function or encounters a return statement.
+ 6
Here is an example to make this more clear
You can see in the second part of the code how it is used to terminate the program while in the first to get annn expression
# calculate the average grade
def calculate_average(grades):
if not grades: # list is empty?
return 0
return sum(grades) / len(grades)
# Function to determine the letter grade based on the average
def determine_letter_grade(average):
if average >= 90:
return 'A'
elif average >= 80:
return 'B'
elif average >= 70:
return 'C'
elif average >= 60:
return 'D'
else:
return 'F'
# List of grades
grades = [8, 90, 12, 77, 88]
# calling
average = calculate_average(grades)
# calling
letter_grade = determine_letter_grade(average)
# Output
print(f"The average grade is: {average:.2f}")
print(f"The letter grade is: {letter_grade}")
+ 3
Jakob yes
For example
def get_user_data():
return 'dan', 18, '17338'
res = get_user_data()
name, age, id = res
print('the data gotten from user:', name, age, id)
+ 2
Thanks
+ 2
Jakob your welcome , keep up the good work good luck
+ 1
Can you save multiple values with return?
+ 1
Please which app are u using to write ur codes on mobile phone
0
On my mobile phone I only use the code playground