Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Detect if an integer ends in 0 or 5
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
>>> n = '125'
>>> n.endswith('5')
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']
micseydel and metulburr like this post
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
my_integers = [1,20,32,145,210,103]

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

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  how to detect \x in string so it can be removed azimmermann 3 92 Jun-12-2017, 10:08 PM
Last Post: wavic
  mysql.connector - incorrect integer value Milo 2 137 May-28-2017, 10:25 AM
Last Post: Milo
  xml to integer kendias 22 950 May-05-2017, 09:11 PM
Last Post: kendias
  How to find any non positive integer noob 3 214 Apr-27-2017, 04:46 PM
Last Post: Ofnuts
  why does integer input give error invalid literal for int() PyPhanman 2 149 Apr-27-2017, 05:32 AM
Last Post: ndc85430
  Script ends, does not start again phanegem 8 326 Mar-30-2017, 07:36 PM
Last Post: nilamo
  Error "an integer is required" in getpass.getuser(). Nirelg 5 344 Mar-20-2017, 04:19 AM
Last Post: nilamo
  moviepy can't detect ImageMagick after I installed it RobertoChen 1 479 Feb-03-2017, 02:09 AM
Last Post: Skaperen
  detect Multiple faces using dlib vijaysagi 1 328 Jan-18-2017, 10:25 AM
Last Post: wavic
  Logic to convert an integer to YEAR / MONTH hugobaur 9 814 Oct-18-2016, 11:58 AM
Last Post: hugobaur

Forum Jump:

Users browsing this thread: 1 Guest(s)