Detect if an integer ends in 0 or 5 - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Detect if an integer ends in 0 or 5 (/thread-1547.html) |
Detect if an integer ends in 0 or 5 - birdieman - Jan-11-2017 I am new to Python. How do I determine if a variable (it will always be a two or three digit integer) ends either in a '0' or a '5'. If it does, I want to print it (I know how to do the print part). thanks for looking RE: Detect if an integer ends in 0 or 5 - snippsat - Jan-11-2017 >>> n = '125' >>> n.endswith('5') True RE: Detect if an integer ends in 0 or 5 - buran - Jan-11-2017 if it is of type int - check that reminder of division by 5 is 0. if it is str - chek the last char to be in ['0','5'] RE: Detect if an integer ends in 0 or 5 - birdieman - Jan-11-2017 Snippsat -- my variable will always be a two or three digit integer -- will your solution work? (I tried it with my variable and received a "Attribut Error": 'int' object has no attribute 'endswith'. buran - will give your solution a shot -- once I go back to manual and figure out how to check for a remainder. thanks to both for responding Burman -- got it --- thanks RE: Detect if an integer ends in 0 or 5 - buran - Jan-11-2017 my_integers = [1,20,32,145,210,103] #loop for num in my_integers: if not num%5: print num #list comprehension nums_ending05 = [n for n in my_integers if not n%5] print nums_ending05 |