Python程序查找数字中的最后一位数

编写一个Python程序,通过实际示例查找数字中的最后一位数。

Python程序查找数字中的最后一位数

这个Python程序允许用户输入任何整数值。接下来,Python将查找用户输入值的最后一位数。

# Python Program to find the Last Digit in a Number

number = int(input("Please Enter any Number: "))

last_digit = number % 10

print("The Last Digit in a Given Number %d = %d" %(number, last_digit))
Python Program to find Last Digit in a Number

使用函数查找数字中最后一位的Python程序

这个Python数字最后一位的程序与上面相同。但这次,我们将Python逻辑分离出来,并将其放入一个单独的函数中。

# Python Program to find the Last Digit in a Number

def lastDigit(num):
    return num % 10

number = int(input("Please Enter any Number: "))

last_digit = lastDigit(number)

print("The Last Digit in a Given Number %d = %d" %(number, last_digit))

Python最后一位数输出

Please Enter any Number: 1925
The Last Digit in a Given Number 1925 = 5
>>> 
Please Enter any Number: 90876
The Last Digit in a Given Number 90876 = 6