挑战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

版权声明:本文为博主作者:Hann Yang原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/boysoft2002/article/details/135185085

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2024年1月3日
下一篇 2024年1月3日

相关推荐