3

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.

chepner
  • 497,756
  • 71
  • 530
  • 681
Manshi Sanghai
  • 283
  • 2
  • 3
  • 10

5 Answers5

7

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:

  • binary represents a number in base 2 (0 and 1)
  • octal represents a number in base 8 (0, 1, 2, 3, 4, 5, 6 and 7)
  • decimal is what is used in daily (western) life to talk about integers, which is base 10 (0 through to 9).
  • hexadecimal is base 16 (0 through to 9, then A, B, C, D, E, F).

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
2

int(string, [base]) function converts given string into decimal number, here base is optional where you can give any number, some examples are

  • 2 = Binary number
  • 8 = Octal number
  • 10 = Decimal number
  • 16 = Hexadecimal number

But please note that the string you give should be a number in that base. Ex: You cannot write int("45",2) because there is no 4 and 5 in binary numbers, instead int("1011",2) is acceptable. In your case,

  int("12",5) => 1*(5 ^ 1) + 2*(5 ^ 0) = 7
  int("0",5) => 0*(5 ^ 0) = 0
  int("10", 2) => 1*(2 ^ 1) + 0*(2 ^ 0) = 2
insearchofcode
  • 438
  • 5
  • 9
0

The int function is returning the string as if it were represented in the base "base". For example, 10 in base 2 (binary) is 2 in base 10 (decimal). 12 in base 5 is 1*5^1 + 2*5^0 = 7.

gariepy
  • 3,576
  • 6
  • 21
  • 34
0

Here base is the number system to which we have to associate the first argument. Like if number system is binary, then "10" mean 2 in decimal. Similarly if "10" with number system ternary mean 3 in decimal and so on. For more details, study regarding number systems and how conversion between number systems works. By the way, we humans understands decimal number system(0-9) very well, while computer understands binary number system.

Mangu Singh Rajpurohit
  • 10,806
  • 4
  • 68
  • 97
0

This fact can be explained as follows:
Suppose our function call is int("12",5), which means (12) is in base 5 and we are requesting for it to be converted into decimal no. i.e., into a base 10 number.

So, the simple rule is

(12)base 5 = 1*(5 ^ 1) + 2*(5 ^ 0)
           = 5 + 2
           = 7

Similar conversions can be done from any other base.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38