zoukankan      html  css  js  c++  java
  • python学习6

    1.购物车作业

    li = ['手机','电脑','鼠标垫','游艇']
    for i in li:
        print('{}		{}'.format(li.index(i)+1,i))
    num = input('选择商品序号>>>>>>>>:')
    print(num,type(num))
    while 1:
    
        if not num.isdigit():
            num = input('输入有误,请重新输入数字')
            continue
        else:
            number = int(num)
            if number >= 1 and number <= len(li):
                print(li[number - 1])
            else:
                num = input('请输入1-{number}范围内的数字'.format(number = len(li)))
                continue
    
        num = input('请继续选择,如果想退出的话,请输入q退出>>>>')
        if num.upper() == 'Q':
            break

    继续实现余额提示功能

    买了什么

    1.商品输入,商品列表的形式

    li = [
            {'name':'苹果','price':10}
            {'name':'香蕉','price':20}
            {'name':'西瓜','price':30}
            ]

    2.资产输入

    3.判断钱够不够

    4.随时能退出

    2.回顾复习,编码格式的转换

    1.预备知识,各种编码格式

    ASCII码
             A : 00000010    8位  一个字节
    
    
    
    unicode
            A : 00000000 00000001 00000010 00000100 32位
            中: 00000000 00000001 00000010 00000100 32位
    
    
    utf-8
          A: 0010 0000 8 位 一个字节
          中: 00000001 00000010 00000110 24位 三个字节
    
    
    
    gbk:
          A  :  00000110 8 位 一个字节
          中:  00000010 00000110 16位 两个字节
    
    1.各个编码之间的二进制不能互相识别,会产生乱码
    2.文件的储存、传输都不能使用unicode编码,只能是utf-8、utf-16、gbk2312,或者ASCII等
    
    python3的str在内存中是用unicode编码方式存储的
    
    bytes 类型  是一种特殊的数据类型   不是以unicode编码方式存储的(utf-8,gbk,ascii.etc)
            对于英文:
            str :    表现形式s = 'alex'
                     编码方式:0101001001010101  unicode
            bytes:  表现形式:  s = b'alex'
                    编码方式:   01010101010    utf-8 gbk
            对于中文:
            str :    表现形式s = '中国'
                     编码方式:0101001001010101  unicode
            bytes:  表现形式:  s = b'xe91e91e01e21e31e32'
                    编码方式:   01010101010    utf-8 gbk.....

    2.如何将python3内存中unicode编码的str转化成可以存储传输的utf-8,gbk编码的bytes格式

    s = '中国'

    s1 = s.encode('gbk')

    print(s1)----------------->>>>>>b'xd6xd0xb9xfa'

    #编码
    # s = 'alex'
    # s1 = b'alex'
    # print(s,type(s))
    # print(s1,type(s1))
    
    # s1 = '中国'
    # # print(s,type(s1))
    # # s2 = b'中国'
    # # print(s1.type(s2))
    
    #encode   编码,如何将str转换成bytes类型
    s1 = 'alex'
    s11 = s1.encode('gbk')
    print(s11)
    s2 = '中国'
    s22 = s2.encode('gbk')
    print(s22)

    3.小数据池

    用来节省内存空间的,把一些东西放在了一个内存地址下

    #数字,字符串,小数据池节省内存(-5——256是一个空间)Pycharm 会改变内存地址
    #字符串:1.不能含有特殊字符
    #        2.s*20 还是同一个地址,s*21以后都是两个地址
    #        3.除了数字和字符串,例如列表、集合没有小数据池概念
    # i1 = 6
    # i2 = 6
    # print(id(i1),id(i2))
  • 相关阅读:
    优先队列
    Problem W UVA 662 二十三 Fast Food
    UVA 607 二十二 Scheduling Lectures
    UVA 590 二十一 Always on the run
    UVA 442 二十 Matrix Chain Multiplication
    UVA 437 十九 The Tower of Babylon
    UVA 10254 十八 The Priest Mathematician
    UVA 10453 十七 Make Palindrome
    UVA 10163 十六 Storage Keepers
    UVA 1252 十五 Twenty Questions
  • 原文地址:https://www.cnblogs.com/benbenxiaofeifei/p/9333111.html
Copyright © 2011-2022 走看看