The output for the following code is:
int("12", 5)
O/P: 7
int("0", 5)
O/P: 0
int("10", 2)
O/P: 2
I cannot make sense of this. From the Python documentation it says: The "[, base]" part is optional i.e it might take one or two arguments.
The first argument should necessarily be a string which has an int value within the quotes.
int(string, base)
accepts an arbitrary base. You are probably familiar with binary and hexadecimal, and perhaps octal; these are just ways of noting an integer number in different bases:
Each base determines how many values each 'position' in the notation can take. In decimal we count up to 9, then add a position to count the 'tens', so 10
means one times ten, zero times one. Count past 99 and you add a 3rd digit, etc. In binary there are only two digits, so after 1
you count up to 10
, which is one time two, and zero times one, and after 11
you count up to 100
.
The base
argument is just the integer base, and it is not limited to 2, 8, 10 or 16. Base 5 means the number is expressed using digits 0 through to 4. The decimal number 10 would be 20
in base 5, for example (2 times 5).
int(string, 5)
then interprets the string as a base-5 number, and will produce a Python integer to reflect its value:
>>> int('13', 5) # one time 5, 3 times 1 == 8
8
>>> int('123', 5) # one time 5**2 (25), 2 times 5, 3 times 1 == 38
38
If you had to name base-5 numbers it would probably be called pental.