How to convert python binary to decimal?

青灯夜游
Release: 2019-06-05 10:14:17
Original
57672 people have browsed it

How to convert python binary to decimal?

In python, you can use the built-in function int() function to convert binary to decimal; the int() function can convert a numeric string or decimal number in a specified base into an integer. . (Recommended learning: python video tutorial)

Syntax:

int(object,base)
Copy after login

How to convert python binary to decimal?

Return value: Return integer data.

Note: When converting floating point numbers to integer data, no matter what the decimal part of the floating point number is, the int() function will only retain the integer part and round off the decimal part. go. Therefore, when solving problems such as rounding of floating point numbers, you should avoid using the int function directly.

Example 1: Convert a binary number to a decimal number

test = ['111011011111', '0b101']
 
for number in test:
    print(int(number, 2))
Copy after login

Output:

3807
5
Copy after login

Example 2: Convert an octal number Convert to decimal number

test = ['-1537202', '0o147']
 
for number in test:
    print(int(number, 8))
Copy after login

Output:

-441986
103
Copy after login

For more python related technical knowledge, please visit the python introductory tutorial column to learn!

The above is the detailed content of How to convert python binary to decimal?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template