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

  • 34
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hann Yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值