Number of python3 (in actual application-hexadecimal, octal, and binary notation)

Source: Internet
Author: User

Number of python3 (in actual application -- hexadecimal, octal, and binary notation)

Python integers can be written in hexadecimal, octal, and binary notation to supplement general decimal notation. Example:

>>> 0o1, 0o20, 0o377 # octal (255, 255) >>> 0x01,0x10, 0xff # hexadecimal (,) >>> 0b1, 0b10000, 0b11111111 # binary (1, 16,255)

Python is displayed in decimal format by default, but it provides built-in functions that allow us to convert Integers to other hexadecimal numeric strings:

>>> oct(64),hex(64),bin(64)('0o100', '0x40', '0b1000000')

The OCT function converts the decimal number to the octal number. The hex function converts the decimal number to the hexadecimal number, and the bin function converts the decimal number to the binary number. In another way, the built-in int function converts a numeric string to an integer, And you can define the second parameter to determine the hexadecimal value of the first numeric string parameter.

>>>int('64'),int('100',8),int('40',16),int('1000000',2)(64, 64, 64, 64)

Note the following before continuing learning. First, python2.6 users should remember to start with a 0 before compiling the octal code. The original octal format in python is as follows:

>>> 0o1, 0o20, 0o377 # available in python2.6 and python3.0 (1, 16,255) >>> 01,020,037 7 # only used in python2.6 (1, 16,255)

In python3.0, the second group of syntaxes in the previous example will produce errors. Even if it is not an error in python2.6, be careful not to start a numeric string with 0 unless you really want to represent an octal value. Python2.6 regards it as an octal number, but it may not work as expected. 010 always has an octal number in python2.6, not a decimal number. That is to say, in order to maintain symmetry with the hexadecimal and binary forms, python3.0 modifies the octal form and must use 0o010 in python3.0, and,In python2.6, you should also try to use 0o010.
Note that these constants can generate Integers of any length. For example, the following example creates an integer in hexadecimal format, and then displays it in decimal format, and converts it to octal and binary format:

>>> X=0xFFFFFFFFFFFFFF>>> X72057594037927935>>> oct(X)'0o3777777777777777777'>>> bin(X)'0b11111111111111111111111111111111111111111111111111111111'>>>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.