Ask Your Question
0

how to write .digit() ??

asked 2011-10-19 17:06:10 +0200

teepan123 gravatar image

I create function that want to see result of example 128.digit() = [1,1,0,0,0,0,..] like this (not sure)

but when I write in python, it has function .digit() too but it has different use and it can't use with integer number (it use to check digit, right?)

how can i write fucntion .digit() just like in sage into my python program?

thank you for your kindness

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2011-10-19 20:26:49 +0200

I'm not sure what [1, 1, 0, 0, 0, 0, ...] has to do with 128, but if you want the base 10 digits:

>>> a = 1234
>>> [int(d) for d in str(a)]
>>> [1, 2, 3, 4]

For base 2 instead of base 10, try Google. I found http://stackoverflow.com/questions/187273/base-2-binary-representation-using-python, for example.

edit flag offensive delete link more

Comments

There's also the .digits() method, which has a base option (e.g. 1234.digits(2) = [0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1]).

DSM gravatar imageDSM ( 2011-10-19 20:34:27 +0200 )edit

I thought the question was how to do it in Python, not in Sage.

John Palmieri gravatar imageJohn Palmieri ( 2011-10-19 20:44:04 +0200 )edit

Ah, you're right. I got it backwards.

DSM gravatar imageDSM ( 2011-10-19 21:10:21 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-10-19 17:06:10 +0200

Seen: 3,044 times

Last updated: Oct 19 '11