第二周作业_Chapter 4课后练习

4-2 动物

代码:

animals = ['cat','dog','fish']
for animal in animals:
print(animal)
for animal in animals:
print("A " + animal + " would make a great pet.")
print("Any of these animals would make a great pet!")

运行结果:



4-3 数到20

代码:

for value in range(1,21):
   print(value)

运行结果:



4-5 计算1~1000000的总和

代码:

numbers = list(range(1,1000001))
print(min(numbers))
print(max(numbers))
print(sum(numbers))

运行结果:



4-9 立方解析

代码:

numbers = [value**3 for value in range(1,11)]
for number in numbers:
print(number)

运行结果:



4-10 切片

代码:

numbers = [value**3 for value in range(1,11)]
for number in numbers:
print(number)
print("The first three items in the list are:")
for item in numbers[:3]:
    print(item)
print("Three items from the middle of the list are:")
for item in numbers[4:7]:
    print(item)
print("The last three items in the list are:")
for item in numbers[-3:]:
    print(item)

运行结果:


猜你喜欢

转载自blog.csdn.net/cchsblog/article/details/79562261