1、homework
'''
输出商品列表,用户输入序号,显示用户选中的商品
商品 li = ['手机','电脑','鼠标垫','游艇']
要求:
1、li: 页面显示 序号+商品名称,如:
1 手机
2 电脑
……
2、用户输入选择的商品序号,然后打印商品名称
3、如果用户输入的商品序号有误,则提示输入有误,并重新输入
4、用户输入Q或者q,退出程序
'''
while 1:
li = ['手机','电脑','鼠标垫','游艇']
for i in li:
print('{} {}'.format(li.index(i)+1,i))
num_of_choice = input('please input your choice id:')
if num_of_choice.isdigit():
num_of_choice = int(num_of_choice)
if num_of_choice>0 and num_of_choice <= len(li):
print('your choice is '+li[num_of_choice-1])
else:print('please input correct digit')
elif num_of_choice.lower() == 'q':
break
else:
print('please input the digit')
2、小知识点总结回顾
pytho2和pytho3区别
# python2
# print 'abc' # print不用加括号,但是也可以加括号
# range() xrange() # xrange是生成器,可以转换成range
# raw_input() # 输入
# python3
# print('abc') # print必须加括号
# range() # 无xrange
# input() # 输入
# = :赋值运算,让2者指向同一个内存地址
# == :比较值
# is : 比较内存地址
# id(内容) :测出内存地址
# 数字,字符串
# 小数据池,为了节省空间,在一定范围内(-5 -- 256),i1和i2 指向的内存是一致的
# i1 = 300
# i2 = 300
# 字符串:1、不能有特殊字符 比如¥等 指向不同的内存地址
# i1 = 'alex$'
# i2 = 'alex$'
# 2、i1和i2 都是 'abc'*20,i1和i2指向的内存是相同的 i1和i2 都是 'abc'*21 ,i1和i2指向的内存是不同的
# 只有字符和数字有小数据池的概念
Python2和Python3 编码
# Python2 和 Python3 编码
Ascii
只有英文
1个英文 占 8位(1个字节)
Unicode
开始16位,后来32位
英文和中文都是32位(4个字节)
utf-8
英文:8位(1个字节)
中文:24位(3个字节)
GBK系列
英文:8位(1个字节)
中文:16位(2个字节)
1、各个编码之间的二进制,是不能互相识别的,会出现乱码
2、文件的储存、传输 不能是Unicode,(只能是utf-8,utf-16,gbk,gb2312,ascii等)
python3 编码
str在内存中是用Unicode编码
bytes类型:utf-8 或者 gb2312 编码
如果str需要存储和传输,需要将str转换成bytes
对于英文
str :表现形式:s = 'alex'
编码方式:01 Unicode
bytes:表现形式:s = b'alex'
编码方式:01,utf-8,gb2312……
对于中文
str :表现形式:s = '中国'
编码方式:01 Unicode
bytes:表现形式:s = b'xe91e91e01e21e31e32'
编码方式:01,utf-8
s1 = 'alex'
# encode:如何将 str->bytes
s11 = s1.encode('utf-8') #b'alex'
s11 = s1.encode('gbk') # b'alex'
s2 = '中国'
# encode:如何将 str->bytes
s11 = s2.encode('utf-8') # b'xe4xb8xadxe5x9bxbd'
s11 = s2.encode('gbk') # b'xd6xd0xb9xfa'