python之路day03

  2019.5.9,总结下今天学的知识:

    集合:(1)集合里没有重复元素。(2)关系测试,即交集,并集,差集等关系运算。

      常用操作:

 1 # Author:K
 2 list_1 = [1,2,3,4,5,6,7]
 3 list_2 = {6,7,8,9,0}
 4 
 5 print(list_1)
 6 print(list_2)
 7 
 8 list_1 = set(list_1)
 9 print(list_1)
10 print(list_2)
11 
12 #集合关系运算:
13     #intersection() & 交集
14 print(list_1.intersection(list_2))
15 print(list_1 & list_2)
16 
17     #union() | 并集
18 print(list_1.union(list_2))
19 print(list_1 | list_2)
20 
21     #difference() - 差集
22 print(list_1.difference(list_2))
23 print(list_1 - list_2)
24 
25     #symmetric_difference() ^ 对称差集  将A、B两个集合做并运算且把相交的元素去掉
26 print(list_1.symmetric_difference(list_2))
27 print(list_1 ^ list_2)
28 
29     # 集合A >= 集合B , B是否是A的子集 或者使用函数issubset()判断子集 函数issuperset()判断父集
30     # 集合A <= 集合B , A是否是B的子集 A.issubset(B) B.issuperset(A)
31 A = {1,2,3}
32 B = {1,2,3,4,5}
33 
34 if A >= B:
35     print("B是A的子集")
36 else:
37     print("B是A的父集")
38 
39 if A.issubset(B):
40     print("A是B的子集")
41 else:
42     print("A不是B的子集")
43 
44 if A.issuperset(B):
45     print("A是B的父集")
46 else:
47     print("A不是B的父集")
48 
49 
50 #增删查
51     #add() update() 增 其中add()添加一项,update()添加多项
52 print(list_1)
53 list_1.add("string")
54 print(list_1)
55 list_1.update([8,9,0])
56 print(list_1)
57 
58     #remove() discard() 删 若删除集合没有的元素remov()则报错,而discard()不会
59 list_1.remove("string")
60 print(list_1)
61 list_1.discard(7)
62 print(list_1)
63 
64     # in 查
65 if "string" in list_1:
66     print("'string'存在")
67 else:
68     print("'string'不存在")
69 
70 if 0 in list_1:
71     print("0 存在")
72 else:
73     print("0 不存在")
74 
75 #求长度 len()
76 print(list_1)
77 print(len(list_1))
集合常用操作

    文件操作:(1)打开文件 (2)操作文件 (3)退出文件。用with语句不需要使用 close() 函数退出文件。

 1 # Author:K
 2 
 3 file_path = "D:\PycharmProjects\python learning\day03\essay"
 4 
 5 # read() 读
 6 f = open(file_path,'r',encoding = 'utf-8')
 7 print(f.read())
 8 f.close() #!!!!!!!!!!重点!每次操作完一个文件夹都要执行关闭函数,不然有可能会得到不是期望得到的结果
 9 
10 # write() 写
11 f = open(file_path,'w',encoding = 'utf-8') #将文件原来的内容全部替换成write()里面的内容
12 f.write("bbb")
13 f.close()
14 # read() 读
15 f = open(file_path,'r',encoding = 'utf-8')
16 print(f.read())
17 f.close()
18 
19 # write() 以追加的方式写
20 f = open(file_path,'a',encoding = 'utf-8')
21 f.write("ccc")
22 f.close()
23 # read() 读
24 f = open(file_path,'r',encoding = 'utf-8')
25 print(f.read())
26 f.close()
27 
28 f = open(file_path,'w',encoding = 'utf-8')
29 # #print(f.readlines()) # f.readlines() 读取文件的每一行,得到一个列表,列表中的每个信息是对应的一行数据.
30 # print(f.readline().strip()) # f.readline() 读取文件的第一行,得到第一行信息
31 # print(f.tell()) # f.tell() 返回当前光标所在位置。系统得到是5的原因可能是这样-->"aaa\n" ,其中'\n'算一个字符
32 # print(f.seek(0)) # f.seek(0) 返回第一行第一个位置,里面的参数可变,但是不好控制。
33 # print(f.encoding) #打印f文件的编码
34 f.write("aaa") # 因为在windows中,写了之后不是马上写到文件中的,会写到缓存中,当达到某个值的时候才会一次性写入。
35 f.flush() # 这个方法就是写一次写完后马上刷新,即把信息马上写到文件中
36 f.truncate(10) # 从头开始截断文件,留10个字符
37 
38 # r+ 以读和追加的方式打开,即可读又可写,但是是以追加的方式写
39 f = open(file_path,'r+',encoding = 'utf-8')
40 print(f.readline())
41 f.write("aaa")
42 f.close()
43 
44 # rb 以二进制方式读 ;wb 以二进制方式写 并且不能有encoding参数
45 f = open(file_path,'rb')
46 print(f.readline())
47 f.close()
48 
49 f = open(file_path,'wb')
50 f.write("bin".encode()) # 以二进制写的时候必须加上调用函数encode()将字符串转成bytes
51 f.close()
52 
53 #对于flush()函数,举个进度条的例子:
54 import sys,time
55 for i in range(20):
56     sys.stdout.write("")
57     sys.stdout.flush()
58     time.sleep(0.3)
59 
60 
61 
62 # 利用with open() as f: 操作文件 以这种方式操作文件可以不用写close()函数。
63     # read() 读
64 with open(file_path,'r',encoding = 'utf-8') as f:
65     print(f.read())
66 
67     # write() 写
68 with open(file_path,'w',encoding = 'utf-8') as f:
69     f.write("bbb")
70     # read() 读
71 with open(file_path,'r',encoding = 'utf-8') as f:
72     print(f.read())
73 
74     #write() 以追加的方式写
75 with open(file_path,'a',encoding = 'utf-8') as f:
76     f.write("ccc")
77     # read() 读
78 with open(file_path,'r',encoding = 'utf-8') as f:
79     print(f.read())
文件常用操作

  

  说一下上回的作业:

  购物车的优化
  用户入口:
   1.商品信息存在文件里
  2.已购商品,余额记录。
   商家入口:
  1.可以添加商品,修改商品价格

    创建两个文件:
  (1)商品信息文件(商家添加)
  (2)已购商品信息文件(系统添加):
判断有无此文件,没有就输入薪水,有就不输入,直接进入购物阶段。
  流程图如下:

    代码如下:

 1 # Author:K
 2 import os
 3 
 4 def showing_commodity_information(commodity_info_path):
 5     print("------commodity information------")
 6     with open(commodity_info_path,'r',encoding='utf-8') as f:
 7         print(f.read())
 8     buying_choice = input("请选择要购买的商品名称:")
 9     return buying_choice
10 
11 def buying_commodity(commodity_info_path,shopping_list_path,buying_choice,balance):
12     price = 0
13     with open(commodity_info_path,'r',encoding='utf-8') as f:
14         for line in f:
15             if buying_choice == line.split()[0]:
16                 price = int(line.split()[1])
17     if price != 0 :
18         if balance >= price:
19             shopping_list = []
20             balance -= price
21             shopping_list.append(balance)
22             shopping_list.insert(-1,buying_choice)
23             with open(shopping_list_path,'a',encoding='utf-8') as f:
24                 f.write(str(shopping_list[0])+' '+str(shopping_list[1])+'\n')
25             print("成功购买 \033[31;1m %s \033[0m 商品" % buying_choice)
26         else:
27             print("余额不足!")
28     else:
29         print("没有此类商品!")
30 
31 def main():
32     commodity_info_path = "D:\PycharmProjects\python learning\day02\homework\commodity_info.txt"
33     shopping_list_path = "D:\PycharmProjects\python learning\day02\homework\shopping_list.txt"
34     choice = int(input("请输入编号(输入1是用户入口,输入2是商家入口):"))
35 
36     if choice == 1:
37         while True:
38             user_choice = input("购买商品按1,退出按q:")
39 
40             if user_choice.isdigit() and int(user_choice) == 1:
41                 if  not os.path.exists("shopping_list.txt"): #若已购商品信息文件不存在则输入薪水
42                     salary = int(input("请输入薪水:"))
43                     buying_choice = showing_commodity_information(commodity_info_path) #打印商品信息并返回购买的商品名称
44                     buying_commodity(commodity_info_path,shopping_list_path,buying_choice,salary) #购买商品
45                 else:  #若已购商品信息文件存在则不需要输入薪水
46                     with open(shopping_list_path,'r',encoding='utf-8') as f:
47                         balance = int(f.read().split()[-1])
48                     print("您的余额为:\033[41;1m %s \033[0m" % balance)
49                     buying_choice = showing_commodity_information(commodity_info_path) #打印商品信息并返回购买的商品名称
50                     buying_commodity(commodity_info_path,shopping_list_path,buying_choice,balance) #购买商品
51             elif user_choice == 'q':
52                 exit("已退出程序!")
53 
54     elif choice == 2:
55         while True:
56             commodity_added = input("请输入要添加的商品信息(按q退出程序):")
57             if commodity_added != 'q':
58                 with open(commodity_info_path,'a',encoding='utf-8') as f:
59                     f.write(commodity_added + '\n')
60                 print("添加成功!")
61             else:
62                 exit("已退出程序!")
63 
64 main()
购物车代码
  第三天的知识今天就学了这么多,还有知识没学完,学完了来补充! 

猜你喜欢

转载自www.cnblogs.com/KisInfinite/p/10841980.html
今日推荐