Python3通过Luhn算法快速验证信用卡卡号的方法

yipeiwu_com6年前Python基础

本文实例讲述了Python3通过Luhn算法快速验证信用卡卡号的方法。分享给大家供大家参考。具体分析如下:

Python3通过Luhn算法快速验证信用卡卡号,python用起来就是爽,很简单的三行代码就可以验证信用卡卡号是否有效

def luhn_check(num):
  ''' Number - List of reversed digits '''
  digits = [int(x) for x in reversed(str(num))]
  check_sum = sum(digits[::2]) + sum((dig//10 + dig%10) for dig in [2*el for el in digits[1::2]])
  return check_sum%10 == 0
if __name__ == "__main__":
  print(luhn_check(543298376))

希望本文所述对大家的Python3程序设计有所帮助。

相关文章

Python之pandas读写文件乱码的解决方法

python读写文件有时候会出现   ‘XXX'编码不能打开XXX什么的,用记事本打开要读取的文件,另存为UTF-8编码,然后再用py去读应该可以了。如果还不行,那...

Python tensorflow实现mnist手写数字识别示例【非卷积与卷积实现】

本文实例讲述了Python tensorflow实现mnist手写数字识别。分享给大家供大家参考,具体如下: 非卷积实现 import tensorflow as tf from t...

flask入门之表单的实现

一、原生表单 form.html {% extends 'common/base.html' %} {% block title %} 原生表单 {% endblock %}...

pandas 获取季度,月度,年度首尾日期的方法

可实现类似于sql中的dateadd、datesub的功能 两种获取日期的方式 z=datetime.datetime(2016,12,5) z=datetime.datetime....

Python实现ping指定IP的示例

Python实现ping指定IP的示例

贴代码: import os import sys iplist = list() ip = '192.168.1.11' # ip = '172.24.186.191'...