python 学习Day2

本文深入浅出地介绍了Python语言的基础知识,包括数据类型、三元运算、进制转换、列表与元组操作,以及如何使用Python标准库和第三方库。通过实例讲解了Python程序的编译与解释过程,并演示了如何利用循环与元组编写购物车程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Day2:

  1. Python 有丰富的标准库(不需要下载,直接导入)与第三方库(下载安装,放在SITE-packages中);学习标准库:SYS模块(打印环境变量)

  2. Python是什么?Python与Java/c#一样,是一门基于虚拟机的语言,先编译后解释(运行),内存中有一个Pycodeobject,该文件是python编译器真正编译成的结果,即python程序运行时,编译结果则保存在位于内存中的pycodeobject中,当Python运行结束时,Python解释器则将Pycodeobject写回Pyc文件中。pyc文件就是Pycodeobject的一种持久化保存方式。

  3. 数据类型:int(整形);long(长整形);float(浮点型);complex(复数);布尔值(0/1);字符串。

  4. 三元运算:result = 值1 if 条件 else 值2

  5. 进制:二进制(0、1)、八进制(0-7)、十进制(0-9)、十六进制(0-F),二进制与字符串转换用encode与decode(编码/解码)

  6. 列表的增删改查,主要用[ ];  元组只可查,用()表示。

  7. 运用循环与元组编写购物车程序。

  8. product_list = [
        ('Iphone',5800),
        ('Mac pro',9800),
        ('Bike',800),
        ('Watch',10600),
        ('Coffee',31),
        ('Python book',120)
    ]
    shopping_list=[]
    salary = input("Input your salary:")       #输入工资
    if salary.isdigit():                          #判断工资是否为数字
        salary = int (salary)                     #整数化
        while True:                             #进入死循环
            for index,item in enumerate(product_list):    #取出元组下标为商品序号
                #print(product_list.index(item),item
                print(index,item)                        #打印商品列表
            user_choice = input("选择需要购买物品货号>>>:")
            if user_choice.isdigit():  #判断是否为数字
                user_choice = int(user_choice)
                if user_choice < len(product_list) and user_choice >= 0:     #输入有效商品序号
                    p_item = product_list[user_choice]          #通过下表取出商品
                    if p_item[1] <= salary:                     # 判断是否买得起
                        shopping_list.append(p_item)            #添加到商品列表中
                        salary -= p_item[1]                     #扣钱
                        print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m" % (p_item, salary))   #\033[31;1m%s\033[0m  表示标注为红色;31改为32,为绿色
                    else:
                        print("\033[41;1m你的余额不足!还有[%s]\033[0m" %salary)
                else:
                    print("product code [%s] is not exist!"%user_choice)     #商品序号不存在
    
            elif user_choice == 'q':                  #推出
                print("------------shopping list----------")
                for p in shopping_list:
                    print(p)
                print("your current balance:",salary)
                exit()
            else:
                print("invalid option")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值