How to Get the First and Last Digit of a Number in Python [3 Examples]

In this Python tutorial, we will understand and perform an example of the topic “How to get the first and last digit of a number in Python.”

For Example, if a user enters the number 34281, it will get the first and last digits from the given number in Python, which is 3 and 1, respectively.

Here, we will demonstrate different methods to write a Python program to get the first and last digits from the given number, such as using a while loop, indexing, and a math module with a log function.

How to get the first and last digit of a number in Python using a while loop

A while loop in Python is used to execute a set of statements until the condition is True or False.

Here is the complete source code of Python to get the first and last digits of a number using a while loop.

MacbookM2_Price = int(input("Enter the price of Macbook M2 price: "))
first_digit = MacbookM2_Price
while first_digit >= 10:
    first_digit //= 10  
last_digit = MacbookM2_Price % 10 
print("First digit:", first_digit)
print("Last digit:", last_digit)

We use the modulo % operator in Python when the modulo (number) is divided by 10 and returns the last digit, and to find the first digit of a number, we divide the given number by 10 until the number is greater than 10.

    first_digit //= 10  
last_digit = MacbookM2_Price % 10 

Output:

Enter the price of Macbook M2 price: 110892
First digit: 1
Last digit: 2

You can refer to the screenshot below to see the output of the program to get the first and last digit of a number in Python using a while loop.

Get first and last digits of a number in Python

Get the first and last digits of a number on Python using indexing

I will use the [] bracket to access any character in the string by index position in Python. We need to pass the index position in the square brackets, which will return the character at that index.

READ:  Sum All the Items in Python List Without using sum()

This is the source code of Python to get the first and last digits of a number using indexing.

population = int(input('Enter the number: '))

population = str(population)

first_digit = population[0]
last_digit = population[-1]
print('The first digit of number:', first_digit)
print('The last digit of number:', last_digit)

Output:

Enter the number: 98734567
The first digit of number: 9
The last digit of number: 7

Refer to the image below, and it will provide the output of the Python program to get the first and last digits of a number using indexing.

How to get the first and last digits of a number in Python

Python program to get the first and last digits of a number using a math module with log function

Python provides the math module, the built-in module for mathematical tasks. The math module has a set of methods and constants.

This is the complete program to get the first and last digit of a number in Python using a math module with a log function.

import math

def firstDigit(temperature) :  
    digits = (int)(math.log10(temperature)) 

    temperature = (int)(temperature / pow(10, digits)) 
    return temperature; 
   
def lastDigit(temperature) : 
    return (temperature % 10) 

temperature = 29; 
print(firstDigit(temperature), end = " " ) 
print(lastDigit(temperature))

Here, I have used math.log10 from the math module in Python to calculate the number of digits using the logarithm function. The pow() function is used to calculate the power of 10 and returns the extracted first digit.

Then, by using the modulo operator %, we get the last digit of the temperature.

def firstDigit(temperature) :  
    digits = (int)(math.log10(temperature)) 
    temperature = (int)(temperature / pow(10, digits)) 
    return temperature;

def lastDigit(temperature) : 
    return (temperature % 10) 

Output:

2 9

You can refer to the screenshot below to see the output of the Python program to get the first and last digits of a number using a math module with a log function.

Python program to get the first and last digits of a number

Conclusion

I hope you find the Python article useful. Here, we have understood and learned how to get the first and last digits of a number in Python. Also, we have seen some ways to perform a Python program to get a number’s first and last digit from the given number.

READ:  Download zip file from URL in Python

Methods explained in this post to get the first and last digit of a number in Python such as using a while loop, indexing, and math module with a log function.

You may also like to read: