string formating字符串格式化,function函数,group组,recursion递归,练习

时间:2023-01-18 19:13:41
 # -*- coding: UTF-8 -*-
msg = 'i am {} my hobby is {}'.format('lhf',18)
print(msg) msg1 = 'i am %s my hobby is %s' % ('lhf',[1,2])
print(msg1) name='lhf'
age=18
msg2 = 'i am %s my hobby is %s' % (name,age)
print(msg2) tpl = 'percent %.2f %%' % 99.9762333333333
print(tpl) tpl1 = 'i am %(name)s age %(age)d' % {'name':'alex','age':18}
print(tpl1) msg3 = 'i am %(name)+60s my hobby is alex' % {'name':'lhf'}
print(msg3) msg4 = 'i am \033[43;1m%(name)+60s\033[0m my hobby is alex' %{'name':'lhf'}
print(msg4) #print('root','x','0','0',sep=':') #结果(root:x:0:0) print('root'+':'+'x'+':'+'','') #结果('root:x:0', '0') result:
i am lhf my hobby is 18
i am lhf my hobby is [1, 2]
i am lhf my hobby is 18
percent 99.98 %
i am alex age 18
i am lhf my hobby is alex
i am [43;1m lhf[0m my hobby is alex
('root:x:0', '')
 # -*- coding: UTF-8 -*-
name = '海风'
def huangwei():
name = '黄伟'
print(name)
def liuyang():
name = '刘洋'
print(name)
def nulige():
name = '炉指花'
print(name)
print(name)
nulige()
liuyang()
print(name)
print(name)
huangwei()
print(name) result:
海风
黄伟
刘洋
刘洋
炉指花
黄伟
海风
 # -*- coding: UTF-8 -*-
def calc(n):
print(n)
if int(n/2) == 0:
return n
res = calc(int(n/2))
return res
calc(20) result:
20
10
5
2
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 == '':
name = input('请输入要添加的节点名称')
temp[name] = {}
elif choice == '':
name = input('请输入要查看的节点名称:')
path.append(name)
elif choice.lower() == 'b':
if path:
path.pop()
elif choice.lower() == 'q':
break
else:
print('输入错误,请重新输入。')