首页 > 代码库 > Python基础(二)
Python基础(二)
Python基础(二)
Python 运算符(算术运算、比较运算、赋值运算、逻辑运算、成员运算)
基本数据类型(数字、布尔值、字符串、列表、元组、字典、set集合)
for 循环
enumrate
range和xrange
编码与进制转换
Python 运算符
1、算术运算:
2、比较运算:
3、赋值运算:
4、逻辑运算:
5、成员运算:
基本数据类型
1、数字
int(整型)
在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647
在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
#返回表示该数字的时占用的最少位数 >>> (951).bit_length() 10 #返回绝对值 >>> (95).__abs__() 95 >>> (-95).__abs__() 95 #用来区分数字和字符串的 >>> (95).__add__(1) 96 >>> (95).__add__("1") NotImplemented #判断一个整数对象是否为0,如果为0,则返回False,如果不为0,则返回True >>> (95).__bool__() True >>> (0).__bool__() False #判断两个值是否相等 >>> (95).__eq__(95) True >>> (95).__eq__(9) False #判断是否不等于 >>> (95).__ne__(9) True >>> (95).__ne__(95) False #判断是否大于等于 >>> (95).__ge__(9) True >>> (95).__ge__(99) False #判断是否大于 >>> (95).__gt__(9) True >>> (95).__gt__(99) False #判断是否小于等于 >>> (95).__le__(99) True >>> (95).__le__(9) False #判断是否小于 >>> (95).__lt__(9) False >>> (95).__lt__(99) True #加法运算 >>> (95).__add__(5) 100 #减法运算 >>> (95).__sub__(5) 90 #乘法运算 >>> (95).__mul__(10) 950 #除法运算 >>> (95).__truediv__(5) 19.0 #取模运算 >>> (95).__mod__(9) 5 #幂运算 >>> (2).__pow__(10) 1024 #整除,保留结果的整数部分 >>> (95).__floordiv__(9) >>> #转换为整型 >>> (9.5).__int__() 9 #返回一个对象的整数部分 >>> (9.5).__trunc__() 9 #将正数变为负数,将负数变为正数 >>> (95).__neg__() -95 >>> (-95).__neg__() 95 #将一个正数转为字符串 >>> a = 95 >>> a = a.__str__() >>> print(type(a)) <class ‘str‘> #将一个整数转换成浮点型 >>> (95).__float__() 95.0 #转换对象的类型 >>> (95).__format__(‘f‘) ‘95.000000‘ >>> (95).__format__(‘b‘) ‘1011111‘ #在内存中占多少个字节 >>> a = 95 >>> a.__sizeof__() 28
class int(object): """ int(x=0) -> integer int(x, base=10) -> integer Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating point numbers, this truncates towards zero. If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by ‘+‘ or ‘-‘ and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal. >>> int(‘0b100‘, base=0) """ def bit_length(self): # real signature unknown; restored from __doc__ """ int.bit_length() -> int Number of bits necessary to represent self in binary. """ """ 表示该数字返回时占用的最少位数 >>> (951).bit_length() """ return 0 def conjugate(self, *args, **kwargs): # real signature unknown """ Returns self, the complex conjugate of any int.""" """ 返回该复数的共轭复数 #返回复数的共轭复数 >>> (95 + 11j).conjugate() (95-11j) #返回复数的实数部分 >>> (95 + 11j).real 95.0 #返回复数的虚数部分 >>> (95 + 11j).imag 11.0 """ pass @classmethod # known case def from_bytes(cls, bytes, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ """ int.from_bytes(bytes, byteorder, *, signed=False) -> int Return the integer represented by the given array of bytes. The bytes argument must be a bytes-like object (e.g. bytes or bytearray). The byteorder argument determines the byte order used to represent the integer. If byteorder is ‘big‘, the most significant byte is at the beginning of the byte array. If byteorder is ‘little‘, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder‘ as the byte order value. The signed keyword-only argument indicates whether two‘s complement is used to represent the integer. """ """ 这个方法是在Python3.2的时候加入的,python官方给出了下面几个例子: >>> int.from_bytes(b‘\x00\x10‘, byteorder=‘big‘) >>> int.from_bytes(b‘\x00\x10‘, byteorder=‘little‘) >>> int.from_bytes(b‘\xfc\x00‘, byteorder=‘big‘, signed=True) -1024 >>> int.from_bytes(b‘\xfc\x00‘, byteorder=‘big‘, signed=False) >>> int.from_bytes([255, 0, 0], byteorder=‘big‘) """ pass def to_bytes(self, length, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ """ int.to_bytes(length, byteorder, *, signed=False) -> bytes Return an array of bytes representing an integer. The integer is represented using length bytes. An OverflowError is raised if the integer is not representable with the given number of bytes. The byteorder argument determines the byte order used to represent the integer. If byteorder is ‘big‘, the most significant byte is at the beginning of the byte array. If byteorder is ‘little‘, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder‘ as the byte order value. The signed keyword-only argument determines whether two‘s complement is used to represent the integer. If signed is False and a negative integer is given, an OverflowError is raised. """ """ python官方给出了下面几个例子: >>> (1024).to_bytes(2, byteorder=‘big‘) b‘\x04\x00‘ >>> (1024).to_bytes(10, byteorder=‘big‘) b‘\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00‘ >>> (-1024).to_bytes(10, byteorder=‘big‘, signed=True) b‘\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00‘ >>> x = 1000 >>> x.to_bytes((x.bit_length() // 8) + 1, byteorder=‘little‘) b‘\xe8\x03‘ """ pass def __abs__(self, *args, **kwargs): # real signature unknown """ abs(self)""" """ 返回一个绝对值 >>> (95).__abs__() -95 >>> (-95).__abs__() """ pass def __add__(self, *args, **kwargs): # real signature unknown """ Return self+value.""" """ 加法,也可区分数字和字符串 >>> (95).__add__(1) >>> (95).__add__("1") NotImplemented >>> """ pass def __and__(self, *args, **kwargs): # real signature unknown """ Return self&value.""" pass def __bool__(self, *args, **kwargs): # real signature unknown """ self != 0 """ """ 判断一个整数对象是否为0,如果为0,则返回False,如果不为0,则返回True >>> (95).__bool__() True >>> (0).__bool__() False """ pass def __ceil__(self, *args, **kwargs): # real signature unknown """ Ceiling of an Integral returns itself. """ pass def __divmod__(self, *args, **kwargs): # real signature unknown """ Return divmod(self, value). """ """ 返回一个元组,第一个元素为商,第二个元素为余数 >>> (9).__divmod__(5) (1, 4) """ pass def __eq__(self, *args, **kwargs): # real signature unknown """ Return self==value. """ """ 判断两个值是否相等 >>> (95).__eq__(95) True >>> (95).__eq__(9) False """ pass def __float__(self, *args, **kwargs): # real signature unknown """ float(self) """ """ 将一个整数转换成浮点型 >>> (95).__float__() 95.0 """ pass def __floordiv__(self, *args, **kwargs): # real signature unknown """ Return self//value. """ """ 整除,保留结果的整数部分 >>> (95).__floordiv__(9) """ pass def __floor__(self, *args, **kwargs): # real signature unknown """ Flooring an Integral returns itself. """ """ 返回本身 >>> (95).__floor__() """ pass def __format__(self, *args, **kwargs): # real signature unknown """ 转换对象的类型 >>> (95).__format__(‘f‘) ‘95.000000‘ >>> (95).__format__(‘b‘) ‘1011111‘ """ pass def __getattribute__(self, *args, **kwargs): # real signature unknown """ Return getattr(self, name). """ """ 判断这个类中是否包含这个属性,如果包含则打印出值,如果不包含,就报错了 >>> (95).__getattribute__(‘__abs__‘) <method-wrapper ‘__abs__‘ of int object at 0x9f93c0> >>> (95).__getattribute__(‘__aaa__‘) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: ‘int‘ object has no attribute ‘__aaa__‘ """ pass def __getnewargs__(self, *args, **kwargs): # real signature unknown pass def __ge__(self, *args, **kwargs): # real signature unknown """ Return self>=value. """ """ 判断是否大于等于 >>> (95).__ge__(9) True >>> (95).__ge__(99) False """ pass def __gt__(self, *args, **kwargs): # real signature unknown """ Return self>value. """ """ 判断是否大于 >>> (95).__gt__(9) True >>> (95).__gt__(99) False """ pass def __hash__(self, *args, **kwargs): # real signature unknown """ Return hash(self). """ """ 计算哈希值,整数返回本身 >>> (95).__hash__() >>> (95.95).__hash__() """ pass def __index__(self, *args, **kwargs): # real signature unknown """ Return self converted to an integer, if self is suitable for use as an index into a list. """ pass def __init__(self, x, base=10): # known special case of int.__init__ """ 这个是一个类的初始化方法,当int类被实例化的时候,这个方法默认就会被执行 """ """ int(x=0) -> integer int(x, base=10) -> integer Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating point numbers, this truncates towards zero. If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by ‘+‘ or ‘-‘ and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal. >>> int(‘0b100‘, base=0) # (copied from class doc) """ pass def __int__(self, *args, **kwargs): # real signature unknown """ int(self) """ """ 转换为整型 >>> (9.5).__int__() """ pass def __invert__(self, *args, **kwargs): # real signature unknown """ ~self """ pass def __le__(self, *args, **kwargs): # real signature unknown """ Return self<=value. """ """ 判断是否小于等于 >>> (95).__le__(99) True >>> (95).__le__(9) False """ pass def __lshift__(self, *args, **kwargs): # real signature unknown """ Return self<<value. """ """ 用于二进制位移,这个是向左移动 >>> bin(95) ‘0b1011111‘ >>> a = (95).__lshift__(2) >>> bin(a) ‘0b101111100‘ >>> """ pass def __lt__(self, *args, **kwargs): # real signature unknown """ Return self<value. """ """ 判断是否小于 >>> (95).__lt__(9) False >>> (95).__lt__(99) True """ pass def __mod__(self, *args, **kwargs): # real signature unknown """ Return self%value. """ """ 取模 % >>> (95).__mod__(9) """ pass def __mul__(self, *args, **kwargs): # real signature unknown """ Return self*value. """ """ 乘法 * >>> (95).__mul__(10) """ pass def __neg__(self, *args, **kwargs): # real signature unknown """ -self """ """ 将正数变为负数,将负数变为正数 >>> (95).__neg__() -95 >>> (-95).__neg__() """ pass @staticmethod # known case of __new__ def __new__(*args, **kwargs): # real signature unknown """ Create and return a new object. See help(type) for accurate signature. """ pass def __ne__(self, *args, **kwargs): # real signature unknown """ Return self!=value. """ """ 不等于 >>> (95).__ne__(9) True >>> (95).__ne__(95) False """ pass def __or__(self, *args, **kwargs): # real signature unknown """ Return self|value. """ """ 二进制或的关系,只要有一个为真,就为真 >>> a = 4 >>> b = 0 >>> a.__or__(b) # a --> 00000100 b --> 00000000 >>> b = 1 # b --> 00000001 >>> a.__or__(b) """ pass def __pos__(self, *args, **kwargs): # real signature unknown """ +self """ pass def __pow__(self, *args, **kwargs): # real signature unknown """ Return pow(self, value, mod). """ """ 幂 >>> (2).__pow__(10) """ pass def __radd__(self, *args, **kwargs): # real signatre unknown """ Return value+self. """ """ 加法,将value放在前面 >>> a.__radd__(b) # 相当于 b+a """ pass def __rand__(self, *args, **kwargs): # real signature unknown """ Return value&self. """ """ 二进制与的关系,两个都为真,才为真,有一个为假,就为假 """ pass def __rdivmod__(self, *args, **kwargs): # real signature unknown """ Return divmod(value, self). """ pass def __repr__(self, *args, **kwargs): # real signature unknown """ Return repr(self). """ pass def __rfloordiv__(self, *args, **kwargs): # real signature unknown """ Return value//self. """ pass def __rlshift__(self, *args, **kwargs): # real signature unknown """ Return value<<self. """ pass def __rmod__(self, *args, **kwargs): # real signature unknown """ Return value%self. """ pass def __rmul__(self, *args, **kwargs): # real signature unknown """ Return value*self. """ pass def __ror__(self, *args, **kwargs): # real signature unknown """ Return value|self. """ pass def __round__(self, *args, **kwargs): # real signature unknown """ Rounding an Integral returns itself. Rounding with an ndigits argument also returns an integer. """ pass def __rpow__(self, *args, **kwargs): # real signature unknown """ Return pow(value, self, mod). """ pass def __rrshift__(self, *args, **kwargs): # real signature unknown """ Return value>>self. """ pass def __rshift__(self, *args, **kwargs): # real signature unknown """ Return self>>value. """ pass def __rsub__(self, *args, **kwargs): # real signature unknown """ Return value-self. """ pass def __rtruediv__(self, *args, **kwargs): # real signature unknown """ Return value/self. """ pass def __rxor__(self, *args, **kwargs): # real signature unknown """ Return value^self. """ pass def __sizeof__(self, *args, **kwargs): # real signature unknown """ Returns size in memory, in bytes """ """ 在内存中占多少个字节 >>> a = 95 >>> a.__sizeof__() """ pass def __str__(self, *args, **kwargs): # real signature unknown """ Return str(self). """ """ 将一个正数转为字符串 >>> a = 95 >>> a = a.__str__() >>> print(type(a)) <class ‘str‘> """ pass def __sub__(self, *args, **kwargs): # real signature unknown """ Return self-value. """ """ 减法运算 >>> (95).__sub__(5) """ pass def __truediv__(self, *args, **kwargs): # real signature unknown """ Return self/value. """ """ 除法运算 >>> (95).__truediv__(5) 19.0 """ pass def __trunc__(self, *args, **kwargs): # real signature unknown """ Truncating an Integral returns itself. """ """ 返回一个对象的整数部分 >>> (95.95).__trunc__() """ pass def __xor__(self, *args, **kwargs): # real signature unknown """ Return self^value. """ """ 将对象与值进行二进制的或运算,一个为真,就为真 >>> a = 4 >>> b = 1 >>> a.__xor__(b) >>> c = 0 >>> a.__xor__(c) """ pass denominator = property(lambda self: object(), lambda self, v: None, lambda self: None) # default """ 分母 = 1 """ """the denominator of a rational number in lowest terms""" imag = property(lambda self: object(), lambda self, v: None, lambda self: None) # default """ 虚数 """ """the imaginary part of a complex number""" numerator = property(lambda self: object(), lambda self, v: None, lambda self: None) # default """ 分子 = 数字大小 """ """the numerator of a rational number in lowest terms""" real = property(lambda self: object(), lambda self, v: None, lambda self: None) # default """ 实属 """ """the real part of a complex number""" int
2、布尔值
真或假
1 或 0
3、字符串
"Hello World!"
s = "nick" #索引 print(s[0]) print(s[1]) print(s[2]) print(s[3]) #长度 ret = len(s) print(ret) #切片 print(s[1:3]) print(s.rsplit("ic")) #替换 name = "Nick is good, Today is nice day." a = name.replace("good","man") print(a) #连接两个字符串 li = ["nick","serven"] a = "".join(li) b = "_".join(li) print(a) print(b) #指定的分隔符将字符串进行分割 a = s.rpartition("i") print(a) #分割,前,中,后三部分 name = "Nick is good, Today is nice day." a = name.partition("good") print(a) #for循环 for i in s: print(i) for i in range(5): print(i) # 反转 s = ‘ssssssssss111‘ print(s[::-1]) # 111ssssssssss
4、列表
list = [‘Google‘, ‘baidu‘, ‘taobao‘]
#在列表末尾添加新的对象 list = [‘Google‘, ‘baidu‘, ‘T‘] list.append(‘taobao‘) print(list) #将指定对象插入列表 list = [‘Google‘, ‘baidu‘, ‘T‘,‘baidu‘] list.insert(1,"Nick") print(list) #在列表末尾追加另一个序列中的多个值 list = [‘Google‘, ‘baidu‘, ‘T‘,‘baidu‘] list2 = [‘nick‘,‘baidu‘] list.extend(list2) print(list) #统计某个元素在列表中出现的次数 list = [‘Google‘, ‘baidu‘, ‘T‘,‘baidu‘] a = list.count(‘baidu‘) print(a) #从列表中找出某个值第一个匹配项的索引位置 list = [‘Google‘, ‘baidu‘, ‘T‘,‘baidu‘] a = list.index(‘baidu‘) print(a) #移除列表中的一个元素(默认最后一个元素) list = [‘Google‘, ‘baidu‘, ‘T‘,‘baidu‘] list.pop() print(list) #移除列表中某个值的第一个匹配项 list = [‘Google‘, ‘baidu‘, ‘T‘,‘baidu‘] list.remove(‘baidu‘) print(list) #清空列表 list = [‘Google‘, ‘baidu‘, ‘T‘] list.clear() print(list) #删除指定索引位置 list = [‘Google‘, ‘baidu‘, ‘T‘,‘baidu‘] del list[2] print(list) list = [‘Google‘, ‘baidu‘, ‘T‘,‘baidu‘] del list[1:3] -->顾头不顾尾 print(list) #复制列表 list = [‘Google‘, ‘baidu‘, ‘T‘] list2 = list.copy() print(list2) #对原列表进行排序 list = [‘Google‘, ‘baidu‘, ‘T‘,‘baidu‘] list.sort() print(list) #反向列表中元素 list = [‘Google‘, ‘baidu‘, ‘T‘,‘baidu‘] list.reverse() print(list)
5、元组(不可修改)
name = (‘nick‘,‘jenney‘)
#索引 name = (‘nick‘,‘jenney‘) a = name[0] print(a) #获取指定元素的索引位置 name = (‘nick‘,‘jenney‘) a = name.index(‘nick‘) print(a) #切片 name = (‘nick‘,‘jenney‘) a = name[0:1] print(a) #计算元素出现的个数 name = (‘nick‘,‘jenney‘) a = name.count(‘nick‘) print(a) #长度 name = (‘nick‘,‘jenney‘) a = len(name) print(a) #for循环 name = (‘nick‘,‘jenney‘) for i in name: print(i)
6、字典(无序)
user_info = {
"name":"nick",
"age":18,
"job":"pythoner"
}
user_info = { "name":"nick", "age":18, "job":"pythoner" } #根据key获取值 a = user_info.get("age") print(a) a = user_info.get("Age",19") print(a) #所有的key 列表 a = user_info.keys() print(a) #所有的值,values a = user_info.values() print(a) #所有项的列表形式 a = user_info.items() print(a) #获取并在字典中移除 user_info.pop(‘age‘) print(user_info) #随机并在字典中移除 user_info.popitem() user_info.popitem() print(user_info) #清除内容 a = user_info.clear() print(a) #浅拷贝 a = user_info.copy() print(a) #如果key不存在,则创建,如果存在,则返回已存在的值且不修改 a = user_info.setdefault("age") print(a) user_info.setdefault("cool") print(user_info) #从序列键和值设置为value来创建一个新的字典 a = dict.fromkeys(user_info) print(("new dict: %s") % str(a)) #更新(两个字典) user_info = { "name":"nick", "age":18, "job":"pythoner" } user_info2 = { "wage":800000000, "drem":"The knife girl" } user_info.update(user_info2) print(user_info)
7、set集合(无序、不重复)
s = set() a = {‘nick‘,‘jenny‘,‘suo‘}
#添加元素 a = {‘nick‘,‘jenny‘,‘suo‘} a.add(‘The knife girl‘) print(a) #更新 a = {‘nick‘,‘jenny‘,‘suo‘} b = {‘nick‘,‘jenny‘,‘The knife girl‘} a.update(b) print(a) #a中存在。b中不存在,赋给新值 a = {‘nick‘,‘jenny‘,‘suo‘} b = {‘nick‘,‘jenny‘,‘The knife girl‘} set = a.difference(b) print(set) #a中存在。b中不存在,并更新a a = {‘nick‘,‘jenny‘,‘suo‘} b = {‘nick‘,‘jenny‘,‘The knife girl‘} a.difference_update(b) print(a) #交集,赋给新值 a = {‘nick‘,‘jenny‘,‘suo‘} b = {‘nick‘,‘jenny‘,‘The knife girl‘} set = a.intersection(b) print(set) #交集,更新a a = {‘nick‘,‘jenny‘,‘suo‘} b = {‘nick‘,‘jenny‘,‘The knife girl‘} a.intersection_update(b) print(a) #对称交集 a = {‘nick‘,‘jenny‘,‘suo‘} b = {‘nick‘,‘jenny‘,‘The knife girl‘} set = a.symmetric_difference(b) print(set) #对称交集,更新a a = {‘nick‘,‘jenny‘,‘suo‘} b = {‘nick‘,‘jenny‘,‘The knife girl‘} a.symmetric_difference_update(b) print(a) #并集,赋给新值 a = {‘nick‘,‘jenny‘,‘suo‘} b = {‘nick‘,‘jenny‘,‘The knife girl‘} set = a.union(b) print(set) #如果没有交集,返回True,否则返回False a = {‘nick‘,‘jenny‘,‘suo‘} b = {‘nick‘,‘jenny‘,‘The knife girl‘} set = a.isdisjoint(b) print(set) #是否是子序列 a = {‘nick‘,‘jenny‘,‘suo‘} b = {‘nick‘,‘jenny‘} set = a.issubset(b) print(set) #是否是父序列 a = {‘nick‘,‘jenny‘,‘suo‘} b = {‘nick‘,‘jenny‘} set = a.issuperset(b) print(set) #移除指定元素,不存在不报错 a = {‘nick‘,‘jenny‘,‘suo‘} a.discard(‘suo‘) print(a) #移除指定元素,不存在则报错 a = {‘nick‘,‘jenny‘,‘suo‘} a.remove(‘suo‘) print(a) a.remove(‘suo‘) print(a) #移除随机元素,并赋给新值 a = {‘nick‘,‘jenny‘,‘suo‘} set = a.pop() print(set) #清空 a = {‘nick‘,‘jenny‘,‘suo‘} a.clear() print(a)
其他
1、for循环
用户按照顺序循环可迭代对象中的内容
name = (‘nick‘,‘jenney‘) for i in name: print(i)
2、enumrate
为可迭代的对象添加序号
user_info = { "name":"nick", "age":18, "job":"pythoner" } for k,v in enumerate(user_info,1): print(k,v,user_info.get(v))
3、range和xrange
指定范围,生成指定的数字
range 前面小节已经说明了,range([start,] stop[, step]),根据start与stop指定的范围以及step设定的步长,生成一个序列。
xrange 用法与 range 完全相同,所不同的是生成的不是一个list对象,而是一个生成器。
#python 2.7 版本 print range(1, 10) # 结果:[1, 2, 3, 4, 5, 6, 7, 8, 9] print range(1, 10, 2) # 结果:[1, 3, 5, 7, 9] print range(30, 0, -2) # 结果:[30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2] #python 3.5 版本 a = range(10) print(a) #结果:range(0, 10)
>>> a = xrange(10) >>> print a xrange(10) >>> a = range(10) >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in range(0, 100): print i for i in xrange(0, 100): print i
4、编码与进制转换
utf-8与gbk编码转换
把自己名字用进制表示出来
name = "翁孟铠" a = bytes(name,encoding=‘utf-8‘) print(a) for i in a: print(i,bin(i)) #输出结果(utf-8 三个字节表示一个汉字) b‘\xe7\xb4\xa2\xe5\xae\x81‘ 231 0b11100111 180 0b10110100 162 0b10100010 229 0b11100101 174 0b10101110 129 0b10000001 b = bytes(name,encoding=‘gbk‘) print(b) for i in b: print(i,bin(i)) #输出结果(gbk 两个字节表示一个汉字) b‘\xcb\xf7\xc4\xfe‘ 203 0b11001011 247 0b11110111 196 0b11000100 254 0b11111110
本文出自 “发酸的牛奶” 博客,谢绝转载!
Python基础(二)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。