In this tutorial, we will learn how to check if a digit is present in a given number using Python. This problem can be solved using various approaches, and in this tutorial, we will look at two of them, i.e., by converting the number to a string and iterating, and by dividing the number and checking the remainder.
Step 1: Converting the Number to String
The first approach is to convert the given number to a string, and then check if the digit to be verified is present in the string. We can use the str()
function to convert a number to a string and check for the presence of the digit using the in
keyword.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
def check_digit(number, digit): # Convert the number and digit to string str_number = str(number) str_digit = str(digit) # Check if digit is in the number string if str_digit in str_number: return "Yes" else: return "No" number = 12345 digit = 3 result = check_digit(number, digit) print("Is the digit", digit, "present in the number", number, "?", result) |
Step 2: Using Division and Modulus
Another approach is to divide the given number and check the modulus (remainder) with the given digit. We can use a while loop to divide the number until it becomes 0 and check if the remaining matches the given digit.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
def check_digit_v2(number, digit): while number > 0: remainder = number % 10 if remainder == digit: return "Yes" # Divide the number by 10 to remove the last digit number = number // 10 return "No" number = 12345 digit = 3 result = check_digit_v2(number, digit) print("Is the digit", digit, "present in the number", number, "?", result) |
Step 3: Comparing the Two Methods
Let’s compare both methods by running them on a sample input.
1 2 3 4 5 6 7 8 |
number = 1234567890 digit = 5 result_1 = check_digit(number, digit) result_2 = check_digit_v2(number, digit) print("Using method 1:", result_1) print("Using method 2:", result_2) |
Using method 1: Yes Using method 2: Yes
You can see that both methods correctly identify if the digit is present in the given number.
Full Code
Here’s the full code for both methods, along with the output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
def check_digit(number, digit): str_number = str(number) str_digit = str(digit) if str_digit in str_number: return "Yes" else: return "No" def check_digit_v2(number, digit): while number > 0: remainder = number % 10 if remainder == digit: return "Yes" number = number // 10 return "No" number = 12345 digit = 3 result = check_digit(number, digit) print("Is the digit", digit, "present in the number", number, "?", result) number = 12345 digit = 3 result = check_digit_v2(number, digit) print("Is the digit", digit, "present in the number", number, "?", result) number = 1234567890 digit = 5 result_1 = check_digit(number, digit) result_2 = check_digit_v2(number, digit) print("Using method 1:", result_1) print("Using method 2:", result_2) |
Is the digit 3 present in the number 12345 ? Yes Is the digit 3 present in the number 12345 ? Yes Using method 1: Yes Using method 2: Yes
Conclusion
In this tutorial, we have learned how to check if a digit is present in a number using Python. We covered two different approaches, i.e., converting the number to a string and iterating, dividing the number, and checking the remainder.
Both methods work effectively and can be used according to your preference or the specific requirements of your project.