1.元素分类
有如下值集合[11,22,33,44,55,66,77,88,99,90], 将所有大于66的值保存至字典的第一个key中,将小于66值保存至第二个key的值中。
li = [11,22,33,44,55,66,77,88,99,90] dic = {} n = [] m = [] for i in li: if i > 66: n.append(i) if i < 66: m.append(i) dic.update(k1 = m, k2 = n) print(dic)
2.输出商品列表,用户输入序号,显示用户选中的商品。
商品 li = ["手机", "电脑", "鼠标垫", "游艇" ]
a. 允许用户添加商品
b. 用户输入序号显示内容
li = ["手机", "电脑", "鼠标垫", "游艇" ] mer = input("请输入您想加入的商品:") li.append(mer) print("商品现有",li) wan = int(input("请输入序号:")) print(li[wan])
3. 用户交互显示类似省市县N级联动的选择
a. 允许用户增加内容
b. 允许用户选择查看某一个级别内容
4.列举布尔值是False的所有值
0 None "" () {} []
5. 有两个列表
l1 = [11, 22, 33]
l2 = [22, 33, 44]
a. 获取内容相同的元素列表
l1 = [11, 22, 33] l2 = [22, 33, 44] for i1 in l1: for i2 in l2: if i1 == i2: print(i1)
b. 获取l1中有, l2中没有的元素列表
l1 = [11, 22, 33] l2 = [22, 33, 44] for i1 in l1: if i1 not in l2: print(i1)
c. 获取l2 中有,l1中没有的元素列表
l1 = [11, 22, 33] l2 = [22, 33, 44] for i2 in l2: if i2 not in l1: print(i2)
d. 获取l1 和 l2 中内容都不同的元素
l1 = [11, 22, 33] l2 = [22, 33, 44] for i2 in l2: if i2 not in l1: print(i2) for i1 in l1: if i1 not in l2: print(i1)
6.转换
a. 将字符串 s = "alex"转换为列表
s = "alex" li = list(s) print(li)
b. 将字符串s = "alex"转换为元组
s = "alex" li = tuple(s) print(li)
c. 将列表li = ["alex", "seven"]转换为元组
li = ["alex", "seven"] tu = tuple(li) print(tu)
d. 将元组 tu = ("Alex", "seven")转换为列表
tu = ("Alex", "seven") li = list(tu) print(li)
7.利用 for 循环和range输出
a. for循环从大到小输出1-100
for i in range(100, 0, -1): print(i, end = " ")
.....1 后面省略
b. for 循环从小到大输出100 -1
for i in range(1, 101): print(i, end = " ")
.....100
c. while 循环从大到小输出1-100
i = 100 while i >= 1: print(i, end = " ") i -= 1
.....1
d.while 循环从小到大输出100-1
i = 1 while i <= 100: print(i, end = " ") i += 1
......100
8.购物车
功能要求:
要求用户输入总资产,例如:2000
显示商品列表,让用户根据序号选择商品,加入购物车购买,如果商品总额大于总资产,提示余额不足,否则,购买成功。
goods = [ {"name": "电脑", "price": 1999}, {"name": "鼠标", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美女", "price": 998} ] ass = int(input("请输入你的总资产:")) dic = {} num =1 for i in goods: n = i["name"] dic.update({num : n}) num +=1 print(dic) zm = 0 while True: wan = int(input("请输入你想要的商品序号(按数字0结束;")) if wan == 0: break if wan > 4: print("请重新输入") continue v = dic.get(wan) print("你想要购买的是:", v) li = [0,1999, 10, 20, 998] print("它的价格为:%d"%li[wan]) p = int(li[wan]) zm = zm + p if zm > ass: print("余额不足,请滚去充值") else: print("购买成功!")
![](https://img2018.cnblogs.com/blog/1697453/201906/1697453-20190603133138432-550917998.png)
9. 有1,2,3,4,5,6,7,8 8个数字,能组成多少个互不相同且重复数字的两位数
count = 0 for i in range(1,9): n = str(i) for j in range(1,9): m = str(j) if m != n: count += 1 v = m + n print(int(v)) print("一共%d个"%count)
10.99乘法表
for i in range(1, 10): for j in range(1, i+1): n = i* j print(j,"x",i,"=",n," ", end = "") #内循环之间不换行 print(end = " ") #内循环结束换行一次
11.有以下列表
nums = [2, 7, 11, 15, 1, 8]
请找到列表中任意相加等于9的元素集合,如:[(0, 1), (4, 5)]
nums = [2, 7, 11, 15, 1, 8] l1 = [] l = len(nums) for i in range(0, l-1): for j in range(i+1,l): if nums[i] + nums[j] == 9: n = (nums[i], nums[j]) l1.append(n) print(l1)
12.用Python开发程序自动计算方案
公鸡5文钱一只, 母鸡3文钱一只,小鸡3只一文钱,用100文钱买100只鸡,其中公鸡,母鸡,小鸡都必须要有,问公鸡,母鸡,小鸡要买多少只刚好凑足100文钱?
gj = 5
mj = 3
xj = 1/3
for x in range(1,100//5):
for y in range(1,100//3):
for z in range(100):
if gj * x + mj * y + xj * z == 100 and x + y + z == 100:
print(x,y,z)
13.请用代码实现:利用下划线将列表的每一个元素拼接成字符串,li = ['alex', 'eric', 'rain']
li = ['alex', 'eric', 'rain'] for i in li: n = "_".join(i) print(n)
14.查找列表元素,移除每个元素的空格,并查找以a 或A开头 并且以c 结尾的所有元素。
li = ["alex", " aric", "Alex", "Tony", "rain"] for i in li: v = i.strip() if v.endswith('c') : if v.startswith('a') or v.endswith('A'): print(v)
tu = ('alexc', ' Aric', 'Tony', 'rain') v = list(tu) for i in v: n = i.strip() if n.endswith('c'): if n.startswith('a') or n.startswith('A'): print(n)
dic = {"k1": "alex", "k2": " aric", "k3": "Tony", "k4": "Arainc"} for v in dic.values(): n = v.strip() if n.endswith('c'): if n.startswith('a') or n.startswith('A'): print(n)
15. 写代码,有如下列表,按照要求实现每一个功能。
a.计算列表长度并输出
li = ['alex', 'eric', 'arin'] print(len(li))
b. 列表中追加元素"seven",并输出添加后的列表
li = ['alex', 'eric', 'arin'] li.append('seven') print(li)
16. 类似13题,如果列表里有数字:
li = ['alex', 'eric', 123] l2 = [] for i in li: n = str(i) l2.append(n) v = "_".join(l2) print(v)
17. 分页显示内容
a. 通过for循环创建301条数据,数据类型不限,如:
user1 email-1 pwd1
user2 email-2 pwd2
.........
b.提示用户 请输入要查看的页码,当用户输入指定页码时,显示指定数据
注意:
- 每页显示10条数据
- 用户输入页码是非十进制数字,则提示输入内容格式错误
li = [] for i in range(1,302): dic = {"user": "user" + str(i), "email": "dabai@qq" + str(i), "pwd": "52" + str(i)} li.append(dic) while True: s = input("请输入页码:") if s.isdecimal(): s = int(s) else: print("请重新输入10进制数") continue # 输入1,查看0-10页 s-1 *10 #输入2,查看11-20页 s * 10 start = (s - 1) * 10 end = s * 10 n = li[start:end] for item in n: print(item)