You can use int
and set the base to 2
(for binary):
>>> binary = raw_input('enter a number: ') enter a number: 11001 >>> int(binary, 2) 25 >>>
However, if you cannot use int
like that, then you could always do this:
binary = raw_input('enter a number: ') decimal = 0 for digit in binary: decimal = decimal*2 + int(digit) print decimal
Below is a demonstration:
>>> binary = raw_input('enter a number: ') enter a number: 11001 >>> decimal = 0 >>> for digit in binary: ... decimal = decimal*2 + int(digit) ... >>> print decimal 25 >>>
Related Posts:
- Converting integer to binary in python
- TypeError: ‘int’ object is not subscriptable
- How do you round UP a number?
- How do you round UP a number?
- TypeError: ‘int’ object is not callable
- TypeError: ‘int’ object is not callable
- TypeError: ‘int’ object is not callable
- Cannot find module cv2 when using OpenCV
- Maximum and Minimum values for ints
- Cannot find module cv2 when using OpenCV
- Maximum and Minimum values for ints
- Python ‘If not’ syntax [duplicate]
- RuntimeWarning: invalid value encountered in divide
- Converting dictionary to JSON
- WinError 2 The system cannot find the file specified (Python)
- How do I parse a string to a float or int?
- IndexError: tuple index out of range —– Python
- Round a floating-point number down to the nearest integer?
- sklearn error ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64’)
- sklearn error ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64’)
- TypeError: cannot perform reduce with flexible type
- Could not find a version that satisfies the requirement tensorflow
- sklearn error ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64’)
- Could not find a version that satisfies the requirement tensorflow
- Local variable referenced before assignment?
- ln (Natural Log) in Python
- ImportError: numpy.core.multiarray failed to import
- Python Traceback (most recent call last)
- Unable to plot Double Bar, Bar plot using pyplot for ndarray
- How to pip or easy_install tkinter on Windows
- Cannot find module cv2 when using OpenCV
- Convert list to tuple in Python
- ImportError: numpy.core.multiarray failed to import
- How to remove \xa0 from string in Python?
- What is the meaning of [:] in python [duplicate]
- How do you express binary literals in Python?
- Why are Python’s ‘private’ methods not actually private?
- Remove list from list in Python
- Maximum and Minimum values for ints
- How to create a new text file using Python
- Binary numbers in Python
- TypeError: write() argument must be str, not bytes (Python 3 vs Python 2 )
- What does the ‘b’ character do in front of a string literal?
- python socket.error: [Errno 98] Address already in use
- How to import files in python using sys.path.append?
- How do I install the yaml package for Python?
- Check string “None” or “not” in Python 2.7
- Reading a binary file with python
- Change figure size and figure format in matplotlib
- What does the ‘b’ character do in front of a string literal?
- How can I convert a character to a integer in Python, and viceversa?
- how to update spyder on anaconda
- how does \r (carriage return) work in Python
- What is Python buffer type for?
- AttributeError(“‘str’ object has no attribute ‘read'”)
- IndexError: index 1 is out of bounds for axis 0 with size 1/ForwardEuler
- Python Save to file
- How to convert string to binary?
- Add list to set?
- Convert hex to binary
- Create 3D array using Python
- TypeError: unsupported operand type(s) for -: ‘list’ and ‘list’
- ImportError: No module named scipy
- AttributeError: ‘datetime’ module has no attribute ‘strptime’
- Python add item to the tuple
- Convert decimal to binary in python
- Checking whether pip is installed?
- Decoding UTF-8 strings in Python
- How can I read pdf in python?
- No module named setuptools
- How to have an array of arrays in Python
- Python int to binary string?
- Checking whether a variable is an integer or not
- ValueError: unsupported pickle protocol: 3, python2 pickle can not load the file dumped by python 3 pickle?
- ValueError: unsupported pickle protocol: 3, python2 pickle can not load the file dumped by python 3 pickle?
- AttributeError(“‘str’ object has no attribute ‘read'”)
- How to convert string to binary?
- How to use 2to3 properly for python?
- Why does using from __future__ import print_function breaks Python2-style print?
- How to fix: “UnicodeDecodeError: ‘ascii’ codec can’t decode byte”
- Python – ‘ascii’ codec can’t decode byte
- Python Error – int object has no attribute
- Difference between scikit-learn and sklearn
- Loading a file with more than one line of JSON into Pandas
- Type error Unhashable type:set
- How to detect key presses?
- What is the difference between json.load() and json.loads() functions
- Python: count repeated elements in the list
- How to XOR two strings in Python
- How to convert ‘binary string’ to normal string in Python3?
- TypeError: ‘_io.TextIOWrapper’ object is not subscriptable
- Copy a list of list by value and not reference
- Why I get ‘list’ object has no attribute ‘items’?
- Python 101: Can’t open file: No such file or directory
- Split a python list into other “sublists” i.e smaller lists
- TypeError: argument of type ‘NoneType’ is not iterable
- scrapy run spider from script
- python error: TypeError: an integer is required
- If list index exists, do X
- Python Requests – No connection adapters