在 Python 中检查一个数字是否是 10 的倍数
使用模 %
运算符来检查一个数字是否是 10 的倍数,例如 if 100 % 10 == 0:
。 模 %
运算符返回第一个数字除以第二个数字的余数。 如果余数为 0,则该数是 10 的倍数。
if 100 % 10 == 0:
print('number is multiple of 10 ✅')
else:
print('number is NOT multiple of 10 ✅')
if 123 % 10 != 0:
print('number is not multiple of 10 ✅')
我们使用模 %
运算符来检查一个数字是否是 10 的倍数。
模 %
运算符返回第一个值除以第二个值的余数。
print(100 % 10) # 👉️ 0
print(123 % 10) # 👉️ 3
如果除法没有余数,则第一个数字是第二个数字的精确倍数。
print(150 % 10) # 👉️ 0
print(150 % 10 == 0) # 👉️ True
10 是 150 的整数倍,所以 150 可以被 10 整除,余数为 0。
如果我们需要检查一个数字是否不能被 10 整除,请使用带不等于
!=
符号的模%
运算符,例如if 123 % 10 != 0:
。
print(123 % 10) # 👉️ 3
if 123 % 10 != 0:
print('number is not multiple of 10 ✅')
10 不是 123 的精确倍数,因此将 123 除以 10 得到余数 3。
这是一个示例,它从用户输入中获取一个数字并检查它是否是 10 的倍数。
num = int(input('Enter a number: '))
print(num) # 👉️ 10
if num % 10 == 0:
print('number is multiple of 10')
输入函数接受一个可选的提示参数并将其写入标准输出,而没有尾随的换行符。
请注意
,我们使用int()
类将输入字符串转换为整数。
然后该函数从输入中读取该行,将其转换为字符串并返回结果。
即使用户输入了一个数字,它仍然会被转换为一个字符串。
如果我们需要检查一个数字是否是两个或多个其他数字的倍数,请使用 and 运算符。
num = 30
if num % 10 == 0 and num % 15 == 0:
print('30 is multiple of 10 and 15 ✅')
表达式 x 和 y 如果为假则返回左边的值,否则返回右边的值。
if
块仅在两个条件都为 True 时运行。
相反,如果我们需要检查一个数字是否可以被多个数字中的 1 整除,请使用 or
运算符。
num = 30
if num % 13 == 0 or num % 10 == 0:
print('30 is divisible by 13 or 10')
表达式 x 或 y 如果为真则返回左边的值,否则返回右边的值。
如果任一条件的计算结果为 True,则运行 if 块。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
如何在 JavaScript 中合并两个数组而不出现重复的情况
发布时间:2024/03/23 浏览次数:86 分类:JavaScript
-
本教程介绍了如何在 JavaScript 中合并两个数组,以及如何删除任何重复的数组。