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程序设计有所帮助。

相关文章

Python3.7 dataclass使用指南小结

dataclass简介 dataclass的定义位于PEP-557,根据定义一个dataclass是指“一个带有默认值的可变的namedtuple”,广义的定义就是有一个类,它的属性均可...

wxPython 入门教程

wxPython 入门教程

这篇文章是关于 wxPython,但 wxPython 实际是两件事物的组合体:Python 脚本语言和 GUI 功能的 wxWindows 库(关于 wxWindows 的介绍,请参阅...

python matplotlib 在指定的两个点之间连线方法

python matplotlib 在指定的两个点之间连线方法

为了找到matplotlib在两个点之间连线的方法真是费了好大功夫,最后还是决定用简单的 plt.plot 来解决。如果有好多对点,则可以通过循环实现连接,还可以用 plt.arrow...

Python中__init__和__new__的区别详解

__init__ 方法是什么? 使用Python写过面向对象的代码的同学,可能对 __init__ 方法已经非常熟悉了,__init__ 方法通常用在初始化一个类实例的时候。例如:...

python去掉行尾的换行符方法

如下所示: mystring.strip().replace(' ', '').replace('\n', '').replace('\t', '').replace('\r', '')...