Python Binary Number: Validation & Conversion Methods

python-binary-number-validation-conversion-methods
Photo by cottonbro studio on Pexels.com

Python Binary Number – Learn how to validate and manipulate binary numbers, and explore essential techniques for handling binary data effectively. Discover Python’s built-in functions and methods to check and convert binary numbers, empowering you to excel in binary-related programming tasks. 

Also read: Python Program: Find the Greatest Number Among Three Numbers

What is Binary Number?

A Binary number is the representation of number made up of two digits 0 and 1.

E.g., 10, 11, 100, 101, 111,…,etc.

In other words,

A Binary number is the conversion of Decimal number. A Binary number is the number system with base 2 and represents numbers only with of two digits i.e., 0 and 1.

A number that is collection of only two digits 0 and 1 with base 2 is Binary number.

Python Binary Number

Example:

				
					def is_binary_number(binary_string):
    for char in binary_string:
        if char not in ('0', '1'):
            return False
    return True

#function called
print(is_binary_number("101010"))  # Output: True
print(is_binary_number("110020"))  # Output: False
print(is_binary_number("010101"))  # Output: True
				
			

Python Binary Number

Explanation:

This function will return True if the input string represents a valid binary number and False otherwise. It checks each character in the string and ensures that it’s either ‘0’ or ‘1’ using the char not in (‘0’, ‘1’) condition. If any character is found to be different from ‘0‘ or ‘1’, the function will return False. Otherwise, it will return True.

Example:

				
					def is_binary_number(binary_string):
    for char in binary_string:
        if char != '0' and char != '1':
            return False
    return True

#function called
print(is_binary_number("101010"))  # Output: True
print(is_binary_number("110020"))  # Output: False
print(is_binary_number("010101"))  # Output: True
				
			

Python Binary Number

Explanation:

In this code, we use a for loop to iterate through each character in the binary_string. Inside the loop, we use an if-else statement to check if the character is either ‘0’ or ‘1’. If it’s not, the function immediately returns False, indicating that the string is not a valid binary number. If all characters pass the check, the function returns True, indicating that the string is a valid binary number.

Example:

				
					def is_binary_number(binary_string):
    i = 0
    while i < len(binary_string):
        char = binary_string[i]
        if char != '0' and char != '1':
            return False
        i += 1
    return True

#function called
print(is_binary_number("101010"))  # Output: True
print(is_binary_number("110020"))  # Output: False
print(is_binary_number("010101"))  # Output: True
				
			

Python Binary Number

Explanation:

In this version, we use a while loop to iterate through each character in the binary_string. We initialize the variable i to 0, and then in each iteration, we access the character at the current index i. If the character is not ‘0’ or ‘1’, the function immediately returns False, indicating that the string is not a valid binary number. If all characters pass the check, the function returns True, indicating that the string is a valid binary number.

Example:

				
					def is_binary_of_10(number):
#Convert to binary using bin() function and remove the '0b' prefix using Python slicing 
    binary_representation = bin(number)[2:]
    return binary_representation == "1010"  

#function called
print(is_binary_of_10(10))    # Output: True
print(is_binary_of_10(2))     # Output: False
print(is_binary_of_10(1.5))    # Output: False
				
			

Explanation:

The function first converts the input number to its binary representation using the bin() function, which returns a string prefixed with ‘0b’, so we remove the prefix by slicing the string [2:]. Then, we compare the binary representation with “1010”, which is the binary representation of 10. If they match, the function returns True, indicating that the given number is the binary representation of 10. Otherwise, it returns False.

Python Binary Number

Conclusion

This article has delved into the world of binary numbers in Python, providing valuable insights and techniques for checking and manipulating binary data. By learning how to validate and work with binary numbers effectively, you’ve gained a strong foundation for handling binary-related programming tasks.

Recent Articles