What are those underscores in a number in Python?



For example, we can see many examples where underscores appearing in a number in Python such as


a = 1_000_000

Explanation 

In English speaking countries, commas are generally used as thousand separators, while in many other countries, periods are used as thousand separators. Given the differing conventions, and the fact that both commas and periods are used for other things in Python, it was decided to use underscores as separators [1]. 

Python allows you to put underscores in numbers for convenience. They're used to separate groups of numbers, much like commas do in non-programming (e.g., 1,000,000). Underscores are completely ignored in numbers, much like comments [1]. So this:

a = 1_000_000
print(a)

Output:

1000000

References 
[1]. https://stackoverflow.com/questions/54009778/what-do-underscores-in-a-number-mean

No comments:

Post a Comment