+ 3
Difference between break and return.
I'm confuse about both of them it seems like both are similar ain't they?
6 ответов
+ 6
@Paul Jacobs good explanation. @Prince just don't confund break and continue either. break just go outside loop and continue skip the iteration but still keeps in loop.
I recommend with you have any doubts when try to learning read python3 documentation. And don't rush it. If you still don't understand post in Q&A section again. I will like to try answer.
Here is a link for more reference about control flow tools.
https://docs.python.org/3/tutorial/controlflow.html (Python3 documentation)
+ 4
Return is used in functions. Break in loops. Return has a value. Break doesn't.
At least that is the way I use them.
+ 3
code after return wont run inside a function
for example:
def func():
x = 123
return x
print(x)
when you call func() it wont print anything since print() is after return
break is used to ”break” loops and return is used to return a value from a function
+ 1
No. They are completely different things.
return (which is used only inside functions) can return a value from a function and store in a variable if you have provided 1.
But break is used on loops.
an instance return is used.
#initializing a function
def makeItThree():
return 3
#initizing variables
n2 = makeItThree()
n3 = makeItThree
print(n2) #3
print(n3) #3
#the reason is because the the variables have stored the value returned by the function - which is 3 here.
And the break thing
for i in range(1,10):
print(i)
if i == 5:
break
#1
#2
#3
#4
#5
According to for loop it should print numbers from 1 - 10 .
But break has stopped it after i is 5.
SIDE NOTE: using a return statement in a loop could also stop the loop from running. So use it wisely
0
break is used to stop a loop (while, for)
return is used to return a value of function or methods of class
0
For Break, please find the link below
https://youtu.be/yCZBnjF4_tU
This should be useful.
And Return, the return statement allows you to terminate the execution of a function before you reach the end.