挑战Python100题(5)

发布时间:2023年12月24日

100+ Python challenging programming exercises 5

Question 41

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.

定义一个函数,该函数可以生成和打印一个元组,其中的值是1到20之间的数字的平方(两者都包括在内)。

提示:

使用**运算符获取一个数字的幂。对循环使用range()。使用list.append()将值添加到列表中。使用元组从列表中获取元组。

Solution

def printTuple():
    li=list()
    for i in range(1,21):
        li.append(i**2)
    print(tuple(li))

printTuple()

Out:
(1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400)


Question 42

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.

对于给定的元组(1,2,3,4,5,6,7,8,9,10),编写一个程序,将前半个值打印在一行中,将后半个值打印到一行中。

提示:使用[n1:n2]表示法从元组中获取切片。

Solution

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

Out:

(1, 2, 3, 4, 5)
(6, 7, 8, 9, 10)?


Question 43

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.

编写一个程序生成并打印另一个元组,该元组的值是给定元组(1,2,3,4,5,6,7,8,9,10)中的偶数。

提示:使用“for”迭代元组使用tuple()从列表中生成元组。

Solution

tp=(1,2,3,4,5,6,7,8,9,10)
li=list()
for i in tp:
    if i%2==0:
        li.append(i)

tp2=tuple(li)
print(tp2)

Out:

(2, 4, 6, 8, 10)?


Question 44

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.

编写一个接受字符串作为输入的程序,如果字符串为“yes”、“YES”或“Yes”,则打印“Yes”;否则打印“No”。

提示:使用if语句判断条件。

Solution

s=input()
if s=="yes" or s=="YES" or s=="Yes":
    print("Yes")
else:
    print("No")

In:
yes
Out:
Yes?


Question 45

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.

编写一个程序,可以使用过滤功能过滤列表中的偶数。列表为:[1,2,3,4,5,6,7,8,9,10]。

提示:使用filter()过滤列表中的一些元素。使用lambda定义匿名函数。

Solution

li = [1,2,3,4,5,6,7,8,9,10]
evenNumbers = filter(lambda x: x%2==0, li)
print(list(evenNumbers))

Out:

[2, 4, 6, 8, 10]


Question 46

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.

编写一个程序,它可以映射()来制作一个元素为[1,2,3,4,5,6,7,8,9,10]中元素平方的列表。

提示:使用map()生成列表。使用lambda定义匿名函数。

Solution

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

Out:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]


Question 47

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.

编写一个程序,它可以映射()和过滤()来生成一个列表,其元素是[1,2,3,4,5,6,7,8,9,10]中偶数的平方。

提示使用map()生成列表,并使用filter()过滤列表中的元素。lambda定义匿名函数。

Solution

li = [1,2,3,4,5,6,7,8,9,10]
evenNumbers = map(lambda x: x**2, filter(lambda x: x%2==0, li))
print(list(evenNumbers))

Out:

[4, 16, 36, 64, 100]


Question 48

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.

编写一个程序,它可以filter()来生成一个元素为1到20之间偶数(两者都包含在内)的列表。

提示:使用filter()过滤列表中的元素。使用lambda定义匿名函数。

Solution

evenNumbers = list(filter(lambda x: x%2==0, range(1,21)))
print(evenNumbers)

Out:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]


Question 49

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.

编写一个程序,它用map()来生成一个元素为1到20之间的数字平方的列表(两者都包含在内)。

提示:使用map()生成列表,并使用lambda定义匿名函数。

Solution

squaredNumbers = list(map(lambda x: x**2, range(1,21)))
print(squaredNumbers)

Out:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]


Question 50

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

Hints: Use @staticmethod decorator to define class static method.

定义一个名为American的类,该类具有名为printNationality的静态方法。

提示:使用@staticmethod decorator定义类静态方法。

Solution

class American(object):
    @staticmethod
    def printNationality():
        print("America")

anAmerican = American()
anAmerican.printNationality()
American.printNationality()

Out:
?America
America


来源:GitHub - 965714601/Python-programming-exercises: 100+ Python challenging programming exercises

文章来源:https://blog.csdn.net/boysoft2002/article/details/135185085
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。