• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Binary Tuple to Integer in Python

Binary Tuple to Integer in Python

Author: Aditya Raj
Last Updated: March 14, 2022

A binary tuple is a tuple that contains only 0s and 1s as its elements. In this article, we will discuss different ways to convert a binary tuple to an integer in python. 

How to Convert a Binary Tuple to an Integer in Python

Consider that we are given the following binary tuple.

myTuple= (1,1,0,1,1,0,1)

Now, we have to create an integer from this binary tuple. The integer will contain all the digits of the tuple in its binary representation. So, the number will be (1101101)2. In decimal representation (1101101)2 evaluates to 109. So, our program should give the output 109. 

To convert a binary number to a decimal number, we multiply the digits of the number with powers of 2. The rightmost bit is multiplied by 20. The second rightmost digit is multiplied by 21, the third digit from the right is multiplied by 22. Similarly, the Nth digit is multiplied by 2N-1. After that, the value from each digit is added and the decimal representation is obtained.

For example, we can convert (1101101)2  to decimal representation as follows.

(1101101)2 = 1x26  +1x25 +0x24+1x23+1x22+0x21+1x20

    =64+32+0+8+4+0+1

    =109

To implement the above logic to convert a binary tuple to integer in python, we will first initialize a variable myInt to 0. We will also calculate the length of the tuple using the len() function.  After that, we will traverse the tuple from right to left and multiply the elements with powers of 0 using a for loop, range() function, and the tuple length. After multiplying, we will add the values to myInt. After execution of the for loop, we will get the integer output in the myInt variable. You can observe this in the following example. 

myTuple = (1, 1, 0, 1, 1, 0, 1)
myInt = 0
length = len(myTuple)
for i in range(length):
    element = myTuple[length - i - 1]
    myInt = myInt + element*pow(2, i)

print("The tuple is:", myTuple)
print("The output integer is:", myInt)

Output:

The tuple is: (1, 1, 0, 1, 1, 0, 1)
The output integer is: 109

Convert a Binary Tuple to an Integer Using Strings

We can also use strings to convert a binary tuple to an integer in python. For this, we will first convert all the elements of the tuple to string using the str() function and map() function. After that, we will create a string from the elements of the tuple using the join() method. The join() method, when invoked on a string, takes an iterable object as input and returns a string consisting of the elements of the iterable. We will first create an empty string and then use the join() method to obtain the string from tuple as follows.

myTuple = (1, 1, 0, 1, 1, 0, 1)
newTuple = map(str, myTuple)
myStr = "".join(newTuple)

print("The tuple is:", myTuple)
print("The output string is:", myStr)

Output:

The tuple is: (1, 1, 0, 1, 1, 0, 1)
The output string is: 1101101

After obtaining the string, we can directly convert the string to an integer using the int() function as follows.

myTuple = (1, 1, 0, 1, 1, 0, 1)
newTuple = map(str, myTuple)
myStr = "".join(newTuple)
myInt = int(myStr, 2)
print("The tuple is:", myTuple)
print("The output integer is:", myInt)

Output:

The tuple is: (1, 1, 0, 1, 1, 0, 1)
The output integer is: 109

In the code, we have passed the value 2 to the int() function as the second input argument to show that the string contains bits of a binary number.

Conclusion

In this article, we have discussed two ways to convert a binary tuple to an integer in python. To know more about strings, you can read this article on string concatenation in python. You might also like this article on list comprehension in python.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us