标签:鼠标 enum com 键值 下划线 and pre 注意 art
1、请用代码实现:利用下划线将列表的每一个元素拼接成字符串,li = [‘alex‘, ‘eric‘, ‘rain‘]
li = [‘alex‘, ‘eric‘, ‘rain‘] v = "_".join(li) print(v) alex_eric_rain
2、查找列表中元素,移除每个元素的空格,并查找以 a 或 A 开头 并且以 c 结尾的所有元素。
li = ["alec", " aric", "Alex", "Tony", "rain"]
li = ["alec", " aric", "Alex", "Tony", "rain"] for i in li: i = i.strip() v = i.endswith(‘c‘) v1 = i.startswith(‘A‘) v2 = i.startswith(‘a‘) if (v==True and v1==True) or (v==True and v2==True): print(i)
tu = ("alec", " aric", "Alex", "Tony", "rain")
tu = ("alec", " aric", "Alex", "Tony", "rain") for i in tu: i = i.strip() v = i.endswith(‘c‘) v1 = i.startswith(‘A‘) v2 = i.startswith(‘a‘) if (v==True and v1==True) or (v==True and v2==True): print(i)
dic = {‘k1‘: "alex", ‘k2‘: ‘ aric‘,"k3": "Alex", "k4": "Tony"}
dic = {‘k1‘: "alex", ‘k2‘: ‘ aric‘, "k3": "Alex", "k4": "Tony"} for i in dic.values(): i = i.strip() v = i.endswith(‘c‘) v1 = i.startswith(‘A‘) v2 = i.startswith(‘a‘) if (v==True and v1==True) or (v==True and v2==True): print(i)
3、写代码,有如下列表,按照要求实现每一个功能
li = [‘alex‘, ‘eric‘, ‘rain‘]
li = [‘alex‘, ‘eric‘, ‘rain‘] v = len(li) print(v)
b. 列表中追加元素 “seven”,并输出添加后的列表
li = [‘alex‘, ‘eric‘, ‘rain‘] li.append("seven") print(li)
c. 请在列表的第 1 个位置插入元素 “Tony”,并输出添加后的列表
li = [‘alex‘, ‘eric‘, ‘rain‘] li.insert(0,"Tony") print(li)
d. 请修改列表第 2 个位置的元素为 “Kelly”,并输出修改后的列表
li = [‘alex‘, ‘eric‘, ‘rain‘] li[1] = "Kelly" print(li)
e. 请删除列表中的元素 “eric”,并输出修改后的列表
li = [‘alex‘, ‘eric‘, ‘rain‘] li.remove("eric") print(li)
f. 请删除列表中的第 2 个元素,并输出删除的元素的值和删除元素后的列表
li = [‘alex‘, ‘eric‘, ‘rain‘] v = li.pop(1) print(v,li)
g. 请删除列表中的第 3 个元素,并输出删除元素后的列表
li = [‘alex‘, ‘eric‘, ‘rain‘] li.pop(2) print(li)
h. 请删除列表中的第 2 至 4 个元素,并输出删除元素后的列表
li = [‘alex‘, ‘eric‘, ‘rain‘, ‘rain‘, ‘rain‘] del li[1:4] print(li)
i. 请将列表所有的元素反转,并输出反转后的列表
li = [‘alex‘, ‘eric‘, ‘rain‘] li.reverse() print(li)
j. 请使用 for、len、range 输出列表的索引
li = [‘alex‘, ‘eric‘, ‘rain‘] for i in range(len(li)): print(i)
k. 请使用 enumrate 输出列表元素和序号(序号从 100 开始)
li = [‘alex‘, ‘eric‘, ‘rain‘] for i, v in enumerate(li,100): print(i, v)
l. 请使用 for 循环输出列表的所有元素
li = [‘alex‘, ‘eric‘, ‘rain‘] for i in li: print(i)
4、写代码,有如下列表,请按照功能要求实现每一个功能
li = ["hello", ‘seven‘, ["mon", ["h", "kelly"], ‘all‘], 123, 446]
a. 请根据索引输出 “Kelly”
li = ["hello", ‘seven‘, ["mon", ["h", "kelly"], ‘all‘], 123, 446] v = li[2][1][1] print(v)
b. 请使用索引找到 ‘all‘ 元素并将其修改为 “ALL”,如:li[0][1][9]...
li = ["hello", ‘seven‘, ["mon", ["h", "kelly"], ‘all‘], 123, 446] li[2][2]="ALL" print(li)
5、写代码,有如下元组,按照要求实现每一个功能
tu = (‘alex‘, ‘eric‘, ‘rain‘)
tu = (‘alex‘, ‘eric‘, ‘rain‘) print(len(tu))
b. 获取元组的第 2 个元素,并输出
tu = (‘alex‘, ‘eric‘, ‘rain‘) print(tu[1])
c. 获取元组的第 1--‐2 个元素,并输出
tu = (‘alex‘, ‘eric‘, ‘rain‘) print(tu[0:2])
d. 请使用 for 输出元组的元素
tu = (‘alex‘, ‘eric‘, ‘rain‘) for i in tu: print(i)
e. 请使用 for、len、range 输出元组的索引
tu = (‘alex‘, ‘eric‘, ‘rain‘) for i in range(len(tu)): print(i)
f. 请使用 enumrate 输出元祖元素和序号(序号从 10 开始)
tu = (‘alex‘, ‘eric‘, ‘rain‘) for i ,v in enumerate(tu,10): print(i,v)
6、有如下变量,请实现要求的功能
tu = ("alex", [11, 22, {"k1": ‘v1‘, "k2": ["age", "name"], "k3": (11,22,33)}, 44])
a. 讲述元祖的特性
元组的一级元素不可被修改增加删除,有序,可迭代,可切片,可索引,可转换为列表。
b. 请问 tu 变量中的第一个元素 “alex” 是否可被修改?
不可被修改
c. 请问 tu 变量中的"k2"对应的值是什么类型?是否可以被修改?如果可以,请在其中添加一个元素 “Seven”
k2对应的值是列表 可以被修改
tu = ("alex", [11, 22, {"k1": ‘v1‘, "k2": ["age", "name"], "k3": (11,22,33)}, 44]) v = tu[1][2]["k2"].append("seven") print(tu) (‘alex‘, [11, 22, {‘k2‘: [‘age‘, ‘name‘, ‘seven‘], ‘k3‘: (11, 22, 33), ‘k1‘: ‘v1‘}, 44])
d. 请问 tu 变量中的"k3"对应的值是什么类型?是否可以被修改?如果可以,请在其中添加一个元素 “Seven”
k3对应的是元组 不可修改
7、字典
dic = {‘k1‘: "v1", "k2": "v2", "k3": [11,22,33]}
dic = {‘k1‘: "v1", "k2": "v2", "k3": [11,22,33]} for i in dic: print(i)
b. 请循环输出所有的 value
dic = {‘k1‘: "v1", "k2": "v2", "k3": [11,22,33]} for i in dic.values(): print(i)
c. 请循环输出所有的 key 和 value
dic = {‘k1‘: "v1", "k2": "v2", "k3": [11,22,33]} for i in dic.items(): print(i)
d. 请在字典中添加一个键值对,"k4": "v4",输出添加后的字典
dic = {‘k1‘: "v1", "k2": "v2", "k3": [11,22,33]} dic.setdefault("k4","v4") print(dic)
e. 请在修改字典中 “k1” 对应的值为 “alex”,输出修改后的字典
dic = {‘k1‘: "v1", "k2": "v2", "k3": [11,22,33]} dic.update(k1="alex") print(dic)
f. 请在 k3 对应的值中追加一个元素 44,输出修改后的字典
dic = {‘k1‘: "v1", "k2": "v2", "k3": [11,22,33]} dic["k3"].append(44) print(dic)
g. 请在 k3 对应的值的第 1 个位置插入个元素 18,输出修改后的字典
dic = {‘k1‘: "v1", "k2": "v2", "k3": [11,22,33]} dic["k3"].insert(0,18) print(dic)
8、转换
s = "alex" v = list(s) print(v)
b. 将字符串 s = "alex" 转换成元祖
s = "alex" v = tuple(s) print(v)
b. 将列表 li = ["alex", "seven"] 转换成元组
li = ["alex", "seven"] v = tuple(li) print(v)
c. 将元祖 tu = (‘Alex‘, "seven") 转换成列表
li = (‘Alex‘, "seven") v = list(li) print(v)
d. 将列表 li = ["alex", "seven"] 转换成字典且字典的 key 按照 10 开始向后递增
li = ["alex", "seven"] dic = {} for i,v in enumerate(li,10): dic[i]=v print(dic)
9、元素分类
有如下值集合 [11,22,33,44,55,66,77,88,99,90],将所有大于 66 的值保存至字典的第一个 key 中,将小于 66 的值保存至第二个 key 的值中。
即: {‘k1‘: 大于 66 的所有值, ‘k2‘: 小于 66 的所有值}
分割
li = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90] li.sort() z = li.index(66) l = len(li) dic = {"k1":li[z+1:l],"k2":li[0:z]} print(dic)
for循环
li = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90] li.sort() lo = [] lp = [] for i in li: if i < 66: lo.append(i) elif i> 66: lp.append(i) dic = {"k1":lo,"k2":lp} print(dic)
输出商品列表,用户输入序号,显示用户选中的商品商品 li = ["手机", "电脑", ‘鼠标垫‘, ‘游艇‘]
a. 允许用户添加商品
b. 用户输入序号显示内容
print("商品列表信息,用户输入序号,显示用户选中的商品") li = ["手机", "电脑", ‘鼠标垫‘, ‘游艇‘] for i,v in enumerate(li,1): print(i,v) x = input("是否添加商品?") if x == "y" or x =="Y" or x == "yes": t = input("请添加商品:") li.append(t) for i,v in enumerate(li,1): print(i,v) s = input("是否查找商品?") if s == "y" or s =="Y" or s == "yes": y = int(input("请输入查找的序号")) print(li[y])
11、用户交互显示类似省市县 N 级联动的选择
允许用户增加内容
b. 允许用户选择查看某一个级别内容
dic = { "植物": {"草本植物": ["牵牛花", "瓜叶菊", "葫芦", "翠菊", "冬小麦", "甜菜"], "木本植物": ["乔木", "灌木", "半灌木", "如松", "杉", "樟"], "水生植物": ["荷花", "千屈菜", "菖蒲", "黄菖蒲", "水葱", "再力花", "梭鱼草"]}, "动物": {"两栖动物": ["山龟", "山鳖", "石蛙", "娃娃鱼", "蟾蜍", "龟", "鳄鱼", "蜥蜴", "蛇"], "禽类": ["雉鸡", "原鸡", "长鸣鸡", "昌国鸡", "斗鸡", "长尾鸡", "乌骨鸡"], "哺乳类动物": ["虎", "狼", "鼠", "鹿", "貂", "猴", "貘", "树懒", "斑马", "狗"]}} count = 0 while count==0: print("物种") l={} count = 0 for i in dic: count +=1 print(count,i) l[count]=i w = int(input("请您输入您想要查找的物种")) x = l[w] p = dic[x] l2 = {} count2 = 0 for i2 in p: count2 += 1 print(count2, i2) l2[count2] = i2 w2 = int(input("请您输入您想要查找的分类")) x2 = l2[w2] p2 = dic[x][x2] l3={} count3 = 0 for i3 in p2: count3 +=1 print(count3,i3) l2[count3]=i3 f = input("是否返首页:(y or Y)") if f == "y" or f == "Y" : count = 0 continue else: f1 = input("是否想添加信息:(y or Y)") if f1 == "y" or f1 == "Y": aa = input("请输入您想添加的级别:\n1 物种 \n2 分类 \n3 品种") if aa == "1": a = input("请输入您想添加物种:") a1 = input("请输入您想添加分类:") a2 = input("请输入您想添加品种:") dic[a]={a1:[a2]} f = input("是否返首页:(y or Y)") if f == "y" or f == "Y": count = 0 continue elif aa == "2": a = input("请输入您想添加物种:") a1 = input("请输入您想添加分类:") a2 = input("请输入您想添加品种:") dic[a][a1] = [a2] f = input("是否返首页:(y or Y)") if f == "y" or f == "Y": count = 0 continue elif aa == "3": a = input("请输入您想添加物种:") a1 = input("请输入您想添加分类:") a2 = input("请输入您想添加品种:") dic[a][a1].append(a2) f = input("是否返首页:(y or Y)") if f == "y" or f == "Y": count = 0 continue
列举布尔值是 False 的所有值
0 "" [] {}()
13、有两个列表
l1 = [11,22,33]
l2 = [22,33,44]
a. 获取内容相同的元素列表
l1 = [11,22,33] l2 = [22,33,44] for i in l1: if i in l2: print(i)
b. 获取 l1 中有, l2 中没有的元素列表
l1 = [11,22,33] l2 = [22,33,44] for i in l1: if i not in l2: print(i)
c. 获取 l2 中有, l1中没有的元素列表
l1 = [11,22,33] l2 = [22,33,44] for i in l2: if i not in l1: print(i)
d. 获取 l1 和 l2 中内容都不同的元素
l1 = [11,22,33] l2 = [22,33,44] for i in l2: if i not in l1: print(i) for v in l1: if v not in l2: print(v)
14、利用 For 循环和 range 输出
a. For 循环从大到小输出 1 - 100
for i in range(1,101): print(i)
b. For 循环从小到到输出 100 - 1
for i in range(100,0,-1): print(i)
c. While 循环从大到小输出 1 - 100
count = 0 while count < 100: count += 1 print(count)
b. While 循环从小到到输出 100 - 1
count = 100 while count > 0: print(count) count -= 1
15、购物车
功能要求:
要求用户输入总资产,例如: 2000
显示商品列表,让用户根据序号选择商品,加入购物车
购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
goods = [ {"name": "电脑", "price": 1999}, {"name": "鼠标", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美女", "price": 998}, ] g = int(input("请您输入您的总资产:")) o = [] count = 0 for i, v in enumerate(goods, 1): k = v[‘price‘] if g >= k: count += 1 print(count, v[‘name‘], v["price"]) o +=[{"name": v[‘name‘],"price":v["price"]}] t = 0 h = [] while True: l = (input("请输入您想要购买的商品:")) v1 = l.isdecimal() if v1 == False: print("没有此序号请重新输入") continue l = int(l) if l > count: print("没有此序号请重新输入") continue c = int(o[l - 1]["price"]) s = o[l - 1]["name"] t = t + c f = (s,c) h.append(f) print("购物车:") for h1 in h: print("\t",h1[0],h1[1]) p = input("是否继续购买") if p == "n" or p == "N" or p == "No" or p == "no": if t > g: print("账户余额不足") break else: print("购买成功") break
16、分页显示内容
a. 通过 for 循环创建 301 条数据,数据类型不限,如:
alex-1 alex1@live.com pwd1
alex-2 alex2@live.com pwd2
alex-3 alex3@live.com pwd3
...
for i in range(301): print("alex{0} alex{0}@live.com pwd{0}\n".format(i+1))
b. 提示用户 请输入要查看的页码,当用户输入指定页码,则显示指定数据
注意:
- 每页显示 10 条数据
- 用户输入页码是非十进制数字,则提示输入内容格式错误
while True: u = [] for i in range(301): a = "alex{0} alex{0}@live.com pwd{0}".format(i+1) x = [a] u = u + x # print(u[0:10]) s = input("请输入您想查看的页码:") v3 = s.isnumeric() if v3 == False : print("输入内容格式错误") elif int(s) < 1 or int(s) > 31: print("输入页码错误") else: s = int(s) a = (s-1)*10 b = s*10 j = (u[a:b]) for g in j: print(g) A =input("是否继续查看(y or Y)") if A =="y" or A =="Y": continue else: break
17、有 1、 2、 3、 4、 5、 6、 7、 8、 8 个数字,能组成多少个互不相同且无重复数字的两位数?
count = 0 li = [] for i in range(1,9): for x in range(1,9): a = ("{0}{1}".format(i,x)) if a not in li: li = li + [a] count =count+1 print(count) print(li)
count = 0 li = [] for i in range(1, 9): for x in range(1, 9): a = ("{0}{1}".format(i, x)) if a not in li: if i != x: li = li + [a] count = count + 1 print(count) print(li)
18、利用 for 循环和 range 输出 9 * 9 乘法表
l = [] for i in range(1,10): l.clear() for x in range(1,i+1): b="{0}*{1}={2} ".format(x,i,i*x) l.append(b) c="".join(l) print(c)
19、有一下列表,
nums = [2, 7, 11, 15, 1, 8, 7]
请找到列表中任意两个元素相加能够等于 9 的元素集合,如: [(0,1),(4,5)]
nums = [2, 7, 11, 15, 1, 8, 7] li = [] lo = [] for i in nums: for x in nums: y = i + x if y == 9: v = [i, x] x =(i,x) if v not in li: v.reverse() if v not in li: lo.append(x) li.append(v) print(lo)
20、用 Python 开发程序自动计算方案:
公鸡 5 文钱一只,母鸡 3 文钱一只,小鸡 3 只一文钱,用 100 文钱买 100 只鸡,其中公鸡,母鸡,小鸡都必须要有,问公鸡,母
鸡,小鸡要买多少只刚好凑足 100 文钱
#100文100只鸡 for g in range(1, 100): g1 = g * 5 # print(g1) for m in range(1, 100): m1 = m * 3 # print(m1) for x in range(1, 100): x1 = x * 1/3 # print(x1) if g1 + m1 + x1 == 100: if g + m + x ==100: print(" 100 文钱买 100 只鸡,其中公鸡{0}只,母鸡{1}只,小鸡{2}只".format(g,m,x))
标签:鼠标 enum com 键值 下划线 and pre 注意 art
原文地址:http://www.cnblogs.com/lianzibing/p/6105287.html