What does the base value in the int function do?

I read the official document https://docs.python.org/2/library/functions.html#int , but still confused.

I tried some command on my terminal, I find some rules, but still do not quite understand. Hopefully someone who has more knowledge of this can further explain this.

Below are my examples and conclusions:

int('0', base=1) ValueError: int() base must be >= 2 and <=36 int('3', base=2) ValueError: invalid literal for int() with base 2: int('3', base=4) 3 int('33', base=4) 15 int('333', base=4) 63 int('353', base=4) ValueError: invalid literal for int() with base 4: 

I find two rules here:

  • line numbers should be less than the base.
  • int() will return a number equal to (n)*(base^(n-1)) + (n-1)*(base^(n-2)) + ... + 1*(base^0)

Are there any other hidden rules besides this, and what is the problem, which is the basis for the solution?

+7
python
source share
4 answers

He does exactly what he says - converts a string into an integer into a given number base. According to the documentation, int() can convert strings to any base from 2 to 36. At the lower end, base 2 is the lowest useful system; base 1 will only have “0” as a character, which is useless to count. At the upper end, 36 is chosen arbitrarily, because we use characters from "0123456789abcdefghijklmnopqrstuvwxyz" (10 digits + 26 characters) - you can continue with a large number of characters, but it is not clear what to use after z.

The "normal" math is base-10 (uses the characters "0123456789"):

 int("123", 10) # == 1*(10**2) + 2*(10**1) + 3*(10**0) == 123 

Binary base-2 (uses the characters "01"):

 int("101", 2) # == 1*(2**2) + 0*(2**1) + 1*(2**0) == 5 

"3" does not make sense in base 2; it uses only the characters "0" and "1", "3" is an invalid character (sort of trying to make an appointment by January 34th).

 int("333", 4) # == 3*(4**2) + 3*(4**1) + 3*(4**0) # == 3*16 + 3*4 + 3*1 # == 48 + 12 + 3 # == 63 
+3
source share

The base value tells python to interpret the given string as the value of another base.

For example, 1011 in base 2 is 11. Thus, int('1011', 2) returns 11 .
On the other hand, 1011 in base 3 is 31. Thus, int('1011', 3) returns 31 .

Decimal numbers are in base 10, so the default value of base is 10 .

An interesting side effect of choosing a number base is that in this system there is no number that is higher (or equal) to the base itself. That's why we do not have a decimal digit in the decimal system, but in the hexadecimal system (base 16), the digit A for ten. That is why you received errors for requesting a number with the number 5 for interpretation in base 4.

+5
source share

The int() function can convert an integer to a base (or radix ) other than 10, passed as a string, where the second parameter is the base of the number in the string, and the result will be an integer in base 10. For example, all of the following string values ​​will result in an integer number 42 :

 int('42') # by default, it base 10 => 42 int('42', 10) # same as above => 42 int('101010', 2) # base 2 (binary) => 42 int('2A', 16) # base 16 (hexadecimal) => 42 
+3
source share

A base is the number of different basic elements or numbers in a number system.

Base 2 has 0 and 1 and can be represented in electronics as on and off.

Base 10 has the familiar 10 digits of 0..9

Any integer can be expressed in any of the bases, but the string of characters will be different. For example, in the base 2 1111 = 15 in the base 10

The ability to choose a basic conversion to strings or from strings allows the software to accept input data in different databases and translate the database, if necessary.

See also http://en.wikipedia.org/wiki/Radix

+2
source share

All Articles