[Tutor] Is there a Big Integer Class in Python?

Danny Yoo dyoo at hashcollision.org
Tue Dec 22 17:17:19 EST 2015


On Tue, Dec 22, 2015 at 6:22 AM, Satya Luzy <wuzzyluzy at gmail.com> wrote:
> Is there a way to extend the numbers so that there is no more L?


Data can be presented in many different ways.  That's something that
you should have control over.  Rather than change the definition of
how numbers are representing in the machine, or do something
fundamental to change their internal structure, you can just present
them in the way you want.

A common term for presenting data for humans to consume is called
"formatting".  Take a look at:

    https://pyformat.info/

If we format a large number, it should show up in a form without the
"L" that signifies a long integer.

################################################################
>>> '{}'.format(2 ** 500)
'3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376'
################################################################


But this is not the only way that numbers can be presented.  For
example, we might consider using the 'humanize' library:

    https://pypi.python.org/pypi/humanize

#############################
import humanize
print(humanize.intcomma(2**100))
print(humanize.intword(2**100))
#############################

which, when I run it, shows:

######################################
1,267,650,600,228,229,401,496,703,205,376
1.3 nonillion
######################################


The main point is: presenting data for humans to consume is something
that *you* can control.  You can write functions from numbers to
strings, and those functions can do arbitrary things.


More information about the Tutor mailing list