Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does base value do in int function?

Tags:

python

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

I've tried some command on my terminal, I find some rules, but still not quite clear about it. Hope someone with more knowledge about this can explain it further.

Below are my examples and findings:

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:

  1. the single string numbers must be smaller than the base number.
  2. the int() will return a number which equals (n)*(base^(n-1)) + (n-1)*(base^(n-2)) + ... + 1*(base^0)

Are there any other hidden rules than this, and what kind of problem the base is designed to solve?

like image 702
Zen Avatar asked Apr 21 '14 02:04

Zen


People also ask

How does int know the base?

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.

How does the int () function work in Python?

Python int() function converts the specified value into an integer number. The int() function will returns an integer object constructed from a number or string let say x, or return 0 if no argum ents are specified.

How is the base of the number in Python?

Python - int()Second parameter to the function is the base of number system. Here �12' is string representation of octal number that is equivalent to 10 (Ten) in decimal number system (with base=10). Similarly, Hexadecimal number (with base=16) is converted to decimal number.

What does int input ()) mean in Python?

With int(input()) you're casting the return value of the input() function call to an integer. With input(int()) you're using int() as a prompt for the user input. Since you don't pass int() an argument, it returns zero.


1 Answers

It does exactly what it says - converts a string to integer in a given numeric base. As per the documentation, int() can convert strings in any base from 2 up to 36. On the low end, base 2 is the lowest useful system; base 1 would only have "0" as a symbol, which is pretty useless for counting. On the high end, 36 is chosen arbitrarily because we use symbols from "0123456789abcdefghijklmnopqrstuvwxyz" (10 digits + 26 characters) - you could continue with more symbols, but it is not really clear what to use after z.

"Normal" math is base-10 (uses symbols "0123456789"):

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

Binary is base-2 (uses symbols "01"):

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

"3" makes no sense in base 2; it only uses symbols "0" and "1", "3" is an invalid symbol (it's kind of like trying to book an appointment for the 34th of January).

int("333", 4)   # == 3*(4**2) + 3*(4**1) + 3*(4**0)
                # == 3*16 + 3*4 + 3*1
                # == 48 + 12 + 3
                # == 63
like image 81
Hugh Bothwell Avatar answered Sep 18 '22 08:09

Hugh Bothwell