python基础篇--day05+day06

23 篇文章 0 订阅
21 篇文章 1 订阅

元组 - tuple

特点: 类似列表,但是不支持删除,添加,修改,一旦定义则就不可更改

声明

list1= []   ,  tuple1=()
list2 = [1, 3, 6, 7, 8]
tuple2 = (1, 3, 5, 7, 8)

ist3 = list()   ---> 同 list3=[]
tuple3 = tuple()  ---> tuple3=()

补充: 声明含有一个元素的元组: (1,) 注意必须要添加逗号

元组也是支持下标和切片

tuple2 = (1, 3, 5, 7, 8)
print(tuple2[::-1])  # (8,7,5,3,1)
print(tuple2[::-2])  # ()

元组的内置函数

不支持:
    添加:
    删除:
    修改:
支持:
   查找: index()
   个数:count()

能不能排序?
    可以使用sorted()系统函数排序,但是返回的结果是一个列表

系统函数:
    max()
    min()
    sum()
    sorted()

类型的转换:
str <---> int
list <---> tuple
   列表  ---> 元组 :  tuple(list) 得到的是元组类型
   元组  ---> 列表 :  list(tuple)  得到的是列表

元组的强制修改

tuple2 = (1, 3, 5, 7)
print("tuple2=", tuple2)
list4 = list(tuple2)
print("tuple2-->list4-->", list4)
list4.append(9)
list4[0] = 0
print('list4-->', list4)
tuple2 = tuple(list4)
print("list4-->tuple2-->", tuple2)

结果:

tuple2= (1, 3, 5, 7)
tuple2–>list4–> [1, 3, 5, 7]
list4–> [0, 3, 5, 7, 9]
list4–>tuple2–> (0, 3, 5, 7, 9)

不建议这种操作,与其后期要修改,还不如直接在早期就定义一个列表

字典 - dict

字典的特点

  • 采用键值对的方式
  • 键必须为唯一值,不能重复,添加重复值并不会报错,并且以最后添加的键所对应得值为主,显示的时候不显示重复值
  • 值可以为任意的数据类型,包括字典
  • 键一般是字符串,不可以是列表、字典
  • 字典是无序的,底层采用了哈希表,同时字典也是没有下标的,不支持切片等操作

声明

  • 空字典的声明
    • dict1 = dict()
    • dict2 = {}
  • 有内容的字典
    - dict1={‘zhangsan’:{‘活着’,‘红楼梦’},‘lisi’:{‘活着’,‘红楼梦’},‘zhangsan’:{}}

字典的获取

获取的方式都是通过键来访问值得

  • 方式1
    • dict1[key] —>得到值----》若key不存在,则会抛出KeyError
  • 方式2
    • dict1.get(key [,deafult])—>得到值----》若key不存在则会返回deafult,deafult的默认值是None

字典的增删改查,字典不同于元组那样,字典是可以任意修改的

  • 增加 和 修改
    • dict[key] = values , 在字典中若key存在,则会把最新的values赋值给key所对应的的值;若key不存在则会把key:values作为一个新的键值对增加在字典中。
  • 取值
    • dict1[key] —> 返回key所对应的值
    • dict1.get(key)—> 返回key所对应的值
    • dict1.values()—> 得到所有的值value
    • dict1.keys()—> 得到所有key
    • dic1.items()—> 得到所有的键值对,将键值对放到元组,多个元组保存在一个列表中
      [(),(),()]
  • 删除
    • pop(‘key’) # 根据key删除键值对,并将值返回
    • popitem() 随机删除,从后向前删除键值对的,返回值是(key,value)
    • clear() 清除
    • del dict1[‘key’] —> 根据key删除键值对 类似 pop(key)
    • del dict1 清空内容并回收内存

字典的内置函数

  • 字典支持符号:

    • in: 可以使用,但是只是比较的是值
    • is:可以
  • 转换:

    • 字典—》list 只不过是将字典中key存放到了列表中
      list2 = list(dict2)
      print(list2)
    • 列表 —》字典: 列表必须符合如下格式,如果不是此格式无法转换
      list1 = [(‘a’, 100), (‘b’, 80), (‘c’, 70)]
      dict2 = dict(list1)
      print(dict2)
"""   采用了字典的数据类型
1.图书库查询
2.借书
3.还书
4.借书人查询
5.退出系统

数据类型:
book_list ={'书名';'数量'}
stu_list = {'姓名':['书籍']}
"""
book_list = {'书籍1': 1, '书籍2': 2, '书籍3': 3, '书籍4': 4}  # 书库的图书信息
stu_list = {}  # '姓名': ['书籍']  个人的图书信息

while True:
    choice = input('''    1.图书库查询
    2.借书
    3.还书
    4.借书人查询
    5.退出系统''')
    if choice == '1':
        print("图书馆库存信息".center(50, "-"))
        for index, values in book_list.items():
            print("{}------库存:{}本".format(index, values))
    elif choice == '2':
        name = input("输入你的姓名:")
        book_name = input("输入你所借阅的图书名称:")
        if name in stu_list:  # 若用户已经借过书,直接添加
            if book_name in book_list:  # 判断书库中是否存在此书
                if book_list[book_name] > 0:  # 判断此书籍在书库的数量
                    if book_name not in stu_list.get(name):  # 判断书名是否已经借阅
                        stu_list.get(name).append(book_name)  # {'name':['book']}
                        book_list[book_name] -= 1
                        print("图书借阅成功!")
                    else:  # 若已经借阅则输出提示信息
                        print(stu_list)
                        print("图书已经被您借阅")
                else:
                    print("{}已经被借阅完成---".format(book_name))
            else:
                print("书籍未存放在本书库中,请到其他书库借阅。")
        else:  # 若未借过书,则需要构建数据
            if book_name in book_list:  # 判断书库中是否存在此书
                li = []
                li.append(book_name)
                stu_list[name] = li
                book_list[book_name] -= 1
                print("图书借阅成功!")
            else:
                print("书籍未存放在本书库中,请到其他书库借阅。")
    elif choice == '3':  # 否则需要新建加入到stu_list中
        name = input("输入你的姓名:")
        if name in stu_list:  # 判断是否用户借阅过图书
            book_name = input("输入你所还的图书名称:")
            if book_name in book_list:  # 判断书库中是否有此书
                if book_name in stu_list.get(name):  # 判断用户是否借阅过此书
                    stu_list.get(name).remove(book_name)
                    book_list[book_name] += 1
                    print("{},还书成功.".format(name))
                else:
                    print("你确定你借过这本书???请正确输入,否则报警了!!!")
            else:
                print("书库都没有的书,你怎么能借阅到呢???")
        else:
            print("你还未借阅过图书!")
    elif choice == '4':
        name = input("输入你的姓名:")
        if name in stu_list:  # 判断是否用户借阅过图书
            print(name + ",你借阅的图书如下:")
            for index, values in enumerate(stu_list.get(name)):
                print("{}:{}".format(index + 1, values))
        else:
            print("你还未借阅过图书!")
    elif choice == '5':
        choice_exit = input("是否退出!(Y\\N)")
        if choice.lower() == 'y':
            print("正在退出系统")
            exit()
    else:
        print("输入错误,请重新输入!")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只敲代码的大脸猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值