Fonction bytearray() Python
Fonctions intégrées de Python3
bytearray()C'est une fonction intégrée à Python utilisée pour créer des séquences d'octets modifiables.
bytearray est très similaire à bytes, mais avec une différence essentielle : bytearray est modifiable, et ses octets peuvent être modifiés à tout moment. Cela le rend très utile dans les cas où des données binaires doivent être modifiées fréquemment.
Définition du mot: bytearrayCela signifie « tableau d'octets » et est une séquence d'octets modifiable.
Syntaxe de base et paramètres
Format de syntaxe
bytearray(source) bytearray(source, encoding) bytearray(source, encoding, errors)
Description du paramètre
- Source du paramètre:
- Type : entier, objet itérable, chaîne de caractères
- Description : données source utilisées pour initialiser le tableau d'octets.
Description de la fonction
- Valeur de retour: renvoie un objet bytearray modifiable.
- Caractéristiques: les éléments d'octets qu'il contient peuvent être modifiés.
Exemples
Exemple 1 : création d'un bytearray
Exemple
# Créer un bytearray de longueur spécifiée
b = bytearray(5)
print(b) # Sortie : bytearray(b'\x00\x00\x00\x00\x00')
# Créer à partir d'un objet itérable
b = bytearray([72, 101, 108, 108, 111])
print(b) # Sortie : bytearray(b'Hello')
# Créer à partir d'une chaîne
b = bytearray(Hello, encoding='utf-8')
print(b) # Sortie : bytearray(b'\xe4\xbd\xa0\xe5\xa5\xbd')
# bytearray vide
b = bytearray()
print(b) # Sortie : bytearray(b'')
b = bytearray(5)
print(b) # Sortie : bytearray(b'\x00\x00\x00\x00\x00')
# Créer à partir d'un objet itérable
b = bytearray([72, 101, 108, 108, 111])
print(b) # Sortie : bytearray(b'Hello')
# Créer à partir d'une chaîne
b = bytearray(Hello, encoding='utf-8')
print(b) # Sortie : bytearray(b'\xe4\xbd\xa0\xe5\xa5\xbd')
# bytearray vide
b = bytearray()
print(b) # Sortie : bytearray(b'')
Sortie attendue :
bytearray(b'\x00\x00\x00\x00\x00') bytearray(b'Hello') bytearray(b'\xe4\xbd\xa0\xe5\xa5\xbd') bytearray(b'')
Analyse du code :
- La méthode de création est similaire à celle de bytes.
- bytearray peut être initialisé de plusieurs manières.
Exemple 2 : Modification de bytearray
Exemple
# bytearray est mutable
b = bytearray("Hello", encoding='utf-8')
print(b) # Sortie : bytearray(b'Hello')
# Modifier un seul octet
b[0] = 74 # Code ASCII de 'J'
print(b) # Sortie : bytearray(b'Jello')
# Ajouter un octet
b.append(33) # Code ASCII de '!'
print(b) # Sortie : bytearray(b'Jello!')
# Étendre
b.extend([33, 33])
print(b) # Sortie : bytearray(b'Jello!!!')
# Supprimer
del b[0]
print(b) # Sortie : bytearray(b'ello!!!')
b = bytearray("Hello", encoding='utf-8')
print(b) # Sortie : bytearray(b'Hello')
# Modifier un seul octet
b[0] = 74 # Code ASCII de 'J'
print(b) # Sortie : bytearray(b'Jello')
# Ajouter un octet
b.append(33) # Code ASCII de '!'
print(b) # Sortie : bytearray(b'Jello!')
# Étendre
b.extend([33, 33])
print(b) # Sortie : bytearray(b'Jello!!!')
# Supprimer
del b[0]
print(b) # Sortie : bytearray(b'ello!!!')
Sortie attendue :
bytearray(b'Hello') bytearray(b'Jello') bytearray(b'Jello!') bytearray(b'Jello!!!') bytearray(b'ello!!!')
bytearray prend en charge un large éventail d'opérations de modification : affectation par index, append, extend, del, etc.
Fonctions intégrées de Python3
Autres extensions