字符串格式化,函数,组,递归,练习
1 # -*- coding: UTF-8 -*- 2 msg = 'i am {} my hobby is {}'.format('lhf',18) 3 print(msg) 4 5 msg1 = 'i am %s my hobby is %s' % ('lhf',[1,2]) 6 print(msg1) 7 8 name='lhf' 9 age=18 10 msg2 = 'i am %s my hobby is %s' % (name,age) 11 print(msg2) 12 13 tpl = 'percent %.2f %%' % 99.9762333333333 14 print(tpl) 15 16 tpl1 = 'i am %(name)s age %(age)d' % {'name':'alex','age':18} 17 print(tpl1) 18 19 msg3 = 'i am %(name)+60s my hobby is alex' % {'name':'lhf'} 20 print(msg3) 21 22 msg4 = 'i am \033[43;1m%(name)+60s\033[0m my hobby is alex' %{'name':'lhf'} 23 print(msg4) 24 25 #print('root','x','0','0',sep=':') #结果(root:x:0:0) 26 27 print('root'+':'+'x'+':'+'0','0') #结果('root:x:0', '0') 28 29 30 31 result: 32 i am lhf my hobby is 18 33 i am lhf my hobby is [1, 2] 34 i am lhf my hobby is 18 35 percent 99.98 % 36 i am alex age 18 37 i am lhf my hobby is alex 38 i am [43;1m lhf[0m my hobby is alex 39 ('root:x:0', '0')
1 # -*- coding: UTF-8 -*- 2 name = '海风' 3 def huangwei(): 4 name = '黄伟' 5 print(name) 6 def liuyang(): 7 name = '刘洋' 8 print(name) 9 def nulige(): 10 name = '炉指花' 11 print(name) 12 print(name) 13 nulige() 14 liuyang() 15 print(name) 16 print(name) 17 huangwei() 18 print(name) 19 20 result: 21 海风 22 黄伟 23 刘洋 24 刘洋 25 炉指花 26 黄伟 27 海风
1 # -*- coding: UTF-8 -*- 2 def calc(n): 3 print(n) 4 if int(n/2) == 0: 5 return n 6 res = calc(int(n/2)) 7 return res 8 calc(20) 9 10 result: 11 20 12 10 13 5 14 2 15 1
集合特点:
1.由不同元素组成
2.集合无序
3.集合元素是不可变类型,包括(数字,字符串,元祖
#交叉补集 python_l = ['lcg','szw','zjw','lcg'] linux_l = ['lcg','szw','sb'] p_s = set(python_l) l_s = set(linux_l) print(p_s.symmetric_difference(l_s)) print(p_s^l_s) p_s.difference_update(l_s) #等同于p_s = p_s - l_s print(p_s) s1 = {1,2} s2 = {2,3,5} print(s1.isdisjoint(s2)) #判断两个集合是否包含相同的元素 #子集,父集 s1 = {1,2} s2 = {1,2,3} print(s1.issubset(s2))#s1是s2的子集 print(s2.issubset(s1)) print(s2.issuperset(s1))#s1是s2的父集 s1.update(s2)#更新多个值 # s1.add(1,2,3,4)#更新一个值 # s1.union(s2)#不更新 print(s1) s = frozenset('hello')#两种字典类型的区别 s5 = set('hello') print(s,s5) names = ['alex','alex','wupeiqi'] names = list(set(names)) print(names) result: {'zjw', 'sb'} {'zjw', 'sb'} {'zjw'} False True False True {1, 2, 3} frozenset({'e', 'o', 'l', 'h'}) {'e', 'o', 'l', 'h'} ['alex', 'wupeiqi']
可迭代类型:for循环可以遍历 函数形参与实参知识点: 位置参数(形如test(1,2,3)):实参和形参一一对应,缺一不行,多一也不行 关键字参数(形如test(x=1,z=3,y=2)):实参和形参无需一一对应,缺一不行,多一也不行 位置函数作为形参必须在关键字函数左边。 def test(x,*args) #*args用于函数扩展
模拟多级节点 方法一: dic = { '植物': {'草本植物': ['牵牛花','瓜叶菊','葫芦','翠菊','冬小麦','甜菜'], '水木植物': ['乔木','灌木','丰灌木','如松','杉'], '水生植物': ['荷花','千屈菜','呂菊','黄奕菊','水葱','再力花','梭鱼草']}, '动物': {'两栖动物': ['山龟','山鳖','石蛙','娃娃鱼','蟾蜍','龟','鳄鱼','蜥蜴','蛇'], '禽类': ['知鸡','原鸡','长鸣鸡','昌国鸡','斗鸡','长尾鸡','乌骨鸡'], '哺乳类动物': ['虎','狼','蛇','鹿','貂','猴','树懒','斑马','狗']}} li = [] bool = True while bool: bool1 = True for i,v in enumerate(dic,1): print(i,v) li.append(v) print('要添加新种类请输入i,否则输入对应下一级数字查看菜单') u_c = input('>>>') if u_c.isdigit(): #数字用来选择 li1 = [] while bool: u_c = int(u_c) for j,k in enumerate(dic[li[u_c-1]],1): print(j,k) li1.append(k) u_c1 = input('>>>>') if u_c1 == 'b': li.clear() break while bool: for x,y in enumerate(dic[li[int(u_c-1)]][li1[int(u_c1)-1]],1): print(x,y) u_c2 = str(input('>>>>>')) u_c2 = u_c2.lower() if u_c2 == 'b': # bool = False li1.clear() break elif u_c2 == 'q': li.clear() bool = False break else: continue elif u_c.isalpha(): #字母i用来添加新种类 if u_c == 'i'or u_c == 'I': while bool1: lx = str(input('类型:')) if dic.get(lx,1) == 1: #得到lx中的字符串,如果该字符串在dic第一层中没有,则返回后面值‘1’ dic[lx] = {} zl = str(input('种类:')) if dic[lx].get(zl,1) == 1: #得到lx中的字符串,如果该字符串在dic第一层中没有,则返回后面值‘1’ dic[lx][zl] = [] while bool1: mc = input('名称:') if mc == 'b': li.clear() bool1 = False break elif mc == 'q': bool = False break elif mc in dic[lx][zl]: print('已经存在') else: dic[lx][zl].append(mc)
方法二:(编程思路来自老男孩郑建文老师) db = { '北京':{}, '上海':{ '虹口':{ '外国语':'小明', '西藏北路':'布丁酒店', '上海南站':'高铁'}, '宝山':{ '大场':'蹦迪', '上海大学':'钱伟长'}, '闵行':{ '上海交大':'闵行校区' } } } path = [] while True: temp = db for item in path: temp = temp[item] print('当前可选的所有子节点:',list(temp.keys())) choice = input('1:添加节点; 2:查看节点(Q/B);\n >>>') if choice == '1': name = input('请输入要添加的节点名称') temp[name] = {} elif choice == '2': name = input('请输入要查看的节点名称:') path.append(name) elif choice.lower() == 'b': if path: path.pop() elif choice.lower() == 'q': break else: print('输入错误,请重新输入。')