Python: RECURSION Explained

  Рет қаралды 305,115

Oggi AI - Artificial Intelligence Today

Oggi AI - Artificial Intelligence Today

Күн бұрын

Пікірлер: 198
@rochanhm
@rochanhm 6 жыл бұрын
To understand recursion, you need to first understand recursion.
@da_lime
@da_lime 5 жыл бұрын
to understandd recursion
@dragon_warrior_
@dragon_warrior_ 5 жыл бұрын
and then you must understand recursion recursively
@jalapenohiway
@jalapenohiway 5 жыл бұрын
😂😂😂
@abdelrahmanabuissa9139
@abdelrahmanabuissa9139 4 жыл бұрын
And to understand recursion, you need to understand recursion
@bentabot4521
@bentabot4521 4 жыл бұрын
Shut up your didnt help at all
@danbw8838
@danbw8838 6 жыл бұрын
Thank you for this. Its harder than you would think to find a beginner recursion tutorial thats easy to understand
@puravida77700
@puravida77700 6 жыл бұрын
Agreed. First video that actually made sense. Thanks!
@karleme9679
@karleme9679 4 жыл бұрын
Was puzzled by recursion until you explained that it digs down to the base case and then works back up. Thank you
@Ty-vp1jp
@Ty-vp1jp 7 жыл бұрын
Again, another video that sums up 3 hours of lecture into a video under 10 minutes.
@Kyemech
@Kyemech 4 жыл бұрын
fucking right
@bevern9477
@bevern9477 3 жыл бұрын
Yes
@IvanIvanov-lr1gz
@IvanIvanov-lr1gz 3 жыл бұрын
*fucking useless 10 min video*
@sotam8938
@sotam8938 3 жыл бұрын
@@IvanIvanov-lr1gz ? why
@BigStuDolla91
@BigStuDolla91 5 ай бұрын
At 4:00 you explain recursion better than the ton of videos i've been watching. thanks
@JustinDAMusic
@JustinDAMusic Жыл бұрын
This video is definitely one of the most comprehensive explanations of recursion for beginners. I always wondered how calculations were being done if the function is repeatedly calling itself but as explained in this video the function keeps calling itself until it reaches the base case and then only are the calculations done. I love the analogy of digging a hole.
@DoYouHaveAName1
@DoYouHaveAName1 2 жыл бұрын
Thank you for this explanation, I like that you compared it as "we are digging ourselves deeper into a hole and then digging ourselves out of it"
@innonhara7040
@innonhara7040 3 жыл бұрын
Best explanation of recursion I have come across on the net.
@absolutezero9947
@absolutezero9947 6 жыл бұрын
Amazing video, not monotonous, but still gives a good overview and lesson on recursion in python! Keep up the good work!
@oggiai
@oggiai 6 жыл бұрын
Thanks
@nuhibrahim215
@nuhibrahim215 2 жыл бұрын
great job! keep it up! finally, a detailed explanation of recursion explaining about the things that going around in the background...
@maximum3080
@maximum3080 2 жыл бұрын
thank you so much, you probably will never see this comment or care but i personally wanted too thank you for helping me to understand recursion better, my computer science teacher was explaining it and I had a hard time understanding but when you explained simpler it it was much easier to understand :)
@oggiai
@oggiai 2 жыл бұрын
Glad to hear that! 😀
@JJJJ-gl2uf
@JJJJ-gl2uf 2 жыл бұрын
Clearly spoken, good explanation, fairly easy to understand. Well done.
@chinmaydas4053
@chinmaydas4053 7 жыл бұрын
Sir your are doing great job.It's very helpful when you first give the pseducode of that algorithms then give the programs in specific language implementation..love you sir.please give more and algorithms implementation in java and python programming languages..this will be very helpful for us...
@baharehkjani6761
@baharehkjani6761 Жыл бұрын
THANK YOU, DANKE, MERCI, SHUKRAN, SEPAS
@abs8090
@abs8090 5 жыл бұрын
I needed to refresh my memory on recession and your video was short and straight the point, THANKS
@asands123
@asands123 5 жыл бұрын
I get it. I GET IT.
@confidential303
@confidential303 4 жыл бұрын
Was this a recursive answer?
@jarrodabott
@jarrodabott Жыл бұрын
A very concise explanation without your face as the focus but rather the issue at hand. The tutorial is on point without having to learn the creation of the universe, which is so annoying. My first Python subscription and like (whoa!).
@alphabetsalphabets5242
@alphabetsalphabets5242 3 жыл бұрын
Very nice! Visualizing it like you did at 4:52 made it extremely obvious and clear. Very good tutorial and explanation.
@alphabetsalphabets5242
@alphabetsalphabets5242 3 жыл бұрын
@Sabinka47 then you don't understand recursion.
@alphabetsalphabets5242
@alphabetsalphabets5242 3 жыл бұрын
@Sabinka47 if you have a function calling itself it needs to be able to pass the data back to the place where it was called.
@mybluemars
@mybluemars 3 жыл бұрын
Thank you Joe! Now I have a basic understanding of RECURSION and can see how searches such as tree traversals and binary code work. :)
@BuildingImpossibleDreams
@BuildingImpossibleDreams 4 жыл бұрын
Thank you for actually saying iterative is a loop. Book after book and video after video never say iterative is loop.
@ryandoucette4343
@ryandoucette4343 2 жыл бұрын
Greatly appreciated for your time to explain these concepts.
@CasperWest
@CasperWest 3 жыл бұрын
THANK YOU! BEST VIDEO ON RECURSION!
@ea1576
@ea1576 3 жыл бұрын
I promise you i love you omds this made everything so clear.
@animegirl4lyfe
@animegirl4lyfe 7 жыл бұрын
Great video! We are learning recursion right now
@impmadness
@impmadness 3 жыл бұрын
3 years later, how has your programming knowledge advanced thus far? I’m curious, Im studying for a CE degree and I’ll learn recursion in a few weeks
@akira_asahi
@akira_asahi 2 жыл бұрын
Thank you for the video. I am gratuful for your time and contribution. Kind regards, Akira.
@carbon9357
@carbon9357 2 жыл бұрын
You explained this amazingly
@4500danny
@4500danny 2 жыл бұрын
Great explanation in a simple way. Thanks
@mrsdhuha
@mrsdhuha Ай бұрын
This is very good way of explaining it - Many thanks.
@gyanology3356
@gyanology3356 3 жыл бұрын
Thankyou sir for making this kind of helpful video for us. It really helped me to understand what recursion is.
@johnjones764
@johnjones764 7 жыл бұрын
Excellent job. Your explanation is clear and easy to follow. Please do more videos like these. Many thanks for your help and hard work.
@ArdernJacina250
@ArdernJacina250 6 ай бұрын
Thanks for explaining this in a such simple and doable manner! 🎉🎉
@bocarlsson3rd
@bocarlsson3rd 3 жыл бұрын
Now I get it!! Thank you very much.
@michaelcostantino5397
@michaelcostantino5397 3 жыл бұрын
Well done! Thank you. Very clear and easy to follow.
@muhammadaffaq6687
@muhammadaffaq6687 4 жыл бұрын
Superb Explanation. Hats-off.
@oggiai
@oggiai 4 жыл бұрын
Thanks.
@michaeljames1334
@michaeljames1334 3 жыл бұрын
this was a super good video. i appreciate it. subbed.
@Kyemech
@Kyemech 4 жыл бұрын
Definitely earned a subscribe here, thank you very much Joe
@antonlinden5216
@antonlinden5216 5 жыл бұрын
Came up with a short recursive python snippet while you were beginning to explain the topic. 5 lines, if anyone has a shorter one, post it below ;) def factorial(n): if n > 1: return n * factorial(n - 1) return n print(factorial(5))
@ferhatakbulut6572
@ferhatakbulut6572 4 жыл бұрын
def factorial(n): return 1 if n < 1 else n * factorial(n-1) print(factorial(5))
@גלמלאך
@גלמלאך 9 ай бұрын
I think I fell in love with the word factorial
@NeilLTayler
@NeilLTayler 4 жыл бұрын
thank you man, very helpful
@gobhandle
@gobhandle 2 жыл бұрын
Thank you man, this was an excellent explanation.
@simonfortin2943
@simonfortin2943 5 жыл бұрын
2019 and I appreciated the visual explanation very much! Thank you.
@bhallalot
@bhallalot 5 жыл бұрын
Thanks for the explanation. I really appreciate how you explained the part about the memory. This cleared up my confusion. This was organized really well!
5 жыл бұрын
factorial = lambda n: reduce(lambda x,y: x*y, range(1, n+1))
@RRatedT
@RRatedT 5 жыл бұрын
Great video, btw you don't really need the "else" on 05:10, because if it's not lower than 2 it doesn't go in anyway so you can just write on the same tab as the if "return ...". Thanks for sharing the video! :)
@heavydremer8716
@heavydremer8716 3 жыл бұрын
thanks bro this helped alot fr. subscribed!!
@KillerTacos54
@KillerTacos54 3 жыл бұрын
That was immensely helpful, thank you!
@s1mo
@s1mo 2 жыл бұрын
before this recursion seemed impossible to me, thanks
@FullMetal-nu8bj
@FullMetal-nu8bj 6 жыл бұрын
Could someone explain on a lower level how recursion works? How does the interpreter assign each recursive call to memory? how does it keep track of the call stack?
@oggiai
@oggiai 6 жыл бұрын
The call stack works just like it always works. Each new function call gets it's own call frame on top of the stack, and that frame stays open until it returns a value. So when you get deep into a recursive series of function calls you could have a very large call stack that eats a ton of memory. That's where the term "Stack Overflow" comes from, if you run out of memory allocated to the stack.
@polysopher
@polysopher 6 жыл бұрын
Hi, a little late but you can try python tutor visualization. www.pythontutor.com/visualize.html#mode=display
@philb1466
@philb1466 5 жыл бұрын
@@polysopher Never too late! There's always someone looking around for information on topics that may be sometime in the past.. Like me! 😁 .. thanks, that link really helped, even a year later!
@magnacarta7045
@magnacarta7045 2 жыл бұрын
@@polysopher 4 years later, this still helped. Thanks!!
@jk3089
@jk3089 4 жыл бұрын
Thank you Sir. Blessings be upon you. God bless you with peace.
@mdougf
@mdougf 4 жыл бұрын
Beautiful. Thank you sir.
@temiolatunji8730
@temiolatunji8730 4 жыл бұрын
Thank you sir, wonderful explanation.
@Stellarspace95
@Stellarspace95 5 жыл бұрын
MY BRAIN HURTS
@silas-hw9274
@silas-hw9274 4 жыл бұрын
Same this just isn't making sense
@dominicoreilly6934
@dominicoreilly6934 4 жыл бұрын
Thank you so much for this wonderful tutorial.
@ephemral
@ephemral 2 жыл бұрын
Very easy to understand. Thank you!
@dilshangunathilaka5551
@dilshangunathilaka5551 3 жыл бұрын
wow. Thanks a lot man ❤️❤️
@noramarji1538
@noramarji1538 4 жыл бұрын
Thank god for this video
@seankearsley6245
@seankearsley6245 4 жыл бұрын
thanks Joe James
@Sickkkkiddddd
@Sickkkkiddddd Жыл бұрын
Watched 20 videos today and I am still dumb as a rock about recursions. I'm ready to pick up knitting or another hobby that doesn't make me question my sanity. Can't keep putting time and effort into computing concepts designed to kick my ass.
@oggiai
@oggiai Жыл бұрын
Start slow and easy. Learn to use variables, lists, for loops and if-else statements first. Then build on it. Recursion is a tricky concept to understand, but this is as simple as I can make it.
@ShiwenDing
@ShiwenDing 4 жыл бұрын
very well explained, thanks a lot for the illustration !
@ibrahimal-mahfooz8737
@ibrahimal-mahfooz8737 5 жыл бұрын
Well explained. Thumbs up!
@morgansolar
@morgansolar 4 жыл бұрын
finally understand it now. thank you.
@xinyichen1418
@xinyichen1418 2 жыл бұрын
Rly helpful. Thank you!
@sschiavo8190
@sschiavo8190 6 жыл бұрын
Simple and to the point.
@gedtoon6451
@gedtoon6451 2 жыл бұрын
It would be interesting if you used timeit on both algorithms and see which is faster in python.
@jamiewise3528
@jamiewise3528 3 жыл бұрын
Great job! Thank you!
@raibahati
@raibahati 4 жыл бұрын
Very well explained ! Thank you Sir!
@regivm123
@regivm123 6 жыл бұрын
Great tutorial. On this topic I face a bottleneck and not able to explain. I tried to sum all natural numbers of 'n' and not able explain the mechanics. def sum_n(n): if n== 0: return 0 else: print('n ', n) res = n + sum_n(n-1) print('res ',res) return res print(sum_n(5)) n 5 n 4 n 3 n 2 n 1 res 1 res 3 res 6 res 10 res 15 15 As the loop run, n is reduced to 1... it is clear. However, res adds cumulatively AFTER this. I expected print(res) to print one value (15). Is there a loop between 'res = n + sum_n(n-1)' and 'return res'?? It is slightly different to your example. But can you clarify?
@TheLambPost
@TheLambPost 6 жыл бұрын
It's similar to a loop but you have to visualize it differently to really understand what's happening. Each time you hit the res = n + sum_n(n-1) line the entire function is called again (going deeper down the hole as Joe said), until res == 0 (the bottom of the hole). At which point it returns 0 back up to the previous time that the function was called, which was when n = 1, so now the line res = n + sum_n(n-1) is really res = 1 + 0, and the code continues from there to return res, which is now 1, farther back up the hole. Each time res increases by a small amount until you get back to the original call to the function, and at that point you have the final answer to the algorithm. If it still doesn't make sense it might help to make a chart of all the variables and what value they have at each call to the function, then to follow the code line by line and fill in the chart as the values change.
@shiridalal273
@shiridalal273 4 жыл бұрын
now I get it. thanks!
@ignaciomarin6927
@ignaciomarin6927 5 жыл бұрын
helpful and to the point, thanks a bunch!
@icontentacademy
@icontentacademy 2 жыл бұрын
Excellent video. In recursion how is the fact tracking the total. I see in code how we are tracking the total using iteration, but how does it remember the total using recursion? Hope my question makes sense.
@oggiai
@oggiai 2 жыл бұрын
It’s returned from successive function calls.
@MrJengo22
@MrJengo22 2 жыл бұрын
I have a recursive function written that wants to “unwind” itself after the base call has been executed. How do stop/prevent unwinding
@spankfanuka9345
@spankfanuka9345 6 жыл бұрын
Perfectly explained. Thank you
@anag99
@anag99 2 жыл бұрын
The cons and pros finally gave me a good perspective on this
@shpazhist
@shpazhist 5 жыл бұрын
Hey Joe -- Really luv your videos -- can you pls do a video tho with the list what one needs to learn to become really more-or-less professional developer? I.e. Like im personally trying to learn it by myself and think that sorta Im on early-intermediate level of proficiency; however, there is not even one single good source that Ive encountered that would really expand more on actual Software Engineering/Computer Science *list of topics one need to know* rather than just Foundations/Syntax. I.e. what CS areas one MUST know like, for instance, this concept of recursion: Like the following is the list that came to my mind but perhaps you can add on from your experience: 1. recursion 2. CRUD 3. algorithms 4. MVC/MVT for Django 5. What are design patterns and why one needs to learn them 6. version control 7. Waterfall engineering model 8. Types of programming: functional, OOP, etc -- why need different? 9. Dynamic programming vs. static 10. What/Why testing is needed? 11. SCRUM/Agile etc 12. how to better learn problem solving skills for coding/approaches/methods of thinking etc, other important topics/concepts/areas .... and etc and in all fairness, someone who would do, say, 1hr/30 mins lecture with literally covering all these topics for like 30sec/1 min each per term in 1-2hr would be an absolute GOD of youtube!!!! for all software developers as he would provide a sorta oxygen to combine all CS concepts (not just syntax) into one sorta coherent and comprehensive fashion. Even just mentioning all the terms, saying, ok -- CRUD is for websites, Waterfall model is just for writing your code, OOP is for writing logical steps for organizing processes and attributes of objects etc Thus, the biggest problem for the inexperienced like me is that all these terms come just from trial and error of exploring and working on problems rather than anyone really covering them first and sorta providing a blueprint or a path on how to become an advanced software engineer I hope I explained what Im looking for but if not, please revert and I will be happy to expand. and Many thanks for your channel again!
@helovesdata8483
@helovesdata8483 3 жыл бұрын
I wont lie, recursion wore me out, but I get it now. Often I like to loop through a directory in python with the os.listdir() function. I realize a for loop will only go through the directory but not directories within the directory.
@oggiai
@oggiai 3 жыл бұрын
Directories are Trees, and are a perfect application for recursion.
@christophercarr5865
@christophercarr5865 4 жыл бұрын
I think new people need to understand the call stack before they get recursive functions. You can get the structure of recursion in the abstract well enough, but not understand how it seems to magically spit out the answer when it stops recursing.
@internallyinteral
@internallyinteral 4 жыл бұрын
thanks Joe
@augustasmaciulskas4124
@augustasmaciulskas4124 2 жыл бұрын
Thank you, helped a lot.
@denizkrom9772
@denizkrom9772 4 жыл бұрын
thanks for the video!
@silverfluoride5073
@silverfluoride5073 5 жыл бұрын
Splendid, I read through my lecture's book and got totally confused, now with this video I just realized the lecture's book sucks
@KoryGraphic
@KoryGraphic 5 жыл бұрын
I'm in the same situation, I have had a lecturer that could barely speak English and he was teaching from an unnecessarily complicated set of slides.
@oscarschuller2309
@oscarschuller2309 3 жыл бұрын
very nice video
@druidmonkey
@druidmonkey 26 күн бұрын
omg finaly an answer thats easy to uder stand
@rafaelmaranhao3710
@rafaelmaranhao3710 2 жыл бұрын
Great video!
@Kingofqueers1
@Kingofqueers1 6 жыл бұрын
Can you list some applications of recursion, for example where would someone use recursion in the real world
@oggiai
@oggiai 6 жыл бұрын
Recursion is very commonly used in Tree algorithms, like doing a Find in a binary search tree. In fact, it would be tough to implement a tree without recursion.
@abigailspencer4113
@abigailspencer4113 Жыл бұрын
I wrote out the same code at the end and got different numbers. I got 6 from get_recursive_factorial and 1 for get_iterative_factorial. Not sure why the outputs would be different with the same code and inputs.
@oggiai
@oggiai Жыл бұрын
Share your code
@yogendrarajahsaarangan457
@yogendrarajahsaarangan457 4 жыл бұрын
what will happen to the code after recursion function called? code afterward executed every time or after the recursion function finishes ?
@yogendrarajahsaarangan457
@yogendrarajahsaarangan457 4 жыл бұрын
def m_sort(arr): if len(arr) > 1: mid = len(arr)//2 a = arr[mid:] b = arr[:mid] m_sort(a) m_sort(b) i = j = k = 0 while i < len(a) and j < len(b): if a[i] < b[j]: arr[k] = a[i] i += 1 else: arr[k] = b[j] j += 1 k += 1
@yogendrarajahsaarangan457
@yogendrarajahsaarangan457 4 жыл бұрын
in this code that while loop will be executed everytime when recursion function executed or it will be executed after the recursion function finishes ?
@oggiai
@oggiai 4 жыл бұрын
Yogendrarajah Saarangan the recursion has to finish before the rest of the program can execute.
@yogendrarajahsaarangan457
@yogendrarajahsaarangan457 4 жыл бұрын
@@oggiai thanks
@albertovitess1898
@albertovitess1898 5 жыл бұрын
Thank you. I get it ,plz can you make a video on tables ?
@oggiai
@oggiai 5 жыл бұрын
I have several on reading in data files, and on Pandas.
@zeinelmokhtar4105
@zeinelmokhtar4105 5 жыл бұрын
In the recursion way, someone will need to write down five functions - before calling them- to just calculate "5!". right?
@Ronitrocket
@Ronitrocket 5 жыл бұрын
no the function calls it self. You make it once, you call it once
@wolfcompany2
@wolfcompany2 4 жыл бұрын
i dun get it :/ 5:16 if i say getFac(4) it will return n * (n-1) , 4 * (4-1) = 12 ? How does this work LOL? i can understand the iterative, but the recursive i died... like where is the loop for the recursive??!!? HOW DOES IT CALL ITSELF I DONT GET IT
@oggiai
@oggiai 4 жыл бұрын
Sorry that’s as simple as I can make it. You might benefit from watching a few other Recursion videos. And if you call getFac(4) it should return 4x3x2 = 24.
@Anonymous-pr3gr
@Anonymous-pr3gr 6 жыл бұрын
Is it normal that my for loop can compute much larger factorial numbers than the simple recursion my prof codded?
@oggiai
@oggiai 6 жыл бұрын
not surprising, as I mentioned, recursion's achilles heal is the risk of stack overflows for function calls.
@wexwexexort
@wexwexexort 6 жыл бұрын
You should check out what "memoization" is.
@He_ze
@He_ze 2 жыл бұрын
Wow I'm such a dummy. I didn't realize the difference between factorial and factor until now lol
@markwild508
@markwild508 6 жыл бұрын
Great video man
@maquindesign9158
@maquindesign9158 6 жыл бұрын
Can recursion be explained without factorial?
@oggiai
@oggiai 6 жыл бұрын
Yes, one of the best applications for recursion is in trees, such as a find function in a binary search tree. But it’s not easy to explain recursion using a tree example because the tree itself is a higher level concept than recursion.
@charlesmuthini1393
@charlesmuthini1393 6 жыл бұрын
Excellent.
@onlylearning1959
@onlylearning1959 3 жыл бұрын
Thank you
@nahilachowdhury1785
@nahilachowdhury1785 5 жыл бұрын
Lovely Vid :)
@DeezNuts43
@DeezNuts43 6 жыл бұрын
Thanks for this.
@AliHaider-bc7cl
@AliHaider-bc7cl 8 ай бұрын
def fu(x): if x>0: result = x+fu(x-1) print(result) else: result = 0 return result fu(5) output: 1 3 6 10 15 could anybody please help me out with how this is working .. iv not been able to figure it out yet. thanks
@user-tt3vp1uw6o
@user-tt3vp1uw6o 2 жыл бұрын
I would have liked an example or two of recursive functions that would not work because they would go on forever
@zpie0333
@zpie0333 6 жыл бұрын
Thank you very much!!!
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
Recursion 'Super Power' (in Python) - Computerphile
12:18
Computerphile
Рет қаралды 491 М.
I thought one thing and the truth is something else 😂
00:34
عائلة ابو رعد Abo Raad family
Рет қаралды 17 МЛН
Python: Lambda, Map, Filter, Reduce Functions
9:59
Oggi AI - Artificial Intelligence Today
Рет қаралды 384 М.
Recursion for Python Beginners with Recursive Function Examples
17:54
Programming Loops vs Recursion - Computerphile
12:32
Computerphile
Рет қаралды 1,5 МЛН
6. Recursion and Dictionaries
48:22
MIT OpenCourseWare
Рет қаралды 379 М.
Python Tutorial: if __name__ == '__main__'
8:43
Corey Schafer
Рет қаралды 2 МЛН
Learn RECURSION in 5 minutes! 😵
5:59
Bro Code
Рет қаралды 179 М.
What on Earth is Recursion? - Computerphile
9:40
Computerphile
Рет қаралды 748 М.
ML Was Hard Until I Learned These 5 Secrets!
13:11
Boris Meinardus
Рет қаралды 343 М.
Recursive Backtracking - DSA Course in Python Lecture 14
12:58
Recursion Simply Explained with Code Examples - Python for Beginners
9:07
Python Simplified
Рет қаралды 180 М.
I thought one thing and the truth is something else 😂
00:34
عائلة ابو رعد Abo Raad family
Рет қаралды 17 МЛН