0
Return vs print (in py)
So return can store only one value? I mean in for loop such as for i in range(5): result = i i+=1 print(result) Print will give you four numbers as looping result answers Why can we substitute “ return “ in the place of “ print “ This gives me syntax error
5 Antworten
+ 3
return doesn't store values,from my knowledge return statements are used in functions not loops
like..
def man:
return 5+5
+ 1
'print()' is a function, which output its arguments to the console.
'return' is a keyword, used to return from a function with, or without a value.
Examples:
for i in range(10):
print("i = ", i) # Output: 0..9
# Simple examples of return keyword
# Return, if odd number is found
even_ls = [2, 4, 5, 6, 8]
def check_even_list(even_ls):
for i in range(len(even_ls)):
if (even_ls[i] % 2 != 0):
print(":( The list is Wrong!")
return
print(":) The list is OK!")
check_even_list(even_ls) # Output: :( The list is Wrong!
# Returning value to the caller
def get_sum(a, b):
return a + b
sum = get_sum(5, 10)
print(sum)
+ 1
#difference between print and return.
return uses in functions.
e.g=
def h(a,b):
return a+b
function give some value using return if you use return in your function then you can do some more with it.
e.g=
print(h(5,3)*2) #output=16
here you can see we don't multiply with 2 in function but we can also do more stuff with it when you call a function,remember that in print() statement this is opposite.
0
I didn’t know.
Thank you Brains.
0
print gives you an output anywhere while return returns any number of values from a function so that the returned value may be used later in the code.
if you want to return all values of a loop you can make a list and .append(i), or if you wish to return a single item from a loop, use if statements to rule out wrong items in the iteration.