8

Is it possible to declare a number in Python as

a = 35_000
a = 35,000  

Neither seem to work of course. How do you emphasize things like these, for clarity in Python? Is it possible?

James Raitsev
  • 92,517
  • 154
  • 335
  • 470
  • Does this answer your question? [How to use digit separators for Python integer literals?](https://stackoverflow.com/questions/38155177/how-to-use-digit-separators-for-python-integer-literals) – Georgy Oct 29 '20 at 23:38

2 Answers2

10

This is actually just now possible in Python 3.6.

You can use the first format that you showed:

a = 35_000

because underscores are now an accepted separator in numbers. (You could even say a = 3_5_00_0, though why would you?)

The second method you showed will actually create a tuple. It's the same as saying:

a = (35, 000)  # Which is also the same as (35, 0).
Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56
3

Yes, this is possible starting with python 3.6.

PEP 515 adds the ability to use underscores in numeric literals for improved readability. For example:

>>> 1_000_000_000_000_000
1000000000000000
>>> 0x_FF_FF_FF_FF
4294967295
Anonymous
  • 11,740
  • 3
  • 40
  • 50