预备知识: bin(): """ Return the binary representation of an integer. >>> bin(2796202) '0b1010101010101010101010' """ pass ord(): """ Return the Unicode code point for a one-character string. ""&q
三元運算 什麼是三元運算?請看下圖說明 透過上圖說明後,可以得出一個三元運算公式: result = 值1 if 條件 else 值2, 如果鯈件為真: result = 值1 如果鯈件為假: result = 值2 如果不寫三元運算的話,那原本的代碼就會寫的較為冗長,就像下面的代碼 #!/usr/bin/env python3 # -*- coding:utf-8 -*- a, b, c = 1, 3, 5 if a > b: d = a else: d = c print(d) ------
最近把一段py2的代码转换到py3的代码,结果运行到向socket中写数据的代码部分出现了'str' does not support the buffer interface这样一个错误. 一番搜索之后,发现py3里是严格区分了str和bytes的.怎么理解str和bytes呢?你可以认为str是一段文本,比如“abcd#%$^*&”什么的,而bytes呢,是二进制的一堆0,1的比特而已.看下面的图: 可以看到str的类型是class 'str',而str.encode()以后类型是class