
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if the given decimal number has 0 and 1 digits only in Python
Suppose we have a number num. We have to check whether num is consists of only 0s and 1s or not.
So, if the input is like num = 101101, then the output will be True.
To solve this, we will follow these steps −
- digits_set := a new set with all elements digits of num
- delete 0 from digits_set
- delete 1 from digits_set
- if size of digits_set is same as 0, then
- return True
- return False
Let us see the following implementation to get better understanding −
Example Code
def solve(num): digits_set = set() while num > 0: digit = num % 10 digits_set.add(digit) num = int(num / 10) digits_set.discard(0) digits_set.discard(1) if len(digits_set) == 0: return True return False num = 101101 print(solve(num))
Input
101101
Output
True
- Related Articles
- Check if the String has only unicode digits or space in Java
- Check whether the number has only first and last bits set in Python
- Check whether a number has consecutive 0’s in the given base or not using Python
- How to check if a Python string contains only digits?
- Check whether a String has only unicode digits in Java
- Find the number of integers from 1 to n which contains digits 0’s and 1’s only in C++
- How to check if the string contains only digits in JavaScript?
- Check if all digits of a number divide it in Python
- Minimum number with digits as and 7 only and given sum in C++
- A number is irrational, if and only if its decimal representation is______
- Check if the given number is Ore number or not in Python
- Check if the String contains only unicode letters or digits in Java
- Python program to check if the given number is Happy Number
- Check if a given number divides the sum of the factorials of its digits in C++
- Check if given number is perfect square in Python

Advertisements