Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

What is Range and Recursive Function in Python?

Range and Recursive Function

Condition for Range and Recursive Function in Python

  • Range: It is used to generate a sequence of numbers, commonly used in for-loops to iterate over a specific range of values.
  • Recursive function: It solves a problem by solving smaller instances of the same problem, with a base case to stop the recursion.
Step-by-Step Process
  • Range: It generates a sequence of numbers, starting from start (default 0) to stop (exclusive),with an optional step value (default 1).It returns an immutable sequence (iterator), which is memory efficient.
  • Recursive function: A function that calls itself .
  • Base Case: The condition that stops the recursion and prevents it from running indefinitely.
  • Recursive Case: The part of the function where it calls itself with modified arguments to solve smaller subproblems.
Sample Code
  • #Range
    print("Output for range:")
    for i in range(3):
    print(i)
    print()
    print("Output for range with step value:")
    # Using range with start, stop, and step
    for i in range(1, 10, 2):
      print(i)
    print()
    #Recursion
    print("Output for recursion:")
    def sum_of_numbers(n):
    if n == 1:
      return 1
    else:
      return n + sum_of_numbers(n - 1)
    result = sum_of_numbers(5)
    print(result)
Screenshots
  • Range and Recursive Function1