目录

基础 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'x64x65x66'
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__":

    # 定义空的字节序列bytearray
    b1 = bytearray()
    print(b1)
    print(type(b1))
    print("***"*20)

    # 定义指定个数的字节序列bytes,默认以0填充,不能是浮点数
    b2 = bytearray(10)
    print(b2)
    print(type(b2))
    print("***" * 20)

    # 定义指定内容的字节序列bytes
    b3 = bytes('abc', 'utf-8')
    print(b3)
    print(type(b3))
    print("***" * 20)

    # 正常输出
    b1 = bytearray([1, 2, 3, 4])
    >> > b'x01x02x03x04'

    # 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 之间的整数,不能大于或者等于256
    b1 = bytearray([1, 257])
    >> > ValueError: bytes  must be in range(0, 256)


'''
输出结果:

bytearray(b'')
<class 'bytearray'>
************************************************************
bytearray(b'x00x00x00x00x00x00x00x00x00x00')
<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] = 65
    print("修改之后:", b2)

'''
输出结果:

修改之前: bytearray(b'abcd')
修改之后: bytearray(b'Abcd')
'''

四.猜你喜欢

  1. Python for 循环
  2. Python 字符串
  3. Python 列表 list
  4. Python 元组 tuple
  5. Python 字典 dict
  6. Python 条件推导式
  7. Python 列表推导式
  8. Python 字典推导式
  9. Python 函数声明和调用
  10. Python 不定长参数 *argc/**kargcs
  11. Python 匿名函数 lambda
  12. Python return 逻辑判断表达式
  13. Python 字符串/列表/元组/字典之间的相互转换
  14. Python 局部变量和全局变量
  15. Python type 函数和 isinstance 函数区别
  16. Python is 和 == 区别
  17. Python 可变数据类型和不可变数据类型
  18. Python 浅拷贝和深拷贝

未经允许不得转载:猿说编程 » Python bytearray 函数

本文由博客 - 猿说编程 猿说编程 发布!

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/shuopython/p/15042352.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!