In your code, simply remove this line as you are immediately reassigning over the functions input argument

binary = []

As a more concise alternative

def binatodeci(binary):
    return sum(val*(2**idx) for idx, val in enumerate(reversed(binary)))

Example

>>> binatodeci([1,1,0,1])
13
Answer from Cory Kramer on Stack Overflow
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-convert-binary-to-decimal
How to Convert Binary to Decimal in Python
January 27, 2026 - def bitwise_binary_to_decimal(binary_str): decimal_val = 0 for bit in binary_str: # Left shift the current value and add the new bit decimal_val = (decimal_val << 1) | int(bit) return decimal_val # Binary flag for a US stock market transaction transaction_flag = "111001" decimal_id = bitwise_binary_to_decimal(transaction_flag) print(f"The transaction flag ID in decimal is: {decimal_id}") I executed the above example code and added the screenshot below. This method is elegant and shows a deep understanding of how computers actually handle data at the hardware level. One thing Iโ€™ve learned over the years is that you should never trust your input data. If you try to convert a string that isnโ€™t a valid binary (like โ€œ1021โ€), Python will throw a ValueError.
Discussions

How to Convert list of binary number to decimal in python without using any inbuilt function?
Treat them as true and false or multiply it by its appropriate counterpart. Ex. [1,0,0,1] => 1ร—23 + 0ร—22 + 0ร—21 + 1ร—20 More on reddit.com
๐ŸŒ r/learnpython
3
1
May 1, 2021
Algorithm to convert a 2d binary list to decimal numbers in Python - Stack Overflow
I ran into this performance issue when I tried to convert a huge 2D list of binary data to decimal numbers. More on stackoverflow.com
๐ŸŒ stackoverflow.com
October 15, 2017
Convert a binary number in a list back to its decimal form in python? - Stack Overflow
How would I convert a binary string to a decimal number? I am taking the binary out of a list of values, and need to then convert it to decimal format. ex. x=["0b000101"] needs to become x= [5] I... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Converting binary to decimal integer output - Stack Overflow
The biggest problem is that we are not allowed to use built-in functions. I understand why we are not allowed to use them, but it's making this problem much more difficult, since I know Python has a built-in function for binary to decimal. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Bhavyasree
bhavyasree.github.io โ€บ PythonClass โ€บ Notebooks โ€บ 07.binary,decimal
Binary and Decimal Conversions - Python Class
binary = list(input("Enter a binary number:")) binaryToDecimal(binary) Enter a binary number:10 The decimal value of the number is 2 ยท We can use the built-in int() function to convert binary numbers into decimals.
๐ŸŒ
Code It Bro
blog.codeitbro.com โ€บ home โ€บ programming โ€บ python โ€บ binary to decimal python: 5 easy conversion methods with examples
Binary to Decimal Python: 5 Easy Conversion Methods with Examples
October 19, 2025 - #Binary_To_Decimal function with ... binary number. ... Iterate the list of binary numbers and convert it to a decimal using the int() function....
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ decimal-to-binary-list-conversion-in-python
Binary to decimal and vice-versa in Python
May 4, 2020 - In this article, we will see how to convert Binary to Decimal and Decimal to Binary. Binary is the simplest kind of number system that uses only two digits of 0 and 1 (i.e. value of base 2).
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-binary-list-to-integer
Python - Binary list to integer - GeeksforGeeks
December 30, 2024 - For each binary digit in the list, we use the left shift operator (<<) to shift the bits of number to the left by one place and then use the bitwise OR operator (|) to add the current bit.
Find elsewhere
๐ŸŒ
Javatpoint
javatpoint.com โ€บ how-to-convert-binary-to-decimal-numbers-in-python
How to convert binary to decimal numbers in python - Javatpoint
How to convert binary to decimal numbers in python with tutorial, tkinter, button, overview, canvas, frame, environment set-up, first python program, etc.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-program-to-covert-decimal-to-binary-number
Convert Decimal to Binary Number - GeeksforGeeks
This article aims at presenting certain shorthand to do the opposite, i.e binary to decimal conversion. Let's discuss certain ways in which this can be done. Method #1 : Using list comprehension + format() In t ยท 6 min read Python Program to Convert Binary to Hexadecimal
Published: March 19, 2025
๐ŸŒ
Medium
medium.com โ€บ @pivajr โ€บ pythonic-tips-how-to-convert-binary-to-decimal-in-python-9f781b029130
Pythonic Tips: How to Convert Binary to Decimal in Python | by Dilermando Piva Junior | Medium
May 12, 2025 - The int() function is one of the most direct ways to convert a binary number (as a string) into a decimal. You just need to pass the string and specify the base (2 for binary). ... Python interprets the string and returns the corresponding decimal ...
๐ŸŒ
3Ri Technologies
3ritechnologies.com โ€บ home โ€บ master python conversions: binary to decimal and decimal to binary
How to Convert Binary to Decimal in Python - Pune
November 7, 2025 - The int() function takes the binary input and outputs the decimal equivalent of the integer value of the Binary. The decimal result can be used in a mathematical calculation or saved in a variable for later use.
๐ŸŒ
CodeScracker
codescracker.com โ€บ python โ€บ program โ€บ python-program-convert-binary-to-decimal.htm
Python Program to Convert Binary to Decimal
Python Program to Convert Binary to Decimal - In this article, we've created some programs in Python, to convert binary number entered by user at run-time to its equivalent decimal value. Binary to Decimal using while loop, Using for loop, int(), function, class
๐ŸŒ
Quora
quora.com โ€บ How-do-I-convert-binary-to-decimal-using-simple-Python-commands
How to convert binary to decimal using simple Python commands - Quora
Answer (1 of 8): It is very simple with the built-in solutions the language offers. The solutions addressed next work both in Python2 and Python3. To convert an integer (integers are in base 10) to binary, you should use the function bin() as follows: [code]bin(200) [/code]This function takes a...
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ binary to decimal in python
Binary to Decimal in Python: A Simple Guide with Code | upGrad
November 12, 2024 - Here, we use a format specifier to convert the binary value '1010' to a decimal string. The "{:d}" specifier indicates that the value should be treated as an integer. ... Imagine you have sensor data in binary format that you need to convert and format for analysis. you will need a Python program to convert binary to decimal, octal, and hexadecimal:
Top answer
1 of 2
5

A few additional points for your learning experience.

You're not using sys, so the import line is unnecessary.

In range(...), the default start is 0, so instead of range(0, x) you can write simply range(x).


Don't use pointless comments like this:

# Return 1 if exponent is 0
if exponent == 0:
    return 1

Instead if this:

power = base
for j in range(1, exponent):
    power *= base

It would be better this way:

power = 1
for j in range(exponent):
    power *= base

Because in this version base is referenced only once, and range is simplified by dropping 1 to use the default 0.


action="store" is unnecessary here:

parser.add_argument('binary_number', action="store")

main is not a good name for that function does. It would be better to rename it to binary_to_decimal, for example.

And while at it, it would be better to move the content from the if __name__ ... inside a new main method, like this:

def main():

    parser = argparse.ArgumentParser(description='Converts a binary to a decimal number.')
    parser.add_argument('binary_number')
    args = parser.parse_args()
    binary_to_decimal(args.binary_number)

if __name__ == '__main__':
    main()

Many people miss this point, actually. The reason to do it this way is that code in the if __name__ ... block is in the global namespace. So variables with the same name in the rest of the code will shadow those variables, which can lead to nasty bugs. By moving that code to a method, this cannot happen.

PEP8

PEP8 is the official style guide of Python, and you have a few violations:

  • Put 2 blank lines before each method definition and you only put 1.

  • Put 2 spaces in front of # inline comments

  • Lines should not be longer than 79 characters

2 of 2
4

That is nice-looking code, but it's also quite long for the problem that you are solving. The simple solution would be print(int(binary_number, 2)).

Assuming that you want to solve the problem the hard way as an exercise, it's a good idea to separate your calculation routines from your input/output routines. You need a binary_to_int(binary_string) function. It can be implemented using a much simpler algorithm.

def binary_to_int(binary_string):
    n = 0
    for char in binary_string:
        n *= 2
        if char == '1':
            n += 1
    return n
๐ŸŒ
WsCube Tech
wscubetech.com โ€บ resources โ€บ python โ€บ programs โ€บ binary-to-decimal
Convert Binary to Decimal in Python (3 Different Ways)
October 28, 2025 - Learn how write python program to convert binary to decimal with two different methods in this complete easy to follow guide.