Тёмный

Python: RECURSION Explained 

Oggi AI - Artificial Intelligence Today
Подписаться 77 тыс.
Просмотров 295 тыс.
50% 1

An intro to recursion, and how to write a factorial function in Python using recursion.
RELATED VIDEOS:
► Lambda Functions: • Python LAMBDA Function...
► Unpacking Variables: • Unpacking Variables in...
► Iterating Lists: • Python ENUMERATE | Lis...
► Generators: • Python GENERATORS & us...
► Progress Bar: • Python: Progress Bar w...
► Datetime & Timestamp: • Python: Dates, Times &...
► HTTP Requests & Web APIs: • Python: Using Web APIs...
► List Comprehensions: • Python: List Comprehen...
► Set Comprehensions: • Python Set COMPREHENSI...
► All about Strings: • Python Strings Tutorial
► String Format: • Python String FORMATti...
► Recursion: • Python: RECURSION Expl...
► Exception Handling: • Python: Exception Hand...
► Debugger: • Python: Debugger
► Twitter: / joejamesusa
► Subscribe: bit.ly/like-this-channel
► Code on GitHub github.com/joeyajames/Python
► Thank me on Patreon: / joeyajames
0:00 Python
0:10 Example: find 5!
0:59 An Iterative Algorithm
2:05 A Recursive Algorithm
5:30 Recursion Pros & Cons
#python
@Dare to Explore

Наука

Опубликовано:

 

7 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 203   
@rochanhm
@rochanhm 5 лет назад
To understand recursion, you need to first understand recursion.
@da_lime
@da_lime 5 лет назад
to understandd recursion
@dragon_warrior_
@dragon_warrior_ 4 года назад
and then you must understand recursion recursively
@jalapenohiway
@jalapenohiway 4 года назад
😂😂😂
@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 6 лет назад
Again, another video that sums up 3 hours of lecture into a video under 10 minutes.
@Kyemech
@Kyemech 3 года назад
fucking right
@bevern9477
@bevern9477 3 года назад
Yes
@IvanIvanov-lr1gz
@IvanIvanov-lr1gz 2 года назад
*fucking useless 10 min video*
@sotam8938
@sotam8938 2 года назад
@@IvanIvanov-lr1gz ? why
@JustinDAMusic
@JustinDAMusic 11 месяцев назад
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.
@BigStuDolla91
@BigStuDolla91 16 дней назад
At 4:00 you explain recursion better than the ton of videos i've been watching. 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
@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 4 года назад
I get it. I GET IT.
@confidential303
@confidential303 4 года назад
Was this a recursive answer?
@chinmaydas4053
@chinmaydas4053 6 лет назад
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...
@abs8090
@abs8090 4 года назад
I needed to refresh my memory on recession and your video was short and straight the point, THANKS
@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
@innonhara7040
@innonhara7040 2 года назад
Best explanation of recursion I have come across on the net.
@JJJJ-gl2uf
@JJJJ-gl2uf 2 года назад
Clearly spoken, good explanation, fairly easy to understand. Well done.
@thesparedrumroom4082
@thesparedrumroom4082 3 года назад
this video gives it to you well, no beating around the bush to lengthen the viewing time. well done!
@DoYouHaveAName1
@DoYouHaveAName1 Год назад
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"
@nuhibrahim215
@nuhibrahim215 2 года назад
great job! keep it up! finally, a detailed explanation of recursion explaining about the things that going around in the background...
@ryandoucette4343
@ryandoucette4343 Год назад
Greatly appreciated for your time to explain these concepts.
@jk3089
@jk3089 4 года назад
Thank you Sir. Blessings be upon you. God bless you with peace.
@CasperWest
@CasperWest 3 года назад
THANK YOU! BEST VIDEO ON RECURSION!
@simonfortin2943
@simonfortin2943 4 года назад
2019 and I appreciated the visual explanation very much! Thank you.
@baharehkjani6761
@baharehkjani6761 Год назад
THANK YOU, DANKE, MERCI, SHUKRAN, SEPAS
@michaelcostantino5397
@michaelcostantino5397 2 года назад
Well done! Thank you. Very clear and easy to follow.
@ea1576
@ea1576 3 года назад
I promise you i love you omds this made everything so clear.
@bhallalot
@bhallalot 4 года назад
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!
@johnjones764
@johnjones764 6 лет назад
Excellent job. Your explanation is clear and easy to follow. Please do more videos like these. Many thanks for your help and hard work.
@gyanology3356
@gyanology3356 3 года назад
Thankyou sir for making this kind of helpful video for us. It really helped me to understand what recursion is.
@BuildingImpossibleDreams
@BuildingImpossibleDreams 4 года назад
Thank you for actually saying iterative is a loop. Book after book and video after video never say iterative is loop.
@spankfanuka9345
@spankfanuka9345 5 лет назад
Perfectly explained. Thank you
@animegirl4lyfe
@animegirl4lyfe 6 лет назад
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
@KillerTacos54
@KillerTacos54 2 года назад
That was immensely helpful, thank you!
@carbon9357
@carbon9357 2 года назад
You explained this amazingly
@sschiavo8190
@sschiavo8190 6 лет назад
Simple and to the point.
@4500danny
@4500danny 2 года назад
Great explanation in a simple way. Thanks
@Kyemech
@Kyemech 3 года назад
Definitely earned a subscribe here, thank you very much Joe
@dominicoreilly6934
@dominicoreilly6934 4 года назад
Thank you so much for this wonderful tutorial.
@akira_asahi
@akira_asahi 2 года назад
Thank you for the video. I am gratuful for your time and contribution. Kind regards, Akira.
@bocarlsson3rd
@bocarlsson3rd 2 года назад
Now I get it!! Thank you very much.
@jiachenhu8242
@jiachenhu8242 Месяц назад
Thanks for explaining this in a such simple and doable manner! 🎉🎉
@ShiwenDing
@ShiwenDing 4 года назад
very well explained, thanks a lot for the illustration !
@ignaciomarin6927
@ignaciomarin6927 4 года назад
helpful and to the point, thanks a bunch!
@sergiobatista5265
@sergiobatista5265 4 года назад
finally understand it now. thank you.
@mdougf
@mdougf 4 года назад
Beautiful. Thank you sir.
@ibrahimal-mahfooz8737
@ibrahimal-mahfooz8737 5 лет назад
Well explained. Thumbs up!
@maximum3080
@maximum3080 Год назад
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 Год назад
Glad to hear that! 😀
@gobhandle
@gobhandle 2 года назад
Thank you man, this was an excellent explanation.
@NeilLTayler
@NeilLTayler 3 года назад
thank you man, very helpful
@raibahati
@raibahati 4 года назад
Very well explained ! Thank you Sir!
@heavydremer8716
@heavydremer8716 2 года назад
thanks bro this helped alot fr. subscribed!!
@jamiewise3528
@jamiewise3528 3 года назад
Great job! Thank you!
@temiolatunji8730
@temiolatunji8730 3 года назад
Thank you sir, wonderful explanation.
@ephemral
@ephemral Год назад
Very easy to understand. Thank you!
@noramarji1538
@noramarji1538 4 года назад
Thank god for this video
@xinyichen1418
@xinyichen1418 2 года назад
Rly helpful. Thank you!
@jarrodabott
@jarrodabott 8 месяцев назад
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!).
@michaeljames1334
@michaeljames1334 2 года назад
this was a super good video. i appreciate it. subbed.
@dilshangunathilaka5551
@dilshangunathilaka5551 2 года назад
wow. Thanks a lot man ❤️❤️
@shiridalal273
@shiridalal273 4 года назад
now I get it. thanks!
@augustasmaciulskas4124
@augustasmaciulskas4124 2 года назад
Thank you, helped a lot.
@rafaelmaranhao3710
@rafaelmaranhao3710 Год назад
Great video!
@markwild508
@markwild508 5 лет назад
Great video man
@denizkrom9772
@denizkrom9772 4 года назад
thanks for the video!
@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.
@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! :)
@ekkomanhighlights388
@ekkomanhighlights388 3 года назад
Thank you !
@charlesmuthini1393
@charlesmuthini1393 5 лет назад
Excellent.
@DeezNuts43
@DeezNuts43 5 лет назад
Thanks for this.
@zoemohamed6599
@zoemohamed6599 3 года назад
Thanks 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.
@zpie0333
@zpie0333 6 лет назад
Thank you very much!!!
@internallyinteral
@internallyinteral 3 года назад
thanks Joe
@seankearsley6245
@seankearsley6245 4 года назад
thanks Joe James
@muhammadaffaq6687
@muhammadaffaq6687 4 года назад
Superb Explanation. Hats-off.
@oggiai
@oggiai 4 года назад
Thanks.
@user-px7gt7ig6t
@user-px7gt7ig6t 4 месяца назад
I think I fell in love with the word factorial
@beibeimiao2327
@beibeimiao2327 4 года назад
Thank you ❤
@gedtoon6451
@gedtoon6451 2 года назад
It would be interesting if you used timeit on both algorithms and see which is faster in python.
@s1mo
@s1mo Год назад
before this recursion seemed impossible to me, thanks
@oscarschuller2309
@oscarschuller2309 2 года назад
very nice video
@unutilizzatoreyoutubbicoca7749
@unutilizzatoreyoutubbicoca7749 3 года назад
amazing
@emprendeperez
@emprendeperez 5 лет назад
Thank you!!!
@onlylearning1959
@onlylearning1959 3 года назад
Thank you
@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.
@oxyht
@oxyht 6 лет назад
Thanks.
@antonlinden5216
@antonlinden5216 4 года назад
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 3 года назад
def factorial(n): return 1 if n < 1 else n * factorial(n-1) print(factorial(5))
@mhmmdabdullayev4386
@mhmmdabdullayev4386 3 года назад
Thanks🖤
@glennicetong843
@glennicetong843 2 года назад
thank you
@nahilachowdhury1785
@nahilachowdhury1785 4 года назад
Lovely Vid :)
@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!
@shashidhardevraj
@shashidhardevraj 5 лет назад
Thanks a lot
4 года назад
factorial = lambda n: reduce(lambda x,y: x*y, range(1, n+1))
@Stellarspace95
@Stellarspace95 5 лет назад
MY BRAIN HURTS
@silas-hw9274
@silas-hw9274 4 года назад
Same this just isn't making sense
@friendforlife3321
@friendforlife3321 5 лет назад
thank you for this video, super helpful! i need help understanding the iterative function at 8:10, esp the fact*=i part....could someone please help me?
@TheLambPost
@TheLambPost 5 лет назад
I'm somewhat new to coding too and haven't used that before but I would assume it's a shorthand way of writing fact = fact * i...it's more common to see += than *= but I would expect they work the same.
@arieljosol3094
@arieljosol3094 4 года назад
thanks
@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 5 лет назад
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.
@albertovitess1898
@albertovitess1898 4 года назад
Thank you. I get it ,plz can you make a video on tables ?
@oggiai
@oggiai 4 года назад
I have several on reading in data files, and on Pandas.
@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.
@MrJengo22
@MrJengo22 Год назад
I have a recursive function written that wants to “unwind” itself after the base call has been executed. How do stop/prevent unwinding
@He_ze
@He_ze 2 года назад
Wow I'm such a dummy. I didn't realize the difference between factorial and factor until now lol
@focusedonpost
@focusedonpost Год назад
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.
@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 4 года назад
@@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 Год назад
@@polysopher 4 years later, this still helped. Thanks!!
@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.
Далее
5 Simple Steps for Solving Any Recursive Problem
21:03
Recursion Simply Explained
27:58
Просмотров 8 тыс.
Python lists, sets, and tuples explained 🍍
15:06
Просмотров 240 тыс.
Programming Loops vs Recursion - Computerphile
12:32
Просмотров 1,4 млн
Python: Lambda, Map, Filter, Reduce Functions
9:59
Просмотров 378 тыс.
Python Tutorial: if __name__ == '__main__'
8:43
Просмотров 2 млн
Stepping Through Recursive Fibonacci Function
8:04
Просмотров 197 тыс.
10 Nooby Mistakes Devs Often Make In Python
24:31
Просмотров 50 тыс.
Recursion 'Super Power' (in Python) - Computerphile
12:18
This is a Better Way to Understand Recursion
4:03
Просмотров 30 тыс.
Так ли Хорош Founders Edition RTX 4080 ?
13:00
ИГРОВОВЫЙ НОУТ ASUS ЗА 57 тысяч
25:33
ИГРОВОВЫЙ НОУТ ASUS ЗА 57 тысяч
25:33