Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining if a number evenly divides by 25, Python

Tags:

python

math

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. 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.

like image 813
Mike Avatar asked Apr 04 '12 20:04

Mike


People also ask

How do you check if something divides evenly in Python?

In Python, the remainder operator (“%”) is used to check the divisibility of a number with 5. If the number%5 == 0, then it will be divisible.

How do you check if a number is evenly divided?

If, after adding up the sum of all the component parts of a number which comes out to another two digit or bigger number the sum is exposed, take that number and add it's component parts. (Take for example 189: 1+8+9=27...if you then take 2+7 you will get 9. Therefore, 189 is evenly divisible by 9.)

How do you know if a Python is divisible by 7?

if cnt % 7 = = 0 and cnt % 5 = = 0 : print (cnt, " is divisible by 7 and 5." ) Output: 35 is divisible by 7 and 5.

What does it mean if a number divides evenly?

evenly divisible (not comparable) (arithmetic) Leaving no remainder when divided by. 15 is evenly divisible by 3, but 16 isn't.


1 Answers

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".

Since 0 is Falsey, 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. If it's not, it's a minor row.

like image 73
agf Avatar answered Oct 24 '22 02:10

agf