25

I want to specify a sequence of large integers (with many zeros) like:

a = [1e13, 1e14, 1e19, ...]

My intuition is to use scientific notation. But in python, it's a float instead of integer. Is there a easy way in python to write these integer literals without writing all the zeros, because making sure the number of zeros correct is a nightmare.

I believe I can cast the floats back to integer using int, but just wonder if there is a better way?

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
Gary
  • 1,828
  • 1
  • 20
  • 27
  • 3
    Will `a = [10 ** 13, 10 ** 14, 10 ** 19]` work for you? – thefourtheye Jul 10 '15 at 13:21
  • 1
    There is no concise way to specify large decimal integer literals in Python ([grammar spec](https://docs.python.org/3/reference/lexical_analysis.html#integer-literals)). If you're lucky, CPython's peephole optimiser will replace `10**13` with `10000000000000` at compile time. – Alex Riley Jul 10 '15 at 13:37
  • 5
    Take care with that float to integer *conversion* (nitpick: Python doesn't have "casts"). It's not always going to give you exactly the value you expect. E.g., `int(1e23)` gives `99999999999999991611392`. – Mark Dickinson Jul 10 '15 at 14:05

6 Answers6

22

For future viewers.

Since python 3.6 PEP 515 would be included.

So you can do a = 1_000_000_000_000 for better code's readability.

vishes_shell
  • 22,409
  • 6
  • 71
  • 81
8

Using * and **, it's not beautiful but gives you the right result.

print(2e10)
20000000000.0

print(2*10**10)
20000000000

You can also generate a whole list like this:

result = []
for i in range(5):
    result.append(1*10**i)


>>> result
[1, 10, 100, 1000, 10000]

If you really want to use the scientific notation, then

result = []
for n in range(5):
    result.append(int(float(f"1e{n}"))) #converting to int directly gives: ValueError: invalid literal for int() with base 10: '1e0'



>>> result
[1, 10, 100, 1000, 10000]
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
  • @user2357112 Thank you for the edit. I missed the rounding error at 1e23. An alternative, very ugly and expensive, solution would be `a = list(map(int, map(Decimal, map(str, [1e13, 1e14, 1e19, 1e23]))))`. – alec_djinn Sep 10 '22 at 07:12
3

In your input, you can specify integers with underscores.

Since python 3.10, you can also use pprint library methods, such as pprint.pprint and pprint.pformat with underscore_numbers=True to output integers with underscores. This has no effect on non-integer numbers.

Examples:

from pprint import pformat

nums = [1000000, 1_000_000, 1e6, 1000000.1]
for num in nums:
    print(num, pformat(num), pformat(num, underscore_numbers=True))

# 1000000 1000000 1_000_000
# 1000000 1000000 1_000_000
# 1000000.0 1000000.0 1000000.0
# 1000000.1 1000000.1 1000000.1

SEE ALSO:

python human readable large numbers

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
1

You can setup some suffixes

K=1000
M=K*K
G=K*M
T=K*G
P=K*T
E=K*P
print 12*P

or use the longer names (kilo, etc).

meuh
  • 11,500
  • 2
  • 29
  • 45
1

You could use comma separators for thousands, as a string, then convert to an int.

import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') 
def bigint(s):
    return locale.atoi(s)

print bigint('1,000,000,000,000')
meuh
  • 11,500
  • 2
  • 29
  • 45
0

You could do something like this, which avoids float to int conversion issues and is fairly readable in my opinion:

a = map(int, ['1'+'0'*13, '1'+'0'*14, '1'+'0'*19, '1'+'0'*23])
for value in a:
    print(format(value, ','))

Output:

10,000,000,000,000
100,000,000,000,000
10,000,000,000,000,000,000
100,000,000,000,000,000,000,000
martineau
  • 119,623
  • 25
  • 170
  • 301