Python: RECURSION Explained

  Рет қаралды 302,591

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!
@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 2 жыл бұрын
@@IvanIvanov-lr1gz ? why
@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
@BigStuDolla91
@BigStuDolla91 4 ай бұрын
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...
@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!).
@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! 😀
@baharehkjani6761
@baharehkjani6761 Жыл бұрын
THANK YOU, DANKE, MERCI, SHUKRAN, SEPAS
@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...
@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. :)
@asands123
@asands123 5 жыл бұрын
I get it. I GET IT.
@confidential303
@confidential303 4 жыл бұрын
Was this a recursive answer?
@abs8090
@abs8090 5 жыл бұрын
I needed to refresh my memory on recession and your video was short and straight the point, THANKS
@BuildingImpossibleDreams
@BuildingImpossibleDreams 4 жыл бұрын
Thank you for actually saying iterative is a loop. Book after book and video after video never say iterative is loop.
@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.
@WhetūAroha
@WhetūAroha 5 ай бұрын
Thanks for explaining this in a such simple and doable manner! 🎉🎉
@akira_asahi
@akira_asahi 2 жыл бұрын
Thank you for the video. I am gratuful for your time and contribution. Kind regards, Akira.
@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.
@mrsdhuha
@mrsdhuha 4 күн бұрын
This is very good way of explaining it - Many thanks.
@CasperWest
@CasperWest 3 жыл бұрын
THANK YOU! BEST VIDEO ON RECURSION!
@jk3089
@jk3089 4 жыл бұрын
Thank you Sir. Blessings be upon you. God bless you with peace.
@ryandoucette4343
@ryandoucette4343 2 жыл бұрын
Greatly appreciated for your time to explain these concepts.
@simonfortin2943
@simonfortin2943 5 жыл бұрын
2019 and I appreciated the visual explanation very much! Thank you.
@gyanology3356
@gyanology3356 3 жыл бұрын
Thankyou sir for making this kind of helpful video for us. It really helped me to understand what recursion is.
@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))
@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
@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!
@carbon9357
@carbon9357 2 жыл бұрын
You explained this amazingly
@bocarlsson3rd
@bocarlsson3rd 3 жыл бұрын
Now I get it!! Thank you very much.
@4500danny
@4500danny 2 жыл бұрын
Great explanation in a simple way. Thanks
@גלמלאך
@גלמלאך 8 ай бұрын
I think I fell in love with the word factorial
@michaeljames1334
@michaeljames1334 3 жыл бұрын
this was a super good video. i appreciate it. subbed.
@muhammadaffaq6687
@muhammadaffaq6687 4 жыл бұрын
Superb Explanation. Hats-off.
@oggiai
@oggiai 4 жыл бұрын
Thanks.
@michaelcostantino5397
@michaelcostantino5397 2 жыл бұрын
Well done! Thank you. Very clear and easy to follow.
@Kyemech
@Kyemech 4 жыл бұрын
Definitely earned a subscribe here, thank you very much Joe
@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.
@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!
@gobhandle
@gobhandle 2 жыл бұрын
Thank you man, this was an excellent explanation.
@heavydremer8716
@heavydremer8716 2 жыл бұрын
thanks bro this helped alot fr. subscribed!!
@KillerTacos54
@KillerTacos54 3 жыл бұрын
That was immensely helpful, thank you!
@NeilLTayler
@NeilLTayler 4 жыл бұрын
thank you man, very helpful
@RRatedT
@RRatedT 4 жыл бұрын
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! :)
@noramarji1538
@noramarji1538 4 жыл бұрын
Thank god for this video
@s1mo
@s1mo 2 жыл бұрын
before this recursion seemed impossible to me, thanks
@helovesdata8483
@helovesdata8483 2 жыл бұрын
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 2 жыл бұрын
Directories are Trees, and are a perfect application for recursion.
@dominicoreilly6934
@dominicoreilly6934 4 жыл бұрын
Thank you so much for this wonderful tutorial.
@temiolatunji8730
@temiolatunji8730 4 жыл бұрын
Thank you sir, wonderful explanation.
5 жыл бұрын
factorial = lambda n: reduce(lambda x,y: x*y, range(1, n+1))
@ephemral
@ephemral 2 жыл бұрын
Very easy to understand. Thank you!
@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
@ShiwenDing
@ShiwenDing 4 жыл бұрын
very well explained, thanks a lot for the illustration !
@mdougf
@mdougf 4 жыл бұрын
Beautiful. Thank you sir.
@dilshangunathilaka5551
@dilshangunathilaka5551 3 жыл бұрын
wow. Thanks a lot man ❤️❤️
@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!!
@sschiavo8190
@sschiavo8190 6 жыл бұрын
Simple and to the point.
@xinyichen1418
@xinyichen1418 2 жыл бұрын
Rly helpful. Thank you!
@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 Жыл бұрын
It’s returned from successive function calls.
@jamiewise3528
@jamiewise3528 3 жыл бұрын
Great job! Thank you!
@seankearsley6245
@seankearsley6245 4 жыл бұрын
thanks Joe James
@sergiobatista5265
@sergiobatista5265 4 жыл бұрын
finally understand it now. thank you.
@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.
@ibrahimal-mahfooz8737
@ibrahimal-mahfooz8737 5 жыл бұрын
Well explained. Thumbs up!
@shiridalal273
@shiridalal273 4 жыл бұрын
now I get it. thanks!
@raibahati
@raibahati 4 жыл бұрын
Very well explained ! Thank you Sir!
@spankfanuka9345
@spankfanuka9345 5 жыл бұрын
Perfectly explained. Thank you
@ignaciomarin6927
@ignaciomarin6927 5 жыл бұрын
helpful and to the point, thanks a bunch!
@gedtoon6451
@gedtoon6451 2 жыл бұрын
It would be interesting if you used timeit on both algorithms and see which is faster in python.
@Stellarspace95
@Stellarspace95 5 жыл бұрын
MY BRAIN HURTS
@silas-hw9274
@silas-hw9274 4 жыл бұрын
Same this just isn't making sense
@christophercarr5865
@christophercarr5865 3 жыл бұрын
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.
@silverfluoride5073
@silverfluoride5073 4 жыл бұрын
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 4 жыл бұрын
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.
@zoemohamed6599
@zoemohamed6599 3 жыл бұрын
Thanks Joe
@rafaelmaranhao3710
@rafaelmaranhao3710 2 жыл бұрын
Great video!
@denizkrom9772
@denizkrom9772 4 жыл бұрын
thanks for the video!
@unutilizzatoreyoutubbicoca7749
@unutilizzatoreyoutubbicoca7749 4 жыл бұрын
amazing
@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
@oscarschuller2309
@oscarschuller2309 2 жыл бұрын
very nice video
@augustasmaciulskas4124
@augustasmaciulskas4124 2 жыл бұрын
Thank you, helped a lot.
@ekkomanhighlights388
@ekkomanhighlights388 3 жыл бұрын
Thank you !
@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.
@He_ze
@He_ze 2 жыл бұрын
Wow I'm such a dummy. I didn't realize the difference between factorial and factor until now lol
@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.
@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.
@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
@markwild508
@markwild508 6 жыл бұрын
Great video man
@AliHaider-bc7cl
@AliHaider-bc7cl 7 ай бұрын
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
@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
@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
@charlesmuthini1393
@charlesmuthini1393 6 жыл бұрын
Excellent.
@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.
@jorriffhdhtrsegg
@jorriffhdhtrsegg 2 жыл бұрын
I now understand recursion. I now don't quite understand why 0!=1 again. 😄
@maximum3080
@maximum3080 2 жыл бұрын
this is probably old but in his example it breaks it down at 4:46 and at the bottom you can see that it returns 1 so you can think in the sense that even if its 0! its still gonna end up returning 1. Idk if this helped at all and you most likely figured it out already but hope you have a good day
@nahilachowdhury1785
@nahilachowdhury1785 5 жыл бұрын
Lovely Vid :)
@maquindesign9158
@maquindesign9158 5 жыл бұрын
Can recursion be explained without factorial?
@oggiai
@oggiai 5 жыл бұрын
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.
@DeezNuts43
@DeezNuts43 6 жыл бұрын
Thanks for this.
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
Recursion 'Super Power' (in Python) - Computerphile
12:18
Computerphile
Рет қаралды 491 М.
ЗНАЛИ? ТОЛЬКО ОАЭ 🤫
00:13
Сам себе сушист
Рет қаралды 4 МЛН
Human vs Jet Engine
00:19
MrBeast
Рет қаралды 184 МЛН
🕊️Valera🕊️
00:34
DO$HIK
Рет қаралды 19 МЛН
Try Not To Laugh 😅 the Best of BoxtoxTv 👌
00:18
boxtoxtv
Рет қаралды 7 МЛН
KulturaSerye: Dokumento: Recording Culture and the Arts
1:57:15
Gateway Gallery
Рет қаралды 17
6. Recursion and Dictionaries
48:22
MIT OpenCourseWare
Рет қаралды 377 М.
Python lists, sets, and tuples explained 🍍
15:06
Bro Code
Рет қаралды 300 М.
Recursion for Python Beginners with Recursive Function Examples
17:54
Please Master These 10 Python Functions…
22:17
Tech With Tim
Рет қаралды 200 М.
Programming Loops vs Recursion - Computerphile
12:32
Computerphile
Рет қаралды 1,5 МЛН
What on Earth is Recursion? - Computerphile
9:40
Computerphile
Рет қаралды 746 М.
What does '__init__.py' do in Python?
6:50
Indently
Рет қаралды 75 М.
Recursion Simply Explained with Code Examples - Python for Beginners
9:07
Python Simplified
Рет қаралды 173 М.
Python Classes and Objects - OOP for Beginners
8:01
Python Simplified
Рет қаралды 563 М.
ЗНАЛИ? ТОЛЬКО ОАЭ 🤫
00:13
Сам себе сушист
Рет қаралды 4 МЛН