convert a number from base 2 to base 10 in python

To convert a number from base 2 to base 10 in Python, we can use the int() function which takes two arguments - the first argument is the string representation of the number and the second argument is the base of the number.

Here's an example:

main.py
binary_num = '1010'
decimal_num = int(binary_num, 2)
print(decimal_num)
72 chars
4 lines

In this example, we have a binary number '1010'. We pass this number along with the base 2 as arguments to the int() function, which converts the binary number to its decimal equivalent. We then print out the decimal number which is 10.

So, the output of the above code will be:

main.py
10
3 chars
2 lines

gistlibby LogSnag