prev
next
Or press ESC to close.
To continue enjoying our content, please consider disabling your ad blocker. Your support helps us continue providing quality content. To continue enjoying our content, please consider disabling your ad blocker. Your support helps us continue providing quality content.

Python Programming Part 12

Rajjit Oct 21st, 2023 10 mins read
part-12

TOPIC: Python functions - Recursive functions and main function

Recursive functions
In Python, recursive functions are functions that call themselves to solve a problem or perform a task. Typically, recursive functions have a base case (a condition that stops the recursion) and a recursive case (a condition that continues the recursion). Here's a structure of a recursive function in Python:

def recursive_function(parameters):
    if base_case:
        # The base case - the condition where the recursion stops
    else:
        # Recursive case - the function calls itself with modified parameters
        return recursive_function(modified_parameter)
Advantages of using recursive functions: 1. Elegance and Readability: Recursive functions often provide elegant and readable solutions to problems, mirroring mathematical descriptions.
2. Problem Solving: They excel at solving complex problems by breaking them into smaller, manageable subproblem.
3. Modularity: Recursive functions promote code modularity by breaking down problems into smaller, self-contained units.
4. Reduced Redundancy: They minimize code duplication, enhancing maintainability.
5. Versatility and Divide-and-Conquer: Recursive functions are versatile, handling problems of varying sizes, and align with the "divide and conquer" strategy.

main function
In Python, the if __name__ == "__main__": construct is a common idiom used to control the execution of code in a script. Understanding this construct is essential for writing modular and reusable code.

Here's a detailed explanation of the "main case" in Python:
  • Execution Control:
    The if __name__ == "__main__": block is a conditional statement that determines whether the code within the block should be executed. It is used to control what code gets executed when a Python script is run.
  • The Main Program:
    In Python, when you run a script, the Python interpreter sets a special variable called __name__ to "__main__" if the script is the main program being executed. If the script is imported as a module into another script, the __name__ variable is set to the name of the script or module.
  • A simple syntax
    def some_function():
        # Code for a function
    def another_function():
        # Code for another function
    if __name__ == "__main__":
        # Code that should be executed when the script is run
        print("This code is in the main part of the script.")
        some_function()
  • The if __name__ == "__main__": construct is commonly used in Python scripts to encapsulate the script's primary functionality. It's often found at the end of the script. In summary, the "main case" in Python, using if __name__ == "__main__":, provides a clear and controlled way to structure your code, making it both reusable as a module and self-contained when executed as the main program. This is a fundamental concept in Python scripting and programming.

Code as discussed in the video:

# Python Recursive functions and main function
'''
-> Recursive function: a function that calls itself
-> 2 parts: base case, Recursive case
'''
# factorial : 5! = 5 * 4 * 3 * 2 * 1 = 120
# 0! = 1! = 1
# recursive function to find the factorial 

# def factorial(n):
#     # base case
#     if n == 0 or n == 1:
#         return 1
#     # recursive case
#     else:
#         return n * factorial(n - 1)

# output = factorial(10)
# print("The factorial of 10 is: ", output)

# main functions
def factorial(n):
    if n == 0 or n == 1:
        return 1
    else:
        return n * factorial(n - 1)

def mainFunc():
    num = int(input("Enter a number: "))
    output = factorial(num)
    print(f"The factorial of {num} is:", output)

# main part
if __name__ == "__main__":
    mainFunc()

You can copy and run this code