I have recently started learning Python and have so much confusion,
I have two input bytearrays
,
x = bytearray(b'\xac\xe0\x1f\x15n\x99\xf1\xce\xba\xba\x8d\x9a\xda-JG')
y = bytearray(b'\x9d\x93\xcd\x0f(\xa8\xd6\xa9\xea\x10\x8d_\xbd7\xc6Y')
When I print them,
print(x)
print(y)
it gives exactly the same output.
I now want to zip
these two variables,x
and y
.
Since we use list
to see the content of zip
in Python 3,
I used the following code,
print( list(zip(x,y)) )
and here is the output,
[(172, 157), (224, 147), (31, 205), (21, 15), (110, 40), (153, 168), (241, 214),
(206, 169), (186, 234), (186, 16), (141, 141), (154, 95), (218, 189), (45, 55),
(74, 198), (71, 89)]
I don't understand Why it is converted to Decimal values ?
It's not "converted to decimal values." A bytearray
is a list of bytes. Bytes are numbers. Numbers can be represented in any base without changing their value. So the numbers stay the same; only their representation changes. The reason for this is that bytearray
represents its contents in hex, while the tuple
contains individual int
s, which have a default decimal representation. This applies only when printing; the following, for example, is already true.
assert x[0] == 172
After the numbers pass through zip
, in other words, they are no longer part of a bytearray
and Python no longer has any way to know it should print them in hex.