Advanced Exercises: Functions in Python

  1. Factorial Function:

    Write a Python function to calculate the factorial of a given non-negative integer using recursion. The factorial of a non-negative integer n is the product of all positive integers less than or equal to n.

                    Input: 5
                    Output: 5! = 5 * 4 * 3 * 2 * 1 = 120
    
                    Input: 0
                    Output: 0! = 1
                
  2. Prime Number Generator:

    Write a Python function that generates a list of prime numbers in a given range (start and end). The function should return the list of prime numbers found in that range.

                    Input: start = 10, end = 30
                    Output: [11, 13, 17, 19, 23, 29]
                
  3. Area of Shapes:

    Write a Python function that calculates the area of different shapes (circle, rectangle, triangle) based on user input. The function should take the shape and relevant parameters as input and return the area of the shape.

                    Input: shape = "circle", radius = 5
                    Output: Area of circle with radius 5 is 78.54
    
                    Input: shape = "rectangle", length = 8, width = 6
                    Output: Area of rectangle with length 8 and width 6 is 48
    
                    Input: shape = "triangle", base = 10, height = 7
                    Output: Area of triangle with base 10 and height 7 is 35
                
  4. Greatest Common Divisor (GCD) Function:

    Write a Python function to find the Greatest Common Divisor (GCD) of two positive integers using Euclidean Algorithm. The GCD is the largest positive integer that divides both numbers without leaving a remainder.

                    Input: num1 = 24, num2 = 36
                    Output: GCD of 24 and 36 is 12
    
                    Input: num1 = 45, num2 = 60
                    Output: GCD of 45 and 60 is 15
                
  5. FizzBuzz Function:

    Write a Python function that prints numbers from 1 to n. For multiples of 3, print "Fizz" instead of the number, and for multiples of 5, print "Buzz" instead of the number. For numbers that are multiples of both 3 and 5, print "FizzBuzz".

                    Input: n = 15
                    Output: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz
                

Using a Function in Another Function YouTube Video