Check First and Last Digits Equal

Take a 4-digit number and check if the first and last digits are equal.

IntermediatePhase 1 - Level 3: Math and Number LogicExample 3 of 10
check-first-and-last-digits-equal.py
1# Take 4-digit number
2num = int(input("Enter a 4-digit number: "))
3
4# Extract first and last digits
5first_digit = num // 1000
6last_digit = num % 10
7
8# Check if equal
9if first_digit == last_digit:
10 print("First and last digits are equal")
11else:
12 print("First and last digits are not equal")

Output

Enter a 4-digit number: 1231
First and last digits are equal

Enter a 4-digit number: 1234
First and last digits are not equal

What's going on

Extract first (thousands) and last (units) digits.

Key Concepts:

First digit: num // 1000
Last digit: num % 10
Compare for equality