Using the python "int" function skillfully

Source: Internet
Author: User

In Python, you can use the keyword "int" to implement other data types to force conversions to shaping data. However, it is important to note that there are long integers in the python2, but in Python3, no matter how long the numbers are, they are shaped.

int function prototype: int (X,[base])

where x's data type can be a string or a number, base is a binary, enclosed in brackets, meaning can be omitted, default value is 10.

Common uses of the INT function can be broadly divided into the following categories:

1. When x is a float type or int type, which is a digital type

V=int (3.3/1) #在python中的运算符 "/" and similar in C, here is the meaning of division, that is, 3.3/1=3.3print(v)

Output is displayed as: 3

V=int (2e3)  # 2e3 refers to 2*10^3, this usage does not print(v) in C

Output is: 2000

V=int (10,16) #这里的10是数字10, 16 is 16 binary meaning print(v)

Output: Error.

Tip: Int () can ' t convert non-string with explicit base

The translation means that when "base" exists, int () cannot convert a non-string type.

2. When x is a string

V=int ("123")print(v)

Output: 123

At this point 123 of the output is 123 meaning, is the shaping of 123. Int () casts the string "123" to reshape 123. It is important to note that when base is omitted, the default value is 10.

In C language has atoi, ATOL, atof and other library functions, can be ASCII encoded string into int, long, float type of number, need to include the header file Stdlib.h, using the method is not as simple as python.

V=int ("123", +)print(v)

Output: 291.

The string "123" is first converted to the number 123, followed by the decimal output, which is 291. Here the 123 is 16 binary 123, the output is 10 binary, so it is actually 0x123=291.

V=int ("GG", +)print(v)

Output: Error.

Tip: Invalid literal for int. () with base: ' GG '

In hexadecimal, the largest is "F", and "G" is out of range. If the code in the "16" is changed to "17", the program is correct, output 288, that is, 17 binary GG to decimal is 288.

Note: The range of base here is 2~36!!!

3.x is a string, and is 0x, 0b, 0 boot 16 binary, binary, octal, and so on.

A="0x10"v=int (A, +)print(v)

Output: 16.

A="0x10"v=int (A, +)print(v)

Output: Error.

Tip: valueerror:invalid literal for int () with base: ' 0x10 '.

0x is not a 17-based leader, and there is no symbol "X" in the 17 binary.

A="0x10"v=int (a,35)print(v)

Output: 40460.

Although 0x is not a 35-based leader, the 35 binary uses "X" to represent 33.

Summary: 1. When x is a number, base must be omitted. (or after base assignment, X can only represent a string)

2. When x is a string, print output requires that the string be converted to decimal according to the base value, and the result must be a decimal.

Classic example: Avanti and the King to play chess, the king said if he lost the words Avanti want anything he can get out. Avanti says it's going to be a bit of rice. The chessboard altogether 64 small lattice, in the first lattice puts 1 grain meters, the second lattice puts 2 grain meters, the third lattice puts 4 grain meters, the fourth lattice puts 8 grain meters, and so on, behind each lattice in the meter is the former one lattice twice times, has been put 64 the lattice to fill. How many grains of rice do you need?

This example is not a problem in junior or high school, and the equations can be solved. But if you think about it as a computer, it becomes very simple. The first lattice is 1, the second one is 2, the third one is 4, and so on ... The problem is actually converting a binary number into decimal.

If there are only two squares, you will need to 1+2=3 grain rice altogether. Two lattice is "11", "11" to decimal is 3.

If there are only three squares, you will need to 1+2+4=7 grain rice altogether. Two lattice is "111", "111" to decimal is 7.

If there are only two squares, you will need to 1+2+4+8=15 grain rice altogether. Two squares are "1111". "11" is converted to decimal 15.

............

So, 64 squares in binary notation is "11 ... 11 ", 64 1, this question becomes 64 1 to 10. How much is it, very simple, 2**64-1

Writing in Python is

V=int ('1'*64,2)print(v)

Output: 18446744073709551615

Using the python "int" function skillfully

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: info-contact@alibabacloud.com 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.