- BASICS
- Statements
- Operators
- Functions
- Incremental
- Errors
- FUNCTIONS
-
Recursion
- Objects
- Lambda
- STRINGS
- Immutable
- Raw Strings
- Validation
- Config
- Security
- CLASS
- Definition
- Attributes
- Functional
- Methods
- COLLECTIONS
- Lists
- Dictionaries
- Efficiency
- Tree
- Iterator
- Tuples
- References
- STORAGE
- Files
- Databases
- Pipes
- With Open
- Shelve
- Zip
- Csv
- Json
Recursion
""" It is legal for a function to call itself
It is one of the most magical things a program can do
"""
def countdown(n):
if n <= 0:
print ("End")
else:
print(n, end=" ")
countdown(n-1)
countdown(3)
"""
3 2 1 End
"""
Execution Flow
""" Recursion / Execution Flow
"""
def countdown(n):
if n <= 0:
print ("End")
else:
print(n, end=" ")
countdown(n-1)
countdown(3) # 3 2 1 End
"""
countdown executions begins with n=3 ...
since n is greater than 0 it outputs n (3) ..
then it calls itself ...
begins with n=2 ...
since n > 0 it outputs n (2) ..
then it calls itself ...
begins with n=1 ...
since n > 0 it outputs n (1) ..
then it calls itself ...
begins with n=0 ...
since n <= 0 it outputs End ..
then returns None
the countdown that got n=1 returns
the countdown that got n=2 returns
the countdown that got n=3 returns
After that we are back in the __main__
"""
Last update: 203 days ago
- Think Python, Allen Downey / amazon.com
- Automate the Boring Stuff with Python, Al Sweigart / amazon.com
- Beyond the Basic Stuff, Al Sweigart / amazon.com
- Machine Learning with Python Cookbook, Chris Albon / amazon.com
- Python for Data Analysis, Wes McKinney / amazon.com
- Python Recursion / greenteapress.com
- Application Example: Factorial / github.com
- Application Example: Fibonacci / github.com