python全栈-Day 6

一、小知识点汇总及编码进阶

 1、Python2、python3的区别

  • Python 2 默认编码格式是 ASCII码,Python 3 默认编码格式是 UTF-8
  • Python 2 默认不支持中文,Python 3 默认支持中文
  • print方法的使用方式不同,Python 2 不用加括号,2.7支持加括号和不加括号
  • Python 2中有range()----可看做一个有序的列表,没有xrange()----一个生成器
  • Python 3中的input(),在Python 2中叫raw_input()
  • Python 2 数字、字符串是小数据池,创建-5 -- 256  范围之内的数字或不包含特殊字符的字符串,会共用一个内存地址。python 3.6的范围更大一些
    i1 = 3000000000000000000000000000
    i2 = 3000000000000000000000000000
    print(i1 is i2)  #True
    i1 = 3000000000000000000000000000*3000000000000000000000000000
    i2 = 3000000000000000000000000000*3000000000000000000000000000
    print(i1 is i2)  #False
    
    s1 = 'alex@#$%^'
    s2 = 'alex@#$%^'
    print(s1 is s2)  #True
    s1 = 'alex' * 2
    s2 = 'alex' * 2
    print(s1 is s2)  #True
    s1 = 'alex' + 'alex'
    s2 = 'alex' + 'alex'
    print(s1 is s2)  #True
    s1 = 'alex' + 'alex@#$%^'
    s2 = 'alex' + 'alex@#$%^'
    print(s1 is s2)  #False

2、相似点比较

  • =  赋值,==  比较值是否相等,is  比较内存地址
  • 赋值运算,等号前后的变量是指向同一个内存地址,用is比较结果是True
li1 = [1,2,3]
li2 = li1
li3 = [1,2,3]
print(li1 is li2)   #True
print(li1 is li3)   #False
print(id(li1),id(li2))  #查看两个变量的内存地址

3、编码进阶

  • ASCII
  1. 1个字符用1个字节-8位表示  如:A 00000010
  • UNICODE
  1. 1个字符用4个字节-32位表示  如:A 00000000 00000000 00000000 00000100
  2. 1个汉字用4个字节-32位表示  如:中 00000000 00000000 00000000 00000110
  • UTF-8
  1. 1个字符用1个字节-8位表示  如:A 000100000
  2. 1个汉字用3个字节-24位表示  如:中 00000000 00000000 00000110
  • GBK
  1. 1个字符用1个字节-8位表示  如:A 00000110
  2. 1个汉字用2个字节-16位表示  如:中 00000000 00000110

1、各个编码方式之间的二进制,是不能互相识别的,会产生乱码

2、文件的储存、传输,都不能是UNICODE(只能是UTF-8,UTF-16,GBK,GB2312,ASCII==),否则会占用过大的空间,使用过大的流量

3、Python 3的str在内存中是用UNICODE编码的,需要转化后再存储传输

4、新增数据类型:bytes类型,可以使用全部的字符串方法,是以非UNICODE的编码的

对于英文

  • str的表现形式: s = 'alex',编码方式:unicode
  • bytes的表现形式: s = b'alex',编码方式:UTF-8,UTF-16,GBK,GBK2312,ASCII==

对于中文

  • str的表现形式: s = '中国',编码方式:unicode
  • bytes的表现形式: s = b'\xe4\xb8\xad\xe5\x9b\xbd',编码方式:UTF-8,UTF-16,GBK,GBK2312,ASCII==
    #encode编码,将str-->bytes
    s1 = 'alex'
    s11 = s1.encode('utf-8')
    print(s11)  #b'alex'
    
    s1 = '中国'
    s11 = s1.encode('utf-8')
    print(s11)  #b'\xe4\xb8\xad\xe5\x9b\xbd',总共3*2个字节

二、购物车作业讲解

'''
购物车需求
买家、卖家、商品、买家金钱、卖家金钱、商品余量
1、卖家进水果
2、欢迎光临
3、获取卖家、买家钱数量,是否合法
4、展示商品
5、买家选购水果----支持可一直增加(买什么,几个),每选购一次进行一次结算
6、每一步告知买家   选购的商品是否足够,是否合法,买了啥,得花多少钱,钱够不够,是否确认购买,是否继续购买
7、更新商品余量和买家、卖家金钱
'''

goods_for_sell = [
    {'name':'苹果','price':10,'allowance':5},
    {'name':'香蕉','price':20,'allowance':5},
    {'name':'西瓜','price':30,'allowance':2}
]
money_for_seller = 0
#把货物放在货架上
print('欢迎光临本店,本店售卖商品如下:')
for i in range(len(goods_for_sell)):
    print('{}\t{}\t售价{}元'.format(i+1,goods_for_sell[i]['name'],goods_for_sell[i]['price']))
money_for_buyer = input('请告诉我您有多少预算,方便向您推荐:')
money_for_buyer = int(money_for_buyer)

while 1:
    goods_for_buy = input('请输入您想要够买的商品编号')
    num_for_buy = input('请输入您想要够买的商品数量')
    if goods_for_buy.isdigit() == True and int(goods_for_buy)>0 and int(goods_for_buy)<=len(goods_for_sell) and num_for_buy.isdigit() == True:
        spend_money = int(num_for_buy) * (goods_for_sell[int(goods_for_buy)-1]['price'])
        if spend_money <= money_for_buyer:
            money_for_buyer -= spend_money
            money_for_seller += spend_money
            print('您想要够买的物品是:商品{}\t{}\t总价{}元\t您的余额是{}元'.format(goods_for_buy, goods_for_sell[int(goods_for_buy) - 1]['name'],spend_money,money_for_buyer))
            if money_for_buyer == 0:
                print('您的余额为0,自动结束够买')
                break
        else:
            print('您的余额不足,请重新输入够买')

    elif goods_for_buy.upper() == 'Q' or num_for_buy.upper() == 'Q':
        break

    else:
        print('您输入的商品编号/数量 不合法哟,请重新输入')

猜你喜欢

转载自www.cnblogs.com/txbbkk/p/9303042.html
今日推荐