首页 > 代码库 > python学习笔记(列表、元组、购物车实例)

python学习笔记(列表、元组、购物车实例)

一、列表

      列表和字典是最常用的两种数据类型

  1. 需求:怎么存放班级80多人的姓名,如何实现?

names = ["Zhangyang","Guyun","Xiangpeng","Xuliangchen"]
print(names[0],names[2]) #取出 Zhangyang Xiangpeng
print(names[2:4]) #切片,取出"Xiangpeng","Xuliangchen"并输出
print(names[0:4]) #切片,取出"Zhangyang","Guyun","Xiangpeng","Xuliangchen"]
print(names[-1]) #切片,负数指从后往前
print(names[-2:]) #取最后2个值

  注意:前后的0可以省略

  2. 举例说明列表的各种操作

names = "ZhangYang Guyun Xiangpeng XuLiangChen" #以此列表为例

增加:

names.append("Leihaidong") #在列表最后增加

 

插入:

names.insert(1,"Chenronghua") #位置1,插入数据
names.insert(3,"Xinzhiyu") #位置3,插入数据

 

修改:

names[2] = "Xiedi" #下标位置2,替换成"Xiedi"

 

删除:(三种方式)

names.remove("Chenronghua") #删除列表中指定数据
del names[1] #删除下标位置1的数据
names.pop() #不加下标,删除最后一个数据;加下标,和del效果相同

 

拷贝:

(1)

names = ["Zhangyang","Guyun","Xiangpeng","Xuliangchen"]
names2 = names.copy()
names[2] = "向鹏"
print(names)
print(names2)

(2)

     a.

names = ["Zhangyang","Guyun","Xiangpeng",["alex","jack"],"Xuliangchen"]
names2 = names.copy() #浅copy,只拷贝第一层
names[2] = "向鹏"
names[3][0] = "ALEX"
print(names)
print(names2)

     

查看输出结果,names2中,Xiangpeng没有改,子列表中的数据都被修改。因为子列表中的数据只是一个内存地址,copy的时候只copy内存地址

 

      b.浅copy,浅copy的用途:创建联合账号

import copy
names = ["Zhangyang","Guyun","Xiangpeng",["alex","jack"],"Xuliangchen"]
names2 = copy.copy(names)
print(names)
print(names2)
names[2] = "向鹏"
names[3][0] = "ALEX"
print(names)
print(names2)

 

(3)深copy

import copy
names = ["Zhangyang","Guyun","Xiangpeng",["alex","jack"],"Xuliangchen"]
names2 = copy.deepcopy(names)
print(names)
print(names2)
names[2] = "向鹏"
names[3][0] = "ALEX"
print(names)
print(names2)

此时,names和names2将会成为独立的两份数据

 

列表循环:

for i in names:
    print(i)

列表切片:

print(names[0:-1:2])
print(name[::2])

上面两条命令效果相同,间隔输出列表中数据。列表操作中的0和-1可以省略。

 

二、元组

元组其实跟列表差不多,也是存一组数,只是它一旦创建,便不能再修改,所以又叫只读列表。

语法:

names = (alex,jack)

元组只有2个方法,一个是count,一个是index

 

三、程序练习

程序:购物车程序

需求:

  1. 启动程序后,让用户输入工资,然后打印商品列表
  2. 允许用户根据商品编号购买商品
  3. 用户选择商品后,检查余额是否够,够就直接扣款,不够就提醒
  4. 可随时退出,退出时,打印已购买商品和余额

代码如下:

shopping_list = [] #已购买的商品列表,预先定义空列表
salary = input("please input your saraly:") # 启动程序后,让用户输入工资
product_list = [
    (iphone,5800),
    (bike,800),
    (coffee,31),
    (Macbook Pro,12000),
    (alex python,81),
    (a,120)
]

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))
                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")

 

python学习笔记(列表、元组、购物车实例)