Recursive Function

  • Recursive functions are used for computations
  • Recursive functions call onto dictionaries
  • Dictionaries are a sequence of variables that functions can pull from.
def factorial(x):
    """This is a recursive function
    to find the factorial of an integer"""

    if x == 1:
        return 1
    else:
        return (x * factorial(x-1))


num = 5
print("The factorial of", num, "is", factorial(num))
The factorial of 5 is 120

While Functions

  • The while loop is used to repeat a section of code an unknown number of times until a specific condition is met
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
The sum is 55