bytearray1.py 664 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. print(bytearray(4))
  2. a = bytearray([1, 2, 200])
  3. print(type(a))
  4. print(a[0], a[2])
  5. print(a[-1])
  6. print(a)
  7. a[2] = 255
  8. print(a[-1])
  9. a.append(10)
  10. print(len(a))
  11. s = 0
  12. for i in a:
  13. s += i
  14. print(s)
  15. print(a[1:])
  16. print(a[:-1])
  17. print(a[2:3])
  18. print(str(bytearray(b"123"), "utf-8"))
  19. # Comparisons
  20. print(bytearray([1]) == bytearray([1]))
  21. print(bytearray([1]) == bytearray([2]))
  22. print(bytearray([1]) == b"1")
  23. print(b"1" == bytearray([1]))
  24. print(bytearray() == bytearray())
  25. # comparison with other type should return False
  26. print(bytearray() == 1)
  27. # TODO: other comparisons
  28. # __contains__
  29. b = bytearray(b"\0foo\0")
  30. print(b"foo" in b)
  31. print(b"foo\x01" in b)
  32. print(b"" in b)