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
#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
Quote
#2
>>> n = '125'
>>> n.endswith('5')
True
Quote
#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']
metulburr and micseydel like this post
Quote
#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
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  datetime.date() - TypeError: an integer is required (got type str) bisoftware 7 303 Jan-30-2018, 08:05 PM
Last Post: buran
  About generating N integer numbers without repetition Otbredbaron 3 118 Jan-30-2018, 12:08 PM
Last Post: Otbredbaron
  Detect comments part using web scraping seco 7 126 Jan-18-2018, 10:06 PM
Last Post: seco
  cannot convert float infinity to integer error despite rounding off floats Afterdarkreader 3 200 Dec-15-2017, 02:01 AM
Last Post: micseydel
  discord bot (detect when someone types certain words) kikoko_2 1 425 Dec-11-2017, 09:04 PM
Last Post: nilamo
  Can a dictionary key be an integer? Pedroski55 7 401 Oct-05-2017, 05:54 AM
Last Post: buran
  How to convert character to integer in python rajeev1729 2 274 Oct-03-2017, 04:30 PM
Last Post: nilamo
  How to map character to integer in python rajeev1729 1 210 Oct-02-2017, 01:41 PM
Last Post: buran
  Integer Required for psspy function mikerosz94 0 236 Aug-31-2017, 06:42 PM
Last Post: mikerosz94
  Convert integer to string Maigol 4 506 Aug-22-2017, 08:27 PM
Last Post: Maigol

Forum Jump:


Users browsing this thread: 1 Guest(s)