- BASICS
- Statements
- Operators
- Functions
- Incremental
- Errors
- FUNCTIONS
- Function Definition
-
Recursion
- Objects
- STRINGS
- Immutable
- Raw Strings
- Regex
- Validation
- Config
- Security
- Encrypt
- CLASS
- Definition
- Attributes
- Functional
- Methods
- COLLECTIONS
- Lists
- List Comprehension
- Dictionaries
- Dictionary Efficiency
- Tuples
- References
- Iterable
- STORAGE
- Files
- Databases
- Pipes
- With Open
- Shelve
- Zip
- Csv
- Json
PYTHON PAGES - LEVEL 1
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__
"""
Questions and answers:
Clink on Option to Answer
1. In Python it is legal for a function to call itself.
- a) true
- b) false
2. It is easier to use for loop instead of recursion.
- a) always
- b) it depends