How to Get the Last Digit of a Number in Python

AuthorSumit Dey Sarkar

Pubish Date11 Apr 2023

categoryPython

In this tutorial we will learn how to how to get the last digit of a number in python.

 

How to Get the Last Digit of a Number in Python

The modulo operator% can be used in Python to get a number's last digit.

Here's an example:

 

number = 12345
last_digit = number % 10
print(last_digit) # Output: 5

In the above example, the variable number has been given the value 12345. The % operator gives us the remainder when we divide number by 10. Since the remainder of 12345 divided by 10 is 5, the value of last_digit will be 5. We then print the value of last_digit using the print() function.

Comments 0

Leave a comment