some answers for chapter 4

4-3

for i inrange(1,21):

    print(i, end=' ')

output

1 2 3 4 5 6 7 8 9 1011 12 13 14 15 16 17 18 19 20

4-5

nums = range(1,1000000)

print(min(nums))

print(max(nums))

print(sum(nums))

output

1

999999

499999500000

4-6 4-10

nums = [i for i inrange(1,21,2)]

for num in nums:

    print(num,end=' ')

print("\n")

print("The firstthree items in the list are:")

for num in nums[:3]:

    print(num,end=' ')

print("\n")

print("Threeitems from the middle of the list are:")

for num in nums[3:6]:

    print(num,end=' ')

print("\n")

print("The lastthree items in the list are:")

for num in nums[-3:]:

    print(num,end=' ')

output

1 3 5 7 9 11 13 15 17 19

 

The first three items in the list are:

1 3 5

 

Three items from the middle of the list are:

7 9 11

 

The last three items in the list are:

15 17 19

4-7

nums = [i for i inrange(3,21,3)]

for num in nums:

    print(num,end=' ')

output

3 6 9 12 15 18

4-9

nums = []

for i inrange(1,10):

    nums.append(i**3)

for num in nums:

    print(num,end=' ')

print("\n")

nums = [i**3for i inrange(1,10)]

for num in nums:

    print(num,end=' ')

output

1 8 27 64 125 216 343 512 729

 

1 8 27 64 125 216 343 512 729

4-13

foods = ('pizza','rice','fruit','vegetable','meet')

for food in foods:

    print(food,end=' ')

print('\n')

foods = ('noodles','water','fruit','vegetable','meet')

for food in foods:

    print(food,end=' ')

foods[1]='rice'

output

pizza rice fruit vegetable meet

 

noodles water fruit vegetable meet

---------------------------------------------------------------------------

TypeError                                 Traceback(most recent call last)

<ipython-input-6-dc9caf9e544f> in <module>()

     6for food in foods:

     7     print(food,end=' ')

----> 8foods[1]='rice'

 

TypeError: 'tuple' object does not support itemassignment

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值