Checking if a number is divisible by 2-11 I am writing a Python class which checks if a given number is divisible by numbers 2-11. I am not using the __init__ method which in my opinion is not necessary in this case.

Questions:


How can I convert a given number in to abs(x) without using the __init__ method in every method at once?
How can I check if a number is divisible by 7 without using a recursive solution?




class Divisibility:
    """
    Program checks if given number is divisible
    by any number from 2 to 11
    """

    # Divisibe by 2 if last digit is divisible by 2
    def divisible_by_2(self, number):
        number = abs(number)
        if (number % 10) % 2 == 0:
            return True
        else:
            return False

    # Divisible by 3 if sum of its digits is divisible by 3 
    def divisible_by_3(self, number):
        number = abs(number)
        n = number

        s = 0
        while n:
            s, n = s + n % 10, n // 10

        if s % 3 == 0:
            return True
        else:
            return False

    # Divisible by 4 if last two digits is divisible by 4
    def divisible_by_4(self, number):
        number = abs(number)
        if (number % 100) % 4 == 0:
            return True
        else:
            return False

    # Divisible by 5 if last digit is either 5 or 0
    def divisible_by_5(self, number):
        number = abs(number)
        if number % 10 == 0 or number % 10 == 5:
            return True
        else:
            return False

    # Divisible by 6 if number is divisible by 2 and 3
    def divisible_by_6(self, number):
        number = abs(number)
        if self.divisible_by_2(number) and self.divisible_by_3(number):
            return True
        else:
            return False


    def divisible_by_7(self, number):
        number = abs(number)
        if number % 7 == 0:
            return True
        else:
            return False

    # Divisible by 8 if last three digits is divisible by 8
    def divisible_by_8(self, number):
        number = abs(number)
        if (number % 1000) % 8 == 0:
            return True
        else:
            return False

    # Divisible by 9 if the sum of its digits is divisible by 9
    def divisible_by_9(self, number):
        number = abs(number)
        s = 0
        while number:
            s, number = s + number % 10, number // 10

        if s % 3 == 0:
            return True
        else:
            return False

    # Divisible by 10 if last digit is 0
    def divisible_by_10(self, number):
        number = abs(number)
        if number % 10 == 0:
            return True
        else:
            return False

    # Divisible by 11 if the difference between
    # the sum of numbers at even possitions and odd
    # possitions is divisible by 11
    def divisible_by_11(self, number):
        number = abs(number)
        n = number
        nn = number // 10
        total = 0

        while n:
            total += n % 10
            n //= 100

        while nn:
            total -= nn % 10
            nn //= 100

        if abs(total) % 11 == 0:
            return True
        else:
            return False

if __name__ == '__main__':

    D = Divisibility()
    print(D.divisible_by_2(-6))
    print(D.divisible_by_3(6))
    print(D.divisible_by_4(6))
    print(D.divisible_by_5(60))
    print(D.divisible_by_6(6))
    print(D.divisible_by_7(616))
    print(D.divisible_by_8(82453))
    print(D.divisible_by_9(9512244))
    print(D.divisible_by_10(-571288441))
    print(D.divisible_by_11(121))
You do this quite alot

if s % 3 == 0:
    return True
else:
    return False


Instead you could return directly

return s % 3 == 0