How to define a petabyte constant in Python

1 Answer

0 votes
"""
In Python, integers have arbitrary precision.
This means they can grow as large as needed, limited only by memory.

A petabyte fits easily:

    1 PiB = 2**50  = 1,125,899,906,842,624 bytes
    1 PB  = 10**15 = 1,000,000,000,000,000 bytes

We define constants using uppercase names (Python convention).
"""

# Binary petabyte (pebibyte), using a bit shift.
# 1 << 50 = 2**50 bytes.
PETABYTE_PIB = 1 << 50 # not a constant - just don't change it 

# Decimal petabyte (SI petabyte), using a readable numeric literal.
# Python allows underscores for digit grouping.
PETABYTE_PB = 1_000_000_000_000_000

print("1 PiB =", PETABYTE_PIB, "bytes")
print("1 PB  =", PETABYTE_PB, "bytes")


'''
run:

1 PiB = 1125899906842624 bytes
1 PB  = 1000000000000000 bytes

'''

 



answered May 3 by avibootz
...