挑战Python100题(5)

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, 1

你可能感兴趣的:(Python,python)