Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
The
base
argument to
int
is meant to be used when parsing a string, not when passing an int to
int
:
>>> int('ff', 16)
It's a well-established convention to use a, b, c, ..., z for digits representing 10, 11, 12, ..., 35, but there's no convention for what symbol to use for digit 36 in base 37.
First of all: you can only specify a base for int()
when converting strings into numbers. Say you have a string with a hexadecimal number, so base 16:
>>> int('2a', 16)
This result differs significantly from the same string interpreted as a different base:
>>> int('2a', 11)
>>> int('2a', 29)
You only ever need the base of an integer number when presenting the value visually or when parsing the integer value from a string representation. You can display an integer value in many different ways, but an int
object is the value, not a visual presentations, and doesn't have a base nor can you change that base.
The logical extension when already supporting hexadecimal notation (digits 0-9 and letters A-F), is also support using the letters G-Z, and 10 digits and 26 letters makes it possible to use base 36:
>>> int('zz', 36)
Further bases would need to use non-alphanumeric symbols, for which there is no clear pre-set ordering.
You can't have a base lower than 2; you can't count with just a single digit, the value of 0
will not change.
–
This is a practical implementation convention. What do you envision as the character set for base 100? :-)
Using digits and the entire alphabet is common enough, so the base
function with a contiguous set of acceptable base values implemented the functionality that far. Base 1 is simply len(arg).
There is also a convention for base-64 encoding; without a convention for the values 37-63, though, the base
function left the radix-64 value for a separate interface.
–
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.