Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
YouTube Security VideosGoogle Cloud Tech: Vibe coding in the pit lane 🏁(23.09.2026 um 01:00 Uhr)
Sichere ProgrammierungBuild an Explainable Vendor-Risk Gate in Node.js(23.09.2026 um 00:27 Uhr)
Sichere ProgrammierungFrom p=none to Enforcement: A Working Sequence for DMARC Rollout(23.09.2026 um 00:40 Uhr)
Sichere ProgrammierungWhen OPA's Bundle Loader Runs Past a `.manifest` Typo(23.09.2026 um 00:53 Uhr)
Sichere ProgrammierungGovernance Attack Surface Review: Bybit(23.09.2026 um 01:00 Uhr)
Linux Tipps & HardeningOpenShot video editor is now available as a snap(23.09.2026 um 00:09 Uhr)
KI & AI VideosAI Revolution: AI Robots Are Beating Humans Now(23.09.2026 um 00:32 Uhr)
YouTube Security VideosGoogle Cloud Tech: Vibe coding in the pit lane 🏁(23.09.2026 um 01:00 Uhr)
Sichere ProgrammierungBuild an Explainable Vendor-Risk Gate in Node.js(23.09.2026 um 00:27 Uhr)
Sichere ProgrammierungFrom p=none to Enforcement: A Working Sequence for DMARC Rollout(23.09.2026 um 00:40 Uhr)
Sichere ProgrammierungWhen OPA's Bundle Loader Runs Past a `.manifest` Typo(23.09.2026 um 00:53 Uhr)
Sichere ProgrammierungGovernance Attack Surface Review: Bybit(23.09.2026 um 01:00 Uhr)
Linux Tipps & HardeningOpenShot video editor is now available as a snap(23.09.2026 um 00:09 Uhr)
KI & AI VideosAI Revolution: AI Robots Are Beating Humans Now(23.09.2026 um 00:32 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Bytearray in Python (3)

Buy Me a Coffee☕ *Memo: My post explains a bytearray (1). My post explains bytearray(). My post explains a bytes (1). My post explains a string (1). A bytearray can be read by indexing and slicing as shown b…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

Buy Me a Coffee



*Memo:





A bytearray can be read by indexing and slicing as shown below:



*Memo:




  • Indexing can be done with one or more [index] in the range [The 1st element index, The last element index]:



    • index(Required-Type:int):


      • Don't use index=.






    • Error occurs if index is out of range.






  • Slicing can be done with one or more [start:end:step] in the range [start, end):



    • start(Optional-Default:None-Type:int and NoneType):


      • It's a start index(inclusive).

      • If it's None, it's the 1st element index.

      • Don't use start=.







    • end(Optional-Default:None-Type:int and NoneType):


      • It's an end index(exclusive).

      • If it's None, it's the next index to the last element index.

      • Don't use end=.







    • step(Optional-Default:None-Type:int and NoneType):


      • It's the interval of indices.

      • If it's None, it's 1.

      • It cannot be zero.

      • Don't use end=.






    • The [] with at least one : is slicing.


    • Error doesn't occur even if [start, end) is out of the range [The 1st element index, The last element index].







  • index, start and end can be signed indices(zero and positive and negative indices).





v = bytearray(b'abcdefgh')

print(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7])
print(v[-8], v[-7], v[-6], v[-5], v[-4], v[-3], v[-2], v[-1])
# 97 98 99 100 101 102 103 104

v[1] = ord('B')
v[-7] = ord('B')

print(v)
# bytearray(b'aBcdefgh')

v[3] = ord('D')
v[-5] = ord('D')

print(v)
# bytearray(b'aBcDefgh')

v[6] = ord('G')
v[-2] = ord('G')

v[1], v[3], v[6] = ord('B'), ord('D'), ord('G')
v[-7], v[-5], v[-2] = ord('B'), ord('D'), ord('G')

print(v)
# bytearray(b'aBcDefGh')

del v[1], v[2], v[4]
# del v[-7], v[-5], v[-2]

print(v)
# bytearray(b'acefh')

del v

print(v)
# NameError: name 'v' is not defined









v = bytearray(b'abcdefgh')

print(v[-9], v[8])
# IndexError: bytearray index out of range









v = bytearray(b'ABCDEFGH')

print(v[:])
print(v[::])
print(v[None:None:None])
print(v[0:8:1])
print(v[-100:100:1])
# bytearray(b'ABCDEFGH')

print(v[::2])
# bytearray(b'ACEG')

print(v[::-2])
# bytearray(b'HFDB')

print(v[2:])
print(v[-6:])
print(v[2::])
print(v[-6::])
# bytearray(b'CDEFGH')

print(v[2::2])
print(v[-6::2])
# bytearray(b'CEG')

print(v[2::-2])
print(v[-6::-2])
# bytearray(b'CA')

print(v[:6])
print(v[:-2])
print(v[:6:])
print(v[:-2:])
# bytearray(b'ABCDEF')

print(v[:6:2])
print(v[:-2:2])
# bytearray(b'ACE')

print(v[:6:-2])
print(v[:-2:-2])
# bytearray(b'H')

print(v[2:6])
print(v[-6:-2])
print(v[2:6:])
print(v[-6:-2:])
# bytearray(b'CDEF')

print(v[2:6:2])
print(v[-6:-2:2])
# bytearray(b'CE')

print(v[2:6:-2])
print(v[-6:-2:-2])
# bytearray(b'')









v = bytearray(b'ABCDEFGH')

print(v)
# bytearray(b'ABCDEFGH')

print(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7])
print(v[-8], v[-7], v[-6], v[-5], v[-4], v[-3], v[-2], v[-1])
# 65 66 67 68 69 70 71 72

print(v[:])
print(v[::])
print(v[None:None:None])
print(v[0:8:1])
print(v[-100:100:1])
# bytearray(b'ABCDEFGH')

print(v[::2])
# bytearray(b'ACEG')

print(v[::-2])
# bytearray(b'HFDB')

print(v[2:])
print(v[-6:])
print(v[2::])
print(v[-6::])
# bytearray(b'CDEFGH')

print(v[2::2])
print(v[-6::2])
# bytearray(b'CEG')

print(v[2::-2])
print(v[-6::-2])
# bytearray(b'CA')

print(v[:6])
print(v[:-2])
print(v[:6:])
print(v[:-2:])
# bytearray(b'ABCDEF')

print(v[:6:2])
print(v[:-2:2])
# bytearray(b'ACE')

print(v[:6:-2])
print(v[:-2:-2])
# bytearray(b'H')

print(v[2:6])
print(v[-6:-2])
print(v[2:6:])
print(v[-6:-2:])
# bytearray(b'CDEF')

print(v[2:6:2])
print(v[-6:-2:2])
# bytearray(b'CE')

print(v[2:6:-2])
print(v[-6:-2:-2])
# bytearray(b'')









v = bytearray(b'ABCDEFGH')

print(v[-9], v[8])
# IndexError: bytearray index out of range






A bytearray can be changed by indexing, slicing and a del statement as shown below:



*Memo:




  • An iterable must be assigned to a sliced variable.

  • A del statement can remove zero or more elements from a bytearray by indexing and slicing and can remove one or more variables themselves.




v = bytearray(b'abcdef')

v[1] = ord(b'X')
v[3:5] = [ord(b'Y'), ord(b'Z')]

print(v)
# bytearray(b'aXcYZf')









v = bytearray(b'abcdef')

del v[1], v[3:5]

print(v)
# bytearray(b'acd')









v = bytearray(b'abcdef')

del v

print(v)
# NameError: name 'v' is not defined






A bytearray can be continuously used through multiple variables as shown below:




v1 = v2 = v3 = bytearray(b'abcde') # Equivalent
# v1 = bytearray(b'abcde')
v1[0] = ord(b'X') # v2 = v1
v2[3:5] = [ord(b'Y'), ord(b'Z')] # v3 = v2
del v3[1:3]

print(v1) # bytearray(b'XYZ')
print(v2) # bytearray(b'XYZ')
print(v3) # bytearray(b'XYZ')






A bytearray can be shallow-copied but cannot be deep-copied as shown below:






<Shallow & Deep copy>:



*Memo:





  • v1 and v2 refer to different bytearrays and each same byte.


  • is keyword can check if v1 and v2 refer to the same bytearray and each same byte.


  • bytearray.copy(), copy.copy(), bytearray() and slicing can shallow-copy a bytearray:



    • bytearray.copy() has no arguments.








  • copy.deepcopy() cannot deep-copy but can shallow-copy a bytearray.





import copy

v1 = bytearray(b'abcde')
v2 = v1.copy()
v2 = copy.copy(v1)
v2 = bytearray(v1)
v2 = v1[:]
v2 = copy.deepcopy(v1)

print(v1, v1[2]) # bytearray(b'abcde') 99
print(v2, v2[2]) # bytearray(b'abcde') 99

print(v1 is v2, v1[2] is v2[2])
# False True

v2[1]= ord('X')
v2[3]= ord('Y')
# ↓ ↓
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'aXcYe')
# ↑ ↑


Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Bytearray in Python (3)

Thematisch verwandte Begriffe: Bytearray, Python · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-58268 | SIPGO is a library for writing SIP services in the GO language. Prior to…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick