Python Forum
Detect if an integer ends in 0 or 5
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Detect if an integer ends in 0 or 5
#1
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
Reply
#2
>>> n = '125'
>>> n.endswith('5')
True
Reply
#3
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']
Reply
#4
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
Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pywin32: Outlook connection ends with 'operation aborted' on one machine tstone 0 2,372 May-03-2022, 04:29 AM
Last Post: tstone
  Regex: a string does not starts and ends with the same character Melcu54 5 2,402 Jul-04-2021, 07:51 PM
Last Post: Melcu54
  threadlocals are garbage collected before thread ends akv1597 0 1,794 Mar-09-2021, 12:13 PM
Last Post: akv1597
  Running a few lines of code as soon as my timer ends nethatar 3 2,397 Feb-26-2021, 01:02 PM
Last Post: jefsummers
  Writing to file ends incorrectly project_science 4 2,680 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Keep Application running after Python script ends PEGylated_User 0 1,970 Nov-12-2020, 03:27 PM
Last Post: PEGylated_User
  Script ends, does not start again phanegem 8 5,073 Mar-30-2017, 07:36 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020