在线购物网站开发,二手房在哪个网站做合同,制作网页时关于可以采用的图像文件格式正确的描述是,珠江夜游微信公众号目录
一.Python bytearray 函数简介二.Python bytearray 函数使用三.bytearray 与 bytes 区别 1. bytes 不可变字节序列2.bytearray 可变字节序列 四.猜你喜欢 基础 Python 学习路线推荐 : Python 学习目录 Python 基础入门 Python 除了 bytes 字节序列 之外#xf…目录
一.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
bi love you
bhttps://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
************************************************************
babc
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 babcdfor 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 moduleb1[0] A
TypeError: bytes object does not support item assignment2.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 babcdb2 bytearray(b1)print(修改之前,b2)b2[0] 65print(修改之后, b2)
输出结果修改之前 bytearray(babcd)
修改之后 bytearray(bAbcd)四.猜你喜欢
Python for 循环Python 字符串Python 列表 listPython 元组 tuplePython 字典 dictPython 条件推导式Python 列表推导式Python 字典推导式Python 函数声明和调用Python 不定长参数 *argc/**kargcsPython 匿名函数 lambdaPython return 逻辑判断表达式Python 字符串/列表/元组/字典之间的相互转换Python 局部变量和全局变量Python type 函数和 isinstance 函数区别Python is 和 区别Python 可变数据类型和不可变数据类型Python 浅拷贝和深拷贝
未经允许不得转载猿说编程 » Python bytearray 函数