luhn algorithm or luhn formula is a simple checksum formula used to validate a variety of identification numbers such as credit card numbers imei numbers national provider identifier numbers in some of the countries it takes a number as an input assuming cardnumber as a string and returns true or false based upon whether number is valid or not param cardnumber return bool valid or not examples checkluhn950123440000 false checkluhn490154203237518 true luhn algorithm or luhn formula is a simple checksum formula used to validate a variety of identification numbers such as credit card numbers imei numbers national provider identifier numbers in some of the countries it takes a number as an input assuming cardnumber as a string and returns true or false based upon whether number is valid or not param card_number return bool valid or not examples check_luhn 950123440000 false check_luhn 490154203237518 true
def check_luhn(card_number):
    
    card_len = len(card_number)

    check_sum = 0

    is_parity = False

    for digit in range(card_len - 1, -1, -1):
        if is_parity:
            cal = int(card_number[digit]) * 2
        else:
            cal = int(card_number[digit])

        if cal > 9:
            check_sum += cal - 9
        else:
            check_sum += cal

        is_parity = not is_parity

    return check_sum % 10 == 0


if __name__ == "__main__":
    import doctest

    doctest.testmod()