1. Given two lists, A = [1,2,3,4,5,6,7,1,2,3] and B = [4,5,6,7,8,9,10,9 , 8,11],
please use python to find the same elements in A and B and put them in list D. Find the different elements in A and B and put them in list C. Make
sure that the elements in the two lists of C and D are not repeat.
A = [1,2,3,4,5,6,7,1,2,3] B=[4,5,6,7,8,9,10,9,8,11] print(set(A)) # {1, 2, 3, 4, 5, 6, 7} print(set(B)) # {4, 5, 6, 7, 8, 9, 10, 11} D = set(A) & set(B) C = set(A) ^ set(B) print(D) # {4, 5, 6, 7} print(C) # {1, 2, 3, 8, 9, 10, 11}
2. Packaging function, can judge whether a number is even
def func (n): if n% 2 == 0: print ( " % d is even " % n) else : print ( " % d is odd " % n) func ( 11 ) # 11 is odd
3. Encapsulation function, can realize the printing of all even numbers between 1-n
def func(n): for i in range(1,n+1): if i%2==0: print(i) func(10) # 2 # 4 # 6 # 8 # 10
4. Encapsulation function, you can find the maximum value in the integer list
lst = [2,7,3,9,1,4] def func(n): x = n[0] for i in n: if i>x: x=i return x print(func(lst)) # 9
5. Check the length of the incoming list, if it is greater than 2, keep the content of the first two lengths, and return the new content to the caller
lst = [2,7,3,9,1,4] def func(n): if len(lst)>2: return lst[:2] print(func(lst)) # [2, 7]
6. Encapsulation function, can complete the reverse order of the list, without using the built-in function
lst = [2,7,3,9,1,4] # print(sorted(lst,reverse=True)) # print(lst.sort()) # print(lst) def func(n): for i in range(len(n)-1): for j in range(len(n)-i-1): if n[j]<n[j+1]: n [j], n [j +1] = n [j + 1 ], n [j] return n print (func (lst)) # [9, 7, 4, 3, 2, 1]
# or
lst1 = [2,7,3,9,1,4]
print(lst1[::-1])
# [4, 1, 9, 3, 7, 2]
7. Encapsulation function, you can judge whether an integer is a prime number
# For a positive integer n, if all integers between 2 and x are divided and cannot be divided, then n is a prime number
from math import sqrt def func (n): if n == 1 : return ' is prime number ' else : for i in range (2, int (sqrt (n)) + 1 ): if n% i == 0: return ' Not a prime number ' return ' is a prime number ' print (func (2 )) #is a prime number
8. Encapsulation function, you can print all the prime numbers between 1-a, and wrap every 4
def func(n): count=0 for i in range(1,n+1): for j in range(2,int(sqrt(i)+1)): if i%j==0: break else: count=count+1 print(i,end=' ') if count%4==0: print("\n") print(func(100)) # 1 2 3 5 # # 7 11 13 17 # # 19 23 29 31 # # 37 41 43 47 # # 53 59 61 67 # # 71 73 79 83 # # 89 97 None
9. Encapsulation function, can print all prime numbers between 2-a, use recursive function
def func(n): if n==2: return 2 for i in range(2,int(sqrt(n)+1)): if n%i==0: return func(n-1) else: print(n) return func(n-1) print(func(100)) # 97 # 89 # 83 # 79 # 73 # 71 # 67 # 61 # 59 # 53 # 47 # 43 # 41 # 37 # 31 # 29 # 23 # 19 # 17 # 13 # 11 # 7 # 5 # 3 # 2
10. Encapsulate functions, find out the elements of the odd index of the incoming list and insert into the new list
def func(n): newlist=[] for i in range(0,len(n)+1,2): newlist.append(n[i]) return newlist list =['a','b','c','d','e','f','g','h','i','j','k'] print(func(list)) # ['a', 'c', 'e', 'g', 'i', 'k']
11. Encapsulation function, judge whether a number is the number of daffodils (the number of daffodils is a three-digit number, and the sum of the cube of each number is also equal to this number.)
s = input ( " Please enter a number: " ) if len (s) == 3 : result = int (s [0]) ** 3 + int (s [1]) ** 3 + int (s [2]) ** 3 if int (s) == result: print ( " is a daffodil number " ) else : print ( " Not a daffodil number " ) else : print ( " Please enter three digits " )