Python 9.3: Difference between break and return

We will continue our learning here we will see the difference between the break and the return I started by commenting our program we had done above first of all it should be noted that the break and the return are linked to to the functions however they have several differences that we will explain through a function I will make def and I will name the function difference colon And in this function I'll run a for loop for i in range parenthesis I will put from zero comma à 20 colon Inside will be a print of i And at the bottom I'm going to call the function Like this I'll run You can see that it displays well from zero à 19 is displayed from zero to 19 because when we use the rank, the first value is inclusive and the second value is exclusive so it is not counted Now when we have the for loops loops or while loops we can put a break or a return so I'm going to start by making an IF and I'm going to put a break in the if so if if i is greater than à 10 colon we will make a break Like this I run my code You can see that it displays from 0 to 10 because when it gets to 10. It makes 10 is greater than 10. no It displays the 10 and when it goes back it makes 11 is greater than 10 it goes directly out of The loop But if I put instead a return Like this I restart you see there is no difference On the other hand I will make here a print so that you see the difference between two I put at the beginning of the program I copy Here I will end I'll restart you see that at the level of return Don't show the print End of program but if I put a break instead I put braek I restart It shows us the beginning of the program and the end of the program so the difference between the break and the return is that the break allows to exit the loop and continue to execute the program while the return exits directly from the function There is also another difference that the return is only applicable in functions so it is executed only in the functions while the break is written even in the main program I will comment this to show you the difference I comment like this and I will copy the for that I will display here below we put a break I run my code you see that it normally gives and when I put a return like this already there is an error if I retry you see there is an error on line 42 because the return only applies in the functions we saw earlier that to avoid putting the return we'll put the exit and we'll put zero if I relaunch like this well that was all for this video we say to the next continent the functions callback and lambdas functions.