一、l1=[11,22,33],l2=[22,33,44]python
a、獲取內容相同元素的列表小程序
b、獲取l1中有,l2中沒有的元素列表app
c、獲取l2中有,l1中沒有的元素列表spa
d、獲取l1中l2內容都不一樣的元素列表3d
1 # -*- coding:utf-8 -*- 2 l1=[11,22,33] 3 l2=[22,33,44] 4 a=[] 5 b=[] 6 c=[] 7 d=[] 8 for i in l1: 9 if i in l2: 10 a.append(i) 11 print(a) 12 13 for i in l1: 14 if i not in l2: 15 b.append(i) 16 print(b) 17 18 for i in l2: 19 if i not in l1: 20 c.append(i) 21 print(c) 22 23 b.extend(c) 24 d=b 25 print(d)
====================運行結果========================code
二、1,2,3,4,5,6,7,8 。8個數字,能組成多少了互相不相同且無重複的兩位數字。blog
1 # -*- coding:utf-8 -*- 2 a = [] 3 for i in range(1, 9): 4 for j in range(1, 9): 5 if i != j: 6 temp = [] 7 temp = str(i) + str(j) 8 temp = int(temp) 9 a.append(temp) 10 print(a)
==================運算結果===================utf-8
三、用range輸出99乘法表開發
1 # -*- coding:utf-8 -*- 2 for i in range(1,10): 3 for j in range(1,i+1): 4 print("%d*%d=%d"%(j,i,i*j),end="\t") 5 print("\n",end=" ")
=================運算結果============================input
四、用python開發一個自動計算的小程序
公雞5文錢一隻,母雞3文錢一隻,小雞3只一文錢,用100文錢去買100只雞,其中公雞,母雞,小雞必須都要有,問公雞,母雞,小雞要買多少隻恰好湊足100文錢??
1 # -*- coding:utf-8 -*- 2 n=1 3 for x in range(1,101): 4 for y in range(1,101): 5 for z in range(1,101): 6 if (x+y+3*z)==100 and (5*x+3*y+z)==100: 7 print("方案:%d"%n,end="\t") 8 print("花費100錢,買100只雞,其中公雞:%d只,母雞:%d只,小雞:%d只"%(x,y,3*z)) 9 n+=1
===============運算結果================
五、 有以列表,nums=[2,7,11,15,1,8,7]
請找到列表中人力兩個元素相加可以等於9的元素集合,如[(8,1),(4,5)]
1 # -*- coding:utf-8 -*- 2 nums = [2, 7, 11, 15, 1, 8, 7] 3 list1 = [] 4 for i in range(0, len(nums)): 5 n = 0 6 while n < len(nums): 7 if i != n: 8 if nums[i] + nums[n] == 9: 9 list1.append((nums[i], nums[n])) 10 n += 1 11 print(list1)
==============運算結果====================
六、分頁顯示內容
a、經過for 循環建立301條數據,數據類型不限,如:
zf1 qq.com1 pwd1
.。。。
b、提示用戶,請輸入要查看的頁碼,當用戶輸入指定頁碼,則顯示指定數據
注意:
每頁顯示10條數據
用戶輸入頁碼非十進制數字時,則提示輸入內容格式錯誤。
1 # -*- coding:utf-8 -*- 2 user_list = [] 3 for i in range(0, 302): 4 temp = "zf" + str(i) + "\t" + "qq@.com" + str(i) + "\t" + "pwd" + str(i) + "\n" 5 if i % 10 == 0: 6 user_list.append("name\temail\tpasswd\n") 7 user_list.append(temp) 8 user_list_page = [] 9 for j in range(len(user_list) // 11): 10 temp1 = user_list[0 + j * 11:11 + j * 11] 11 user_list_page.append(temp1) 12 while True: 13 n = (input("請輸入須要查看的頁面:(按q退出)")) 14 if n == "q": 15 break 16 if n.isnumeric(): 17 n = int(n) 18 else: 19 print("請輸入十進制數") 20 continue 21 temp3 = "" 22 for m in range(0, 11): 23 temp3 = temp3 + (user_list_page[n - 1][m]) 24 print(temp3.expandtabs(20))
==============運行結果==============