226k views
5 votes
Write a function named last_digit that returns the last digit of an integer. For example, last_digit(3572) should return 2. It should work for negative numbers as well; last_digit(-947) should return 7.

User Akhtar
by
4.7k points

1 Answer

2 votes

def last_digit(num):

num = str(num)

return int(num[-1])

print(last_digit(-947))

I hope this helps!

User Brooke
by
4.9k points