python基础知识第七篇(练习)

# a.     获取内容相同的元素列表
 l1   =  [11,22,33]
 l2   =  [22,33,44]
 for l in l1:
     if l in l2:
         print(l)

 


# b. 获取 l1 中有, l2 中没有的元素列表
 l1   =  [11,22,33]
 l2   =  [22,33,44]
 for l in l1:
     if l not in l2:
         print(l)

 


# 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 l1:
     if i not in l2:
         print(i)
 for i in l2:
     if i not in l1:
          print(i)

 



#有12345678 八个数字,如何组成不重复的两位数,共有多少种?
 count=0
 for i in range(1, 9):
     for i1 in range(1,9):
         if i!=i1:
             count +=1
 print(count)

 

 count=0
 list1=[1,3]
 for i in range(0,len(list1)):
     for i1 in range(0,len(list1)):
         if i!=i1:
             count +=1

 


#九九乘法表
 for i in range(1,10):
     for i1 in range(1,i+1):
         print(str(i)+"*"+str(i1)+"="+str(i*i1)+"\t",end="")
     print(end="\n")

 



#公鸡5文钱一只,母鸡3文钱一只,小鸡3只一文钱,100文钱只能买100只鸡,有哪些组合?
 for x in range(1, (100 // 5+1) ):
     for y in range(1, (100 // 3+1) ):
         for z in range(1, 101):
             if x + y + z == 100 and 5 * x + 3 * y + z / 3 == 1000:
                 print(x,y,z)

 


#li=["dsk","ds","pp"],用下划线把列表的值连起来
 li=["dsk","ds","pp",12]
 for i in li:
     print(i,end="_")

 

 li=["dsk","ds","pp",12]
 li[3]=str(li[3])
 v="_".join(li)
 print(v)

 



#获取元组tu=("dhik","xjo","xsx"),计算元组的长度并输出,获取元组的第二个元素并输出
 tu=("dhik","xjo","xsx")
 v=len(tu)
 print(v)
 
tu=("dhik","xjo","xsx")
 v=tu[1]
 print(v)

 



#nums=[1,3,5,2,8,4]
#找出列表中两个元素相加等于9的结合
nums=[1,3,5,2,8,4]
cound=[]
for i in nums:
    for j in nums:
        if i+j==9:
            cound.append((i,j))

print(cound)

 

posted @ 2018-12-14 22:29  图图W  阅读(223)  评论(0编辑  收藏  举报