Python之小练习

时间:2023-12-25 23:24:31

1、1 2 3 4 5 6 7 8能组成多少个不同的两位数?

count = 0
for i in range(1,9):
for V in range(1,9):
if i != V:
count+=1
# count=count-8;
print(count) #如果是列表[1,3,5,9]的形式,那么如下
ls = [1,3,5,9]
count = 0
for i in range(0,len(ls)):
for V in range(0,len(ls)):
if i != V:
count+=1
print(count)
2、
l1 = [11, 22, 33]
l2 = [22, 33, 44]
# 获取l1中有,l2中没有的元素
# 获取l2中有,l1中没有的元素
# 获取l1,l2中内容不同的元素 for i in l1:
if i not in l2:
print(i)
for i in l2:
if i not in l1:
print(i) for i in l1:
if i not in l2:
print(i)
for i in l2:
if i not in l1:
print(i)
# 99乘法表
# 第一种:
for i in range(1,10):
string = ""
for j in range(1,i+1):
string += str(j) + "*" +str(i) + "=" +str(j*i) + "\t"
print(string) # 第二种
for i in range(1,10):
for j in range(1+i):
print(str(j) + "*" +str(i) + "=" +str(j*i) + "\t",end='')
print("\n",end='')
# 输出两个不同数字的组合,且数字不能重复

li = [1,2,3,4]
l = len(li)
for i in range(0,l-1):
for v in range(i+1,l):
print(li[i],li[v]) # 请用代码实现列表li=['alex','eric',123]元素之间用下划线连接起来
# 如果列表有多个元素不是字符串,用for循环将其修改

li=['alex','eric',123]
li[2] = str(li[2])
v = "_".join(li)
print(v)
#公鸡5文钱一只,母鸡3文钱一只,三只小鸡一文钱,总共要买一百只鸡,其中公鸡母鸡小鸡都要有
#请问各买多少只才能凑足100文钱?

for x in range(1,100//5):
for y in range(1,100//3):
for z in range(1,100):
if x + y + z == 100 and 5*x +3*y +(z/3) ==100:
print(x,y,z) # 使用for,len,range输出元组的索引
# 使用enumerate输出元组和序号(序号从10开始)

tu = ('alex','eric','rain')
for idx in range(len(tu)):
print(idx)
for idx,lex in enumerate(tu,10):
print(idx,lex)
# 有如列表num=[2,7,11,15,8,7],请找任意两个元素相加能够等于9的元素集合,索引
num=[2,7,11,15,8,7]
a = []
for i in num:
for j in num:
if i + j == 9:
a.append((i,j))
print(a) a = []
for i in range(len(num)):
for j in range(len(num)):
if num[i] + num[j] == 9:
a.append((i,j))
print(a) # 分页显示内容,每页显示10条数据,提示用户需要查看的页码:
# 当输入指定的页码则显示指定数据。
user_list = []
for i in range(1,302):
temp = {'name':'alex' + str(i),'email':'alex@live.com' + str(i),'password':'pwd' + str(i)}
user_list.append(temp) while True:
s = input("请输入页数:")
s = int(s)
start = (s-1)*10
end = s*10
result = user_list[start:end]
for item in result:
print(item,type(item))