# Python代码阅读（第63篇）：数字奇偶性

InfoQ 2021-11-25 16:11:30

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Python 代码阅读合集介绍：","attrs":{}},{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/s/7RqixavOlghqrTnr5GFeeA","title":"","type":null},"content":[{"type":"text","text":"为什么不推荐Python初学者直接看项目源码","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本篇阅读的代码实现了判断数字的奇偶性质，以及扩展判断一个数字能否被另一个数字整除的功能。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本篇阅读的代码片段来自于","attrs":{}},{"type":"link","attrs":{"href":"https://github.com/30-seconds/30-seconds-of-python","title":"","type":null},"content":[{"type":"text","text":"30-seconds-of-python","attrs":{}}]},{"type":"text","text":"。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"codeinline","content":[{"type":"text","text":"is_odd","attrs":{}}],"attrs":{}}]},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"def is_odd(num):\n return num % 2 != 0\n\n# EXAMPLES\nis_odd(3) # True\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"is_odd","attrs":{}}],"attrs":{}},{"type":"text","text":"函数接收一个数字，返回这个数字是否是奇数。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"函数使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"%","attrs":{}}],"attrs":{}},{"type":"text","text":"运算符进行取余计算。奇数无法被","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"2","attrs":{}}],"attrs":{}},{"type":"text","text":"整除，所以使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"%","attrs":{}}],"attrs":{}},{"type":"text","text":"取余不等于","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"0","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"codeinline","content":[{"type":"text","text":"is_even","attrs":{}}],"attrs":{}}]},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"def is_even(num):\n return num % 2 == 0\n\n# EXAMPLES\nis_even(3) # False\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"is_even","attrs":{}}],"attrs":{}},{"type":"text","text":"函数和","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"is_odd","attrs":{}}],"attrs":{}},{"type":"text","text":"函数类似，使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"%","attrs":{}}],"attrs":{}},{"type":"text","text":"判断是否可以被","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"2","attrs":{}}],"attrs":{}},{"type":"text","text":"整除。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"codeinline","content":[{"type":"text","text":"is_divisible","attrs":{}}],"attrs":{}}]},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"def is_divisible(dividend, divisor):\n return dividend % divisor == 0\n\n# EXAMPLES\nis_divisible(6, 3) # True\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"is_divisible","attrs":{}}],"attrs":{}},{"type":"text","text":"函数扩展了前两个函数，使用指定的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"divisor","attrs":{}}],"attrs":{}},{"type":"text","text":"进行取余计算。判断","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"dividend","attrs":{}}],"attrs":{}},{"type":"text","text":"能否被","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"divisor","attrs":{}}],"attrs":{}},{"type":"text","text":"整除。","attrs":{}}]}]}

https://xie.infoq.cn/article/24dd67581ab92b784706ede3a