目录
- 一.Python bytearray 函数简介
- 二.Python bytearray 函数使用
- 三.bytearray 与 bytes 区别
- 1. bytes 不可变字节序列
- 2.bytearray 可变字节序列
- 四.猜你喜欢
基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门
Python 除了 bytes 字节序列 之外,还有 bytearray 可变的字节序列,具体区别在哪呢?顾名思义,bytes 是不可变的,而 bytearray 是可变的!具体本文会有详细的讲解!
一.Python bytearray 函数简介
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:Python bytearray 函数.py
@Time:2021/05/04 07:37
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!"""# 1.定义空的字节序列bytearray
bytearray() -> empty bytearrayarray# 2.定义指定个数的字节序列bytes,默认以0填充,不能是浮点数
bytearray(int) -> bytes array of size given by the parameter initialized with null bytes# 3.定义指定内容的字节序列bytes
bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer# 4.定义指定内容的字节序列bytes
bytearray(string, encoding[, errors]) -> bytearray# 5.定义指定内容的字节序列bytes,只能为int 类型,不能含有float 或者 str等其他类型变量
bytearray(iterable_of_ints) -> bytearray
返回值 : 返回一个新的可变字节序列,可变字节序列 bytearray 有一个明显的特征,输出的时候最前面会有一个字符 b 标识,举个例子:
b'\x64\x65\x66'
b'i love you'
b'https://www.codersrc.com'
凡是输出前面带有字符 b 标识的都是字节序列 ;bytearray 可变的字节序列,bytes 是不可变的字节序列;
二.Python bytearray 函数使用
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:Python bytearray 函数.py
@Time:2021/05/04 07:37
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!"""if __name__ == "__main__":# 定义空的字节序列bytearrayb1 = bytearray()print(b1)print(type(b1))print("***"*20)# 定义指定个数的字节序列bytes,默认以0填充,不能是浮点数b2 = bytearray(10)print(b2)print(type(b2))print("***" * 20)# 定义指定内容的字节序列bytesb3 = bytes('abc', 'utf-8')print(b3)print(type(b3))print("***" * 20)# 正常输出b1 = bytearray([1, 2, 3, 4])>> > b'\x01\x02\x03\x04'# bytes字节序列必须是 0 ~ 255 之间的整数,不能含有float类型b1 = bytearray([1.1, 2.2, 3, 4])>> > TypeError: an integer is required# bytes字节序列必须是 0 ~ 255 之间的整数,不能含有str类型b1 = bytearray([1, 'a', 2, 3])>> > TypeError: an integer is required# bytes字节序列必须是 0 ~ 255 之间的整数,不能大于或者等于256b1 = bytearray([1, 257])>> > ValueError: bytes must be in range(0, 256)'''
输出结果:bytearray(b'')
<class 'bytearray'>
************************************************************
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
<class 'bytearray'>
************************************************************
b'abc'
<class 'bytes'>
************************************************************
'''
三.bytearray 与 bytes 区别
- 相同点:bytearray 与 bytes 取值范围都是 0 ~ 256 ;
- 不同点:bytearray 可变的字节序列,bytes 是不可变的字节序列 ;
1. bytes 不可变字节序列
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:Python bytearray 函数.py
@Time:2021/05/04 07:37
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!"""if __name__ == "__main__":# bytes不可变字节序列b1 = b"abcd"for i in b1:print(i,end=" ")print()b1[0] = "A"'''
输出结果:97 98 99 100
Traceback (most recent call last):File "E:/Project/python/python_project/untitled10/123.py", line 22, in <module>b1[0] = "A"
TypeError: 'bytes' object does not support item assignment
'''
2.bytearray 可变字节序列
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:Python bytearray 函数.py
@Time:2021/05/04 07:37
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!"""if __name__ == "__main__":# bytearray可变字节序列b1 = b"abcd"b2 = bytearray(b1)print("修改之前:",b2)b2[0] = 65print("修改之后:", b2)'''
输出结果:修改之前: bytearray(b'abcd')
修改之后: bytearray(b'Abcd')
'''
四.猜你喜欢
- Python for 循环
- Python 字符串
- Python 列表 list
- Python 元组 tuple
- Python 字典 dict
- Python 条件推导式
- Python 列表推导式
- Python 字典推导式
- Python 函数声明和调用
- Python 不定长参数 *argc/**kargcs
- Python 匿名函数 lambda
- Python return 逻辑判断表达式
- Python 字符串/列表/元组/字典之间的相互转换
- Python 局部变量和全局变量
- Python type 函数和 isinstance 函数区别
- Python is 和 == 区别
- Python 可变数据类型和不可变数据类型
- Python 浅拷贝和深拷贝
未经允许不得转载:猿说编程 » Python bytearray 函数