900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > Python bytes 函数 - Python零基础入门教程

Python bytes 函数 - Python零基础入门教程

时间:2020-06-03 09:52:05

相关推荐

Python bytes 函数 - Python零基础入门教程

目录

一.bytes 函数简介二.bytes 函数使用 1.定义空的字节序列 bytes2.定义指定个数的字节序列 bytes ,默认以 0 填充,不能是浮点数3.定义指定内容的字节序列 bytes ,只能是整数类型的序列,否则异常4.定义个字节序列 bytes 三.重点提醒四.猜你喜欢

零基础 Python 学习路线推荐 :Python 学习目录>>Python 基础入门

Python bytes 对于刚接触 Python 的小伙伴来讲,可能还是有点陌生!bytes 是字节序列,值得注意的是它有取值范围:0 <= bytes <= 255;凡是输出前面带有字符 b 标识的都是字节序列 bytes ;

一.bytes 函数简介

Python bytes 字节序列有以下几种使用方式:

"""bytes(iterable_of_ints) -> bytesbytes(string, encoding[, errors]) -> bytesbytes(bytes_or_buffer) -> immutable copy of bytes_or_bufferbytes(int) -> bytes object of size given by the parameter initialized with null bytesbytes() -> empty bytes objectConstruct an immutable of bytes from:- an iterable yielding integers in range(256)- a text string encoded using the specified encoding- any object implementing the buffer API.- an integer# (copied from class doc)"""#1.定义空的字节序列bytesbytes()->emptybytes#2.定义指定个数的字节序列bytes,默认以0填充,不能是浮点数bytes(int)->bytesofsizegivenbytheparameterinitializedwithnullbytes#3.定义指定内容的字节序列bytesbytes(iterable_of_ints)#4.定义字节序列bytes,如果包含中文的时候必须设置编码格式bytes(string,encoding[,errors])->immutablecopyofbytes_or_buffer

**返回值 : 返回一个新的字节序列,字节序列 bytes 有一个明显的特征,输出的时候最前面会有一个字符 b 标识,**举个例子:

b'\x64\x65\x66'b'iloveyou'b''

凡是输出前面带有字符 b 标识的都是字节序列 bytes ;

二.bytes 函数使用

1.定义空的字节序列 bytes

#!usr/bin/envpython#-*-coding:utf-8_*-"""@Author:何以解忧@Blog(个人博客地址):@Github:@File:python_bytes.py@Time:/2/2521:25@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!"""if__name__=="__main__":a=bytes()print(a)print(type(a))'''输出结果:b''<class 'bytes'>'''

2.定义指定个数的字节序列 bytes ,默认以 0 填充,不能是浮点数

if__name__=="__main__":b1=bytes(10)print(b1)print(type(b1))#bytes通过decode函数转为str类型s1=b1.decode()print("s1:",s1)print(type(s1))'''输出结果:b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'<class 'bytes'>s1:<class 'str'>'''

3.定义指定内容的字节序列 bytes ,只能是整数类型的序列,否则异常

if__name__=="__main__":#正常输出b1=bytes([1,2,3,4])>>>b'\x01\x02\x03\x04'#bytes字节序列必须是0~255之间的整数,不能含有float类型b1=bytes([1.1,2.2,3,4])>>>TypeError:'float'objectcannotbeinterpretedasaninteger#bytes字节序列必须是0~255之间的整数,不能含有str类型b1=bytes([1,'a',2,3])>>>TypeError:'str'objectcannotbeinterpretedasaninteger#bytes字节序列必须是0~255之间的整数,不能大于或者等于256b1=bytes([1,257])>>>ValueError:bytesmustbeinrange(0,256)

4.定义个字节序列 bytes

if__name__=="__main__":b1=bytes('abc','utf-8')#如果包含中文必须设置编码格式print(b1)print("***"*20)b2=bytes(b'def')print(b2)print(type(b2))print(id(b2))print("***"*20)b3=b'\x64\x65\x66'print(b3)print(type(b3))print(id(b3))print("***"*20)#result=Trueifb2==b3elseFalseprint("b==bb的结果是",(b2==b3))print("bisbb的结果是",(b2isb3))'''输出结果:b'abc'************************************************************b'def'<class 'bytes'>2563018794448************************************************************b'def'<class 'bytes'>2563018794448************************************************************b == bb 的结果是 Trueb is bb 的结果是 True'''

注意:

1.**Python is 和==的区别 **文章中有详细介绍:== 是 Python 标准操作符中的比较操作符,用来比较判断两个对象的 value (值)是否相等,例如下面两个字符串间的比较;

2.is 也被叫做同一性运算符,这个运算符比较判断的是对象间的唯一身份标识,也就是 id 是否相同;

3.如果 bytes 初始化含有中文的字符串必须设置编码格式,否则报错:TypeError: string argument without an encoding,如下:

b=bytes("猿说python")>>>b=bytes("猿说python")>>>TypeError:stringargumentwithoutanencoding

三.重点提醒

1.bytes 字节序列的取值范围:必须是 0 ~ 255 之间的整数;

2.bytes 字节序列是不可变序列:bytes 是不可变序列,即和 str 类型一样不可修改,如果通过 find 、replace 、islower 等函数修改,其实是创建了新的 bytes 、str 对象,可以通过内置函数 id 查看值 是否发生变化,示例如下:

if__name__=="__main__":#1.通过replace生成新的bytes字节序列b1=bytes(b"abcdefg")print(b1)print(type(b1))print(id(b1))print("***"*20)b2=bytes.replace(b1,b"cd",b"XY")print(b2)print(type(b2))print(id(b2))print("***"*20)#2.bytes是不可变序列,不能直接修改bytes的内容b1[0]=b"ss">>>TypeError:'bytes'objectdoesnotsupportitemassignment'''输出结果:b'abcdefg'<class 'bytes'>2264724270976************************************************************b'abXYefg'<class 'bytes'>2264707281104************************************************************'''

Python 除了 bytes 字节序列之外,还有 bytearray 可变的字节序列,具体区别在哪呢?我们后续继续讲解;

四.猜你喜欢

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 bytes 函数

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。