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 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.

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**.

## 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**.

## 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**.

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:

- How to Get the first digit of a Number in Python
- How to Find the Smallest Number in a Python List
- How to Remove Brackets from a List in Python
- How to Remove Brackets from a Python String

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etcâ€¦ for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.