100题练习记录(五)

    科技2025-12-18  13

    Question 31: Define a function that can accept an integer number as input and print the “It is an even number” if the number is even, otherwise print “It is an odd number”.

    Hints:

    Use % operator to check if a number is even or odd.

    def check_num(n): if n%2 ==0: print("The number %d. It is an odd number"%n) elif n%2 != 0: print("The number %d. It is an even number" % n) check_num(3) check_num(8)

    Question 32: Define a function which can print a dictionary where the keys are numbers between 1 and 3 (both included) and the values are square of keys.

    Hints:

    Use dict[key]=value pattern to put entry into a dictionary. Use ** operator to get power of a number.

    def dic_num(): dic = {} for i in range(1,4): dic[i] = i ** 2 print(dic) dic_num()

    Question 33: Define a function which can print a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys.

    Hints:

    Use dict[key]=value pattern to put entry into a dictionary. Use ** operator to get power of a number. Use range() for loops.

    import math def dic_20(): d = {} for i in range(1,21): d[i] = int(math.pow(i,2)) print(d) dic_20()

    Question 34: Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the values only.

    Hints:

    Use dict[key]=value pattern to put entry into a dictionary. Use ** operator to get power of a number. Use range() for loops. Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.

    def dic_value(): d = {} for i in range(1,21): d[i] = i * i print(d.values()) dic_value()

    Question 35: Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the keys only.

    Hints:

    Use dict[key]=value pattern to put entry into a dictionary. Use ** operator to get power of a number. Use range() for loops. Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.

    def dic_key(): d = {} for i in range(1,21): d[i] = i * i for k in d.keys(): print(k,end=' ') dic_key()

    Question 36: Define a function which can generate and print a list where the values are square of numbers between 1 and 20 (both included).

    Hints:

    Use ** operator to get power of a number. Use range() for loops. Use list.append() to add values into a list.

    def d_value(): l = [] d = {} for i in range(1,21): d[i] = i ** 2 for v in d.values(): l.append(v) print(l) d_value()

    Question 37: Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the first 5 elements in the list.

    Hints:

    Use ** operator to get power of a number. Use range() for loops. Use list.append() to add values into a list. Use [n1:n2] to slice a list

    def d_5(): l = [] d = {} for i in range(1,21): d[i] = i**2 for k,v in d.items(): l.append(v) print(l[0:5]) d_5()

    Question 38: Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the last 5 elements in the list.

    Hints:

    Use ** operator to get power of a number. Use range() for loops. Use list.append() to add values into a list. Use [n1:n2] to slice a list

    def d_l5(): l = [] d = {} for i in range(1,21): d[i] = i**2 for k,v in d.items(): l.append(v) print(l[-5::]) d_l5()

    Question 39: Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print all values except the first 5 elements in the list.

    Hints:

    Use ** operator to get power of a number. Use range() for loops. Use list.append() to add values into a list. Use [n1:n2] to slice a list

    def d_e_5(): l = [] d = {} for i in range(1,21): d[i] = i**2 for k,v in d.items(): l.append(v) print(l[5::]) d_e_5()

    Question 40: Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included).

    Hints:

    Use ** operator to get power of a number. Use range() for loops. Use list.append() to add values into a list. Use tuple() to get a tuple from a list.

    def tuple_num(): l = [] for i in range(1,21): l.append(i**2) tup = tuple(l) print(tup) tuple_num()

    Question 41: With a given tuple (1,2,3,4,5,6,7,8,9,10), write a program to print the first half values in one line and the last half values in one line.

    Hints:

    Use [n1:n2] notation to get a slice from a tuple.

    tu = (1,2,3,4,5,6,7,8,9,10) print(tu[:5]) print(tu[5:])

    Question 42: Write a program to generate and print another tuple whose values are even numbers in the given tuple (1,2,3,4,5,6,7,8,9,10).

    Hints:

    Use “for” to iterate the tuple Use tuple() to generate a tuple from a list.

    tu = (1,2,3,4,5,6,7,8,9,10) l =[] for i in tu: if i % 2 == 0: l.append(i) print(tuple(l))

    Question 43: Write a program which accepts a string as input to print “Yes” if the string is “yes” or “YES” or “Yes”, otherwise print “No”.

    Hints:

    Use if statement to judge condition.

    s = input("请输入:") if s in ("yes", "YES","Yes"): print("Yes") else: print("No")

    Question 44: Write a program which can filter even numbers in a list by using filter function. The list is: [1,2,3,4,5,6,7,8,9,10].

    Hints:

    Use filter() to filter some elements in a list. Use lambda to define anonymous functions.

    #方法一 l = [1,2,3,4,5,6,7,8,9,10] l1 = [i for i in l if i % 2 == 0] print(l1) #方法二 l = [1,2,3,4,5,6,7,8,9,10] evenNumbers = filter(lambda x:x % 2 == 0,l) print(list(evenNumbers))

    Question 45: Write a program which can map() to make a list whose elements are square of elements in [1,2,3,4,5,6,7,8,9,10].

    Hints:

    Use map() to generate a list. Use lambda to define anonymous functions.

    l = [1,2,3,4,5,6,7,8,9,10] values = map(lambda x:x**2,l) print(list(values))

    Question 46: Write a program which can map() and filter() to make a list whose elements are square of even number in [1,2,3,4,5,6,7,8,9,10].

    Hints:

    Use map() to generate a list. Use filter() to filter elements of a list. Use lambda to define anonymous functions.

    l = [1,2,3,4,5,6,7,8,9,10] #分布操作见注释部分,使用lambda表达式见下 # r1 = filter(lambda x:x % 2 == 0,l) # r2 = map(lambda y:y**2,list(r1)) r2 = map(lambda y:y**2,list(filter(lambda x:x % 2 == 0,l))) print(list(r2))

    Question 47: Write a program which can filter() to make a list whose elements are even number between 1 and 20 (both included).

    Hints:

    Use filter() to filter elements of a list. Use lambda to define anonymous functions.

    r = filter(lambda x:x % 2 == 0,[x for x in range(1,21)]) print(list(r))

    Question 48: Write a program which can map() to make a list whose elements are square of numbers between 1 and 20 (both included).

    Hints:

    Use map() to generate a list. Use lambda to define anonymous functions.

    r = map(lambda x:x**2,[x for x in range(1,21)]) print(list(r))

    Question 49 : Define a class named American which has a static method called printNationality.

    Hints:

    Use @staticmethod decorator to define class static method.

    class American(object): @staticmethod def printNationality(): return "USA" a = American() print(a.printNationality()) #通过类来调用 print(American.printNationality())

    Question 50: Define a class named American and its subclass NewYorker.

    Hints:

    Use class Subclass(ParentClass) to define a subclass.

    class American(object): def __init__(self): self.nation = "USA" class NewYorker(American): pass n =NewYorker() print(n.nation)
    Processed: 0.021, SQL: 9