In this article, we are going to understand and perform a very simple Python program, to get the first and last digit from the given number. For example, we would be given a number, for example, 1234, and we need to get the first and last digits from the given number, which are 1 and 4 respectively. Here are some sample input outputs for our program –

## Python program to find first and last digit in a number

Sample input and output –

**Example 1 –**

input –

Please enter the number: 33794

output –

first digit – 3

last digit – 4

**Example 2 –**

input –

Please enter the number: 47892

output –

first digit – 4

last digit – 2

### Program Approach

So now, that we have a concrete idea about what we need to achieve, let’s now discuss how are we going to approach the solution. The task here is simple. We just need to find the first and last digits of the given number.

#### Getting the last digit from the given number

Finding the last digit of the given number is a very easy task. In order to get the last digit from the number, we just need to get the remainder when we divide the number by 10. So, the instruction looks something like this –

last_digit = number%10

Here, the number is the variable, which is assigned with the number, whose last digit is required to be found. So, just getting the remainder here, we are going to get the last digit of the number. Getting the first digit is also simple, but it might seem a little bit tricky at first. So now, let’s have a look at how are we going to get the first digit from the given number.

#### Getting the first digit from the given number

If we want to get the first digit from the given number, there are some things that we can go with. Let’s discuss them one by one.

**Way 1 –**

In order to get the first digit from the given number, we just keep dividing the number by 10, till the number becomes less than 10. Once the number is less than 10, we are left with only one digit, which is our first digit.

**Way 2 –**

To get the first digit from the given number, we just need to divide the number by 10 to the power of (n-1), where n is the number of digits in the given number.

For example, if we have the number 9876, and we need to get the first digit from the given number, we just need to divide the number 9876 by 1000, and 1000 comes out to be 10 to the power 3, and 3 is one less than the number of digits in the given number.

So, this is how easy it is to get the first and last digits from the given number. To get the first digit, we are going to use the way 2, but the way 1 would also be given in the comments so that you can easily understand things.

### Python program to find the first and last digit in a number

import math

number = int(input(“Please enter the number:”))

last_digit = number%10

first_digit = number // (10**math.floor(math.log10(number)))

# You can uncomment the below block of code, to get the first digit with way 1.

# while number>=10:

# number//=10

# print(“First digit:”, number)

print(“first digit:”, first_digit)

print(“Last digit:”, last_digit)

As you can see in the above program, we have taken the number as user input, and then simply we are getting the first and last digits from the given numbers. To get the last digit, we simply have taken the remainder when dividing the number by 10. To get the first digit, we have demonstrated both ways, and you can use the way that you are comfortable understanding. Let’s now have a look at the output of the above program.

**Example output 1 –**

Please enter the number:34759

first digit: 3

Last digit: 9

**Example output 2 –**

Please enter the number:47982

first digit: 4

Last digit: 2

As you can see from the output, we have got the first and last digits from the given number successfully. You can also try playing with the program for other input values.

#### Conclusion

In this article, we have understood and performed a simple Python program, to get the first and last digits from the given number. Getting the last digit from the given number is an easy task. Also, we have seen some ways to get the first digit from the given number. You can also try the above program for more input values, and explore more such programs.