[Tutor] built in functions int(),long()
lonetwin
lonetwin@yahoo.com
Wed Jun 18 03:26:01 2003
Hi there,
On Wednesday 18 Jun 2003 12:09 pm, cino hilliard wrote:
> >>>long('123',2)
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> ValueError: invalid literal for long(): 123
This is correct because '123' is an invalid literal in a base 2 number
system.
int and long convert ^^strings^^ to ^^numbers^^. The optional second
argument is to specify what radix the first (string) argument is in. For
example:
--------------------------------------------
>>> int('01',2)
1
>>> int('10',2)
2
>>> int('11',2)
3
>>> int(11)
11
>>>
--------------------------------------------
If the second argument is zero, the radix is guessed. For example:
--------------------------------------------
>>> int('0xff', 0)
255
>>>
--------------------------------------------
I guess you mistakenly assumed that the int() and float() builtins also
convert the number strings into the base radix that you provide as the second
argument.
HTH
Peace
Steve
--
Never be afraid to tell the world who you are.
- Anonymous