RECURSION - PYTHON PROGRAMMING

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends so welcome back to our channel so in the previous session we have seen the functions concept and then we have seen the difference between the local variables and up local variables and then we have seen the types of augments and now let us see one more concept in the functions that is a recursive function so very important concept recursion so what is memory recursion so a function calling itself is called a recursion that means the function definition and the function call inside the function definition again we will call the same function so that we call it as in recursion so function calling itself the self calling function right so here the important thing is there must be two cases what is discus and the vector sim case so we are saying that the recursion is function calling itself so every time if the function calls itself automatically it will be going to the infinite recursive not so there will be no end so every recursive function should terminate after some finite recursive calls so for that purpose for that termination would write some business eliminating so after five number of steps every recursive function should be terminated that means it should stop its recursive gods so coming to the recursive case here we will keep on calling the function itself calling itself right so if you write an example so that you will be careful so the best example for this recursion is right so why is this factorial is an example for recursion because see let us take this five factorial we can write this by factor yes 5 into 4 factorial in the next system we can write it as 5 into 4 into 3 factorial we can write this find into 4 into 3 into 2 February we can write the same as 5 into 4 into 3 into 2 into 1 5 so after finite number of steps it will return 1 so every time we are applying the factor there so we will write the code just like calling the same function C that is right here this is a very important thing to be remembered that is to cases what is this case a recursive list so base cases for terminating from the recursion and recursive cases calling itself and the program so that you will be factorial using recursive def fact to real off the N n is passed if L is equal to is equal to 0 or M is equal to is equal to 1 so in the both cases the answer is 1 because you know factor is 1 1 factor is 1 so here we can write it as written 1 yes repeat the same procedure written yes into factorial of n minus 1 so that means first file so file not equal to 0 not equal to 1 so the may then return five into factor of four in the next iteration 5 into 4 into factorial of 3 I will raise it when I'm here en is equal to rattling function and is equal to a of right on the input function rather value enter and value next result is equal to factorial of yen and here brain yes so this will automatically prints the result let us trace it let us taste it first LV is equal to 5 and is equal to 5 right now n is equal to final result is equal to factor of M so result is equal to factorial of 5 so what is the output of this 1 5 5 is equal to 0 5 0 is equal to 1 so it is pulse so it will return yelling the factor of 4 it will return 5 into factor little off right so it will return 5 into factor of 4 now again cyclo 4 will be pardoned so again factor of 4 4 is equal to 0 4 is equal to 1 again response so any the factor off so this will be given as 4 into active of think 4 in 2 factorial of 3 right next a factor of 3 is called so function calling itself right factor of 3 3 0 3 is equal to 1 so condition false else path it will return 3 into factor of 2 3 into factor of 2 next a 2 2 is equal to 0 2 is equal to 1 that means again it will return to unify cross 1 so this will return to into factor 1 again see factor of 1 1 in this version is equal to 1 - kalimpong so this will form this function will return 1 so what is the output for this 1 2 into 1 so to do we may return here so 2 into 3 2 into 3 that is 6 will be returned here next here six will be returned 16 - 4 4 into 6 24 24 is returned here 24 8 5 min - 24 so 5 into 44 is equal to 120 so this will be given to a result so in the result we will get 1 point II hope you understood oh yeah this one is the base case and this one is there a breaker suitcase right so it's some finite number of steps it will be terminated so from the termination it will be returned to the previous functions so here it will return 1 so going to 1 will be returned from this function 2 into 3 6 will be returned from this functional 62 424 will be returned from this function 24 into 5 that is 140 will be returned to reserve so if you print result we get the 1 but the two things we have in this recursion is this is it recursive case after some finite number of recursive calls every recursion function should be terminated right so here also there is no much complexity in the recursion just only one complex thing is we have to identify the base case and the recursive case so if you find this base case and records of this the logic would be very simple so hope you understood this one so let us stop here so if you really like my videos like my videos so share my sessions with your friends and if you are having any doubts regarding this right and programming feel free to post your dogs in the comment section so that I will definitely try to clarify on your thumbs so don't forget to subscribe to our channel so keep watching and thanks for listening thank you very much
Info
Channel: Sundeep Saradhi Kanthety
Views: 57,079
Rating: 4.9305816 out of 5
Keywords: SUNDEEP, SARADHI, KANTHETY, PYTHON, PROGRAMMING, HIGH-LEVEL, LANGUAGE, PORTABLE, FEATURES, APPLICATIONS, INSTALLATION, INTERPRETER, PYTHON SHELL, PYTHON SCRIPT, INTRODUCTION, BEGINNERS, GUI, GAMING, WEB DESIGNING, PYTHON 3.7.0, PLATFORM INDEPENDENT, CASE SENSITIVE, function call, function definition, def keyword, arguments., argument types, recursion, factorial, gcd, iteration
Id: F3yi5jmU-s4
Channel Id: undefined
Length: 10min 0sec (600 seconds)
Published: Fri Oct 12 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.