简体   繁体   English

判断一个数是否能被25整除,Python

[英]Determining if a number evenly divides by 25, Python

I'm trying to check if each number in a list is evenly divisible by 25 using Python. I'm not sure what is the right process.我正在尝试使用 Python 检查列表中的每个数字是否可以被 25 整除。我不确定什么是正确的过程。 I want to do something like this:我想做这样的事情:

n = [100, 101, 102, 125, 355, 275, 435, 134, 78, 550]
for row in rows:

    if n / 25 == an evenly divisble number:
        row.STATUS = "Major"
    else:
        row.STATUS = "Minor"

Any suggestions are welcome.欢迎提出任何建议。

Use the modulo operator :使用模运算符

for row in rows:
    if n % 25:
        row.STATUS = "Minor"
    else:
        row.STATUS = "Major"

or要么

for row in rows:
    row.STATUS = "Minor" if n % 25 else "Major"

n % 25 means "Give me the remainder when n is divided by 25 ". n % 25的意思是“给我n除以25的余数”。

Since 0 is False y, you don't need to explicitly compare to 0 , just use it directly in the if -- if the remainder is 0 , then it's a major row.由于0False y,您不需要明确地与0进行比较,只需在if中直接使用它——如果余数是0 ,那么它就是一个主要行。 If it's not, it's a minor row.如果不是,那就是次要行。

Use the modulo operator to determine the division remainder:使用取模运算符确定除法余数:

if n % 25 == 0:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM