[Part V]DocTests in Python🐍

[Part V]DocTests in Python🐍

Applying knowledge of functions and getting to know how to use doctests to make debugging easy

Welcome back to the series "Learn Python with me!"👋👋

There are times when it seems as if everything important is happening in the same week. Prioritizing is an essential skill that one must-have. After almost a week of grueling hard work, I finally have the time to write this blog, so let's get started.

We will be applying our knowledge of functions in Python today and learn about a very cool feature.

Printing the factorial of the first nine whole numbers(0-9).

Factorial is basically the product of the integer and all the integers below it(till 1).

For example,

5! = 5 * 4 * 3 * 2 * 1 = 120

0! = 1

1! = 1

If we generalise this, for a whole number n ,

n! = n * (n-1) * (n-2) * ...... * 3 * 2 * 1

Let's think about the code now.

✔We know that we need the first nine whole numbers. We will require loops to generate these. Since we already know how many times we need the loop to run we can decide on a for loop.

✔We want to write clean code. One of the ways to do this is writing functions. We could use a function that gets called for every number generated.

Let's see how our code looks until this point of our discussion.

def main():
    #generating numbers 0-9
    for i in range(10):
        print(str(i)+ "! =" + str(factorial(i)))


def factorial(num):
    #generates the factorial for the number passed in from the main function


if __name__ == '__main__':
    main()

How do we find the factorial of the number? We basically have to get the product of all the numbers from 1 to num, which will give the factorial of the number.

✔Generating numbers from 1 to num will require a for loop. ✔We will also require a variable that will store our product.

Let's see how our code looks.

def main():
    #generating numbers 0-9
    for i in range(10):
        print(str(i)+ "! =" + str(factorial(i)))


def factorial(num):
    #generates the factorial for the number passed in from the main function
    fact = 1
    for i in range(1,num+1):
        fact = fact * i
    return fact


if __name__ == '__main__':
    main()

Before running this program we can make it a little better.

In this program, we know clearly how many numbers we need to generate, that number is a constant. Hence, we can declare it outside the functions.

Did you know that we can run checks within our Python script?

This feature works great when we want to debug our program. Writing checks along the way will show us exactly where we are going wrong.

A doctest is a special command that is written in the comment at the start of a function definition.

In this case, we can write the doctest like this👇

def factorial(num):
    """
    generates the factorial for the number passed in from the main function
    Doctest:
    >>> factorial(5)
    120
    >>> factorial(4)
    24
    >>> factorial(2)
    2
    >>> factorial(0)
    1
   """
    fact = 1
    for i in range(1,num+1):
        fact = fact * i
    return fact

The test is written in after >>>, below which the answer is given.

You can doctests in your terminal with the following command:

python -m doctest <.py filename> -v

Let's get the final program👇

MAX_NUM = 9
def main():
    #generating numbers 0-9
    for i in range(MAX_NUM+1):
        print(str(i)+ "! =" + str(factorial(i)))


def factorial(num):
    """
    generates the factorial for the number passed in from the main function
    Doctest:
    >>> factorial(5)
    120
    >>> factorial(4)
    24
    >>> factorial(2)
    2
    >>> factorial(0)
    1
   """
    fact = 1
    for i in range(1,num+1):
        fact = fact * i
    return fact


if __name__ == '__main__':
    main()

We will first run the doctest and check if all our tests are passing and then proceed towards running our program.

factorial_black.gif

Congratulations!! We passed all our tests!🥳🎉

We practiced functions today and learned about doctests and passed with flying colors! As always, feel free to ask questions in the comments section below!

Happy learning!