Is there a way to get the last digit of a number. I am trying to find variables that end with "1" like 1,11,21,31,41,etc..
If I use a text variable I can simply put
print number[:-1]
but it works for variables with text[like "hello] but not with numbers. With numbers I get this error:
TypeError: 'int' object is not subscriptable
I am trying to see if there's a better way to deal with numbers this way. I know a solution is to convert to a string and then do the above command but I'm trying to see if there's another way I have missed.
Thanks so much in advance...
asked Mar 10, 2011 at 2:36
Remainder when dividing by 10, as in
numericVariable % 10
This only works for positive numbers. -12%10 yields 8
answered Mar 10, 2011 at 2:36
Jim GarrisonJim Garrison
84.3k20 gold badges153 silver badges187 bronze badges
0
Use the modulus operator with 10:
num = 11
if num % 10 == 1:
print 'Whee!'
This gives the remainder when dividing by 10, which will always be the last digit [when the number is positive].
answered Mar 10, 2011 at 2:37
CameronCameron
93.4k21 gold badges194 silver badges221 bronze badges
2
So you want to access the digits in a integer like elements in a list; easiest way I can think of is:
n = 56789
lastdigit = int[repr[n][-1]]
# > 9
Convert n into a string, accessing last element then use int constructor to convert back into integer.
For a Floating point number:
n = 179.123
fstr = repr[n]
signif_digits, fract_digits = fstr.split['.']
# > ['179', '123']
signif_lastdigit = int[signif_digits[-1]]
# > 9
answered Oct 17, 2011 at 5:07
Zv_oDDZv_oDD
1,7701 gold badge17 silver badges26 bronze badges
1
I can't add a comment yet, but I wanted to iterate and expand on what Jim Garrison said
Remainder when dividing by 10, as in numericVariable % 10
This only works for positive numbers. -12%10 yields 8
While modulus [%] is working as intended, throw on an absolute value to use the modulus in this situation.
abs[numericVariable] % 10
answered Feb 21, 2017 at 21:19
Micah PearceMicah Pearce
1,7213 gold badges24 silver badges55 bronze badges
Lostsoul, this should work:
number = int[10]
#The variable number can also be a float or double, and I think it should still work.
lastDigit = int[repr[number][-1]]
#This gives the last digit of the variable "number."
if lastDigit == 1 :
print["The number ends in 1!"]
Instead of the print statement at the end, you can add code to do what you need to with numbers ending in 1.
Hope it helped!
answered Feb 3, 2019 at 3:37
Convert to string first:
oldint = 10101
newint = int[str[oldint][-1:]]
answered Dec 15, 2019 at 3:05
JayJay
1,2412 gold badges10 silver badges22 bronze badges
The simplest and most efficient way is to use the reminder :
last_digit = orginal_number % 10
answered Aug 14, 2020 at 17:37
1
This is a simple yet effective way to do it
if number < 0:
remainder = number % -10
else:
remainder = number % 10
answered Jun 28 at 10:29
1
By using iteration and the in built function of 'digit' the number is treated as binary and so it goes from backwards to forwards. Here is an example of a bit of code for you.
for digit in binary:
denary= denary*2 + int[digit]
answered Nov 5, 2017 at 15:02