Validate Credit Card Number Using Luhn Algorithm

Published August 25, 2025

We will write a Python program to check the validity of a credit card number using Luhn Algorithm.

Find the sum of even numbers in a Python list Check the validity of a credit card number using Python

What is the Luhn Algorithm?

The Luhn Algorithm is a verification algorithm used to validate various numbers, including credit card numbers.

The way it works is it calculates a control digit, called checksum, from a number with several digits. Using this checksum, we verify that the number is correct (because the key is a number which is dependent on the others).

This algorithm was invented by an IBM engineer, Hans Peter Luhn, in 1954 and is used for all major credit company companies including American Express, Mastercard, VISA card, etc, and also in various data processing systems.

How to verify the credit card number

Step 1) Loop through the digits of the credit card number from right to left. Starting with the rightmost digit, double the value of every second digit. If the doubled value is great than 9, add the digits. You can also subtract 9 from the doubled value greater than 9.

Step 2) Add all the digits in the new number you just created in Step 1.

Step 3) Multiply the sum found in Step 2 with 9. The rightmost digit, or units digit, is the check digit. You can find the rightmost digit by performing a modulo 10 operation. This check digit is also the Luhn checksum.

Step 4) If the check digit number equals to 0, then the number is valid, according to the Luhn algorithm.

How to verify a credit card number with examples

Let us verify this American Express credit card number 370419175982879.

Put each digit in its own box

3 7 0 4 1 9 1 7 5 9 8 2 8 7 9

Reverse the digits of this number

9 7 8 2 8 9 5 7 1 9 1 4 0 7 3

Double every other digit beginning with the second one

9 14 8 4 8 18 5 14 1 18 1 8 0 14 3

If the number in each box is is greater 9, subtract 9 from it

9 5 8 4 8 9 5 5 1 9 1 8 0 5 3

Add all the digits

9 + 5 + 8 + 4 + 8 + 9 + 5 + 5 + 1 + 9 + 1 + 8 + 0 + 5 + 3 = 80

The sum of all these numbers is 80.

Since 80 is divisible by 10, or ends in 0, this is a valid credit card number.

Python program to verify if a number is a valid credit card number

Let us translate this into a working Python program.

#!env python
# Python program to verify if a credit card number is valid
# https://aruljohn.com/blog/luhn-algorithm-validate-credit-card-number/

def verify_credit_card_number(cc_number):
    # Reverse the digits and convert it to a list of integers
    cc_number = [int(x) for x in list(cc_number[::-1])]
    total = 0

    for i, x in enumerate(cc_number):
        # Double every other digit beginning with the second digit
        if i % 2 == 1:
            x = x * 2

        # If current number is greater than 9, subtract 9 from it
        if x > 9:
            x -= 9

        # Add to total
        total += x

    # If total is a multiple of 10, return True
    if total % 10 == 0:
        return True

    # Or else, it is an invalid number
    return False

# Main
if __name__ == '__main__':
    credit_card_number = '4556885842081063' # Visa credit card number
    print(f'Verifying credit card number {credit_card_number}')
    if (verify_credit_card_number(credit_card_number)):
        print('✅ This credit card number is valid.')
    else:
        print('❌ This credit card number is invalid.')

Output:

$ python creditcardverification.py 
Verifying credit card number 4556885842081063
✅ This credit card number is valid.

Conclusion

Have you used any of these methods in an interview or school assignment? Let me know what worked and didn't work for you in the comments below.

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Last Updated: August 25, 2025.     This post was originally written on August 21, 2025.