check all the factors of a certain number in python

To find all the factors of a certain number in python, we can use a loop to iterate through all the possible factors. A factor is a number that can divide the given number without leaving a remainder. We start the loop from 1 and go till the given number. If any number completely divides the given number, then it is a factor of the given number. Here's the code:

main.py
def find_factors(num):
    """
    This function finds all the factors of the given number
    Args:
    num - The number whose factors need to be found
    
    Returns:
    A list of all the factors of the given number
    """
    
    factors = []
    
    for i in range(1, num+1):
        if num % i == 0:
            factors.append(i)
            
    return factors
373 chars
18 lines

We can then call this function and pass the number as the argument. The function will return a list of all the factors of the given number. For example, let's find all the factors of 36:

main.py
>>> print(find_factors(36))
[1, 2, 3, 4, 6, 9, 12, 18, 36]
59 chars
3 lines

This means that 1, 2, 3, 4, 6, 9, 12, 18 and 36 are all factors of 36.

gistlibby LogSnag