Credit card number validation using Luhn’s algorithm in python

Allwin Raju
3 min readDec 8, 2020

In this blog post, we shall how to validate a credit card number using Luhn’s algorithm. We shall also learn about Luhn’s algorithm where and how it is used. This blog post also contains a sample python code to validate the credit card number.

Photo by Avery Evans on Unsplash

Luhn’s algorithm

The Luhn algorithm or Luhn formula, also known as the “modulus 10” or “mod 10” algorithm, named after its creator, IBM scientist Hans Peter Luhn, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers, etc.

The algorithm is in the public domain and is in wide use today. It is specified in ISO/IEC 7812–1. It is not intended to be a cryptographically secure hash function; it was designed to protect against accidental errors, not malicious attacks. Most credit cards and many government identification numbers use the algorithm as a simple method of distinguishing valid numbers from mistyped or otherwise incorrect numbers.

How does it work?

Luhn’s algorithm consists of the following three steps. They are,

  1. Starting from the rightmost digit, double every second digit.

--

--

No responses yet