Determining if a number evenly divides by 25, Python

2024/10/6 16:21:49

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.

Answer

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.

https://en.xdnf.cn/q/70352.html

Related Q&A

OpenCV SimpleBlobDetector detect() call throws cv2.error: Unknown C++ exception from OpenCV code?

I need to detect semicircles on image and I find follow stuff for this: import cv2 import numpy as npdef get_circle(img_path):im = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)detector = cv2.SimpleBlobDet…

Is it possible to add a global argument for all subcommands in Click based interfaces?

I am using Click under a virtualenv and use the entry_point directive in setuptools to map the root to a function called dispatch.My tool exposes two subcommands serve and config, I am using an option …

Error importing h5py

Ive been trying to import h5py to read this type of file.Here is my code:import h5pyfile_1 = h5py.File("Out_fragment.h5py")print file_1The output is:Traceback (most recent call last):File &qu…

Tensorflow leaks 1280 bytes with each session opened and closed?

It seems that each Tensorflow session I open and close consumes 1280 bytes from the GPU memory, which are not released until the python kernel is terminated. To reproduce, save the following python scr…

Python Pandas -- Forward filling entire rows with value of one previous column

New to pandas development. How do I forward fill a DataFrame with the value contained in one previously seen column?Self-contained example:import pandas as pd import numpy as np O = [1, np.nan, 5, np.…

Microsoft Visual C++ 14.0 is required - error - pip install fbprophet

I am trying pip install fbprophet. I am getting that error: "Microsoft Visual C++ 14.0 is required" It has been discussed many times (e.g. Microsoft Visual C++ 14.0 is required (Unable to fin…

find position of item in for loop over a sequence [duplicate]

This question already has answers here:Closed 12 years ago.Possible Duplicate:Accessing the index in Python for loops list = [1,2,2,3,5,5,6,7]for item in mylist:...How can I find the index of the item…

How to convert BeautifulSoup.ResultSet to string

So I parsed a html page with .findAll (BeautifulSoup) to variable named result. If I type result in Python shell then press Enter, I see normal text as expected, but as I wanted to postprocess this res…

How can I manually place networkx nodes using the mouse?

I have a fairly large and messy network of nodes that I wish to display as neatly as possible. This is how its currently being displayed:First, I tried playing with the layout to see if it could genera…

How to create a vertical scroll bar with Plotly?

I would like to create a vertical scroll for a line chart in Plotly. For visualisation, the vertical scroll is something depicted in the figure below.Assume, we have 6 line chart as below, then how ca…