python第二周作业——chapter 3,chapter 4 列表

本周主要学习了列表的相关知识,第三章包括如何定义列表以及如何增删元素; 如何对列表进行永久性排序, 以及如何为展示列表而进行临时排序; 第四章主要是列表的遍历,以及列表切片,元组。

Chapter 3

Q 3-4,3-5,3-6,3-9

主要考察知识点是列表的增删改及长度

代码:

#3-4 Make a list that includes at least three people you’d like to invite to dinner Then use your list to print a message to each person, inviting them to dinner
guests = ['Alice','David','Henry','Ellen']
for guest in guests:
    print(guest.title() + ", would you like to have dinner with me?")

#3-5
#Start with your program from Exercise 3-4 Add a print statement at the end of your program stating the name of the guest who can’t make it
guest_failed = 'Henry'
print(guest_failed.title() + "couldn't make it.")

#Modify your list, replacing the name of the guest who can’t make it with the name of the new person you are inviting
guests[-2] = 'Jay'

#Print a second set of invitation messages, one for each person who is still in your list
for guest in guests:
    print("I will invite " + guest + " to dinner.")

#3-6
#Add a print statement to the end of your program informing people that you found a bigger dinner table
print("I have found a bigger dinner table.")

#Use insert() to add one new guest to the beginning of your list
guests.insert(0,'Lucy')

#Use insert() to add one new guest to the middle of your list
guests.insert(int(len(guests)/2),'Tom')

#Use append() to add one new guest to the end of your list
guests.append('Jane')

#Print a new set of invitation messages, one for each person in your list
for guest in guests:
    print(guest.title() + ", would you like to have dinner with me?")

#3-9 use len() to print a message indicating the number of people you are inviting to dinner
print(len(guests))

运行结果:

Alice, would you like to have dinner with me?
David, would you like to have dinner with me?
Henry, would you like to have dinner with me?
Ellen, would you like to have dinner with me?
Henrycouldn't make it.
I will invite Alice to dinner.
I will invite David to dinner.
I will invite Jay to dinner.
I will invite Ellen to dinner.
I have found a bigger dinner table.
Lucy, would you like to have dinner with me?
Alice, would you like to have dinner with me?
Tom, would you like to have dinner with me?
David, would you like to have dinner with me?
Jay, would you like to have dinner with me?
Ellen, would you like to have dinner with me?
Jane, would you like to have dinner with me?
7


Q 3-8

主要考察知识点是列表的永久性排序与临时排序,正向排序和反向排序

代码:

#3-8 Seeing the World: Think of at least five places in the world you’d like to visit
#Store the locations in a list Make sure the list is not in alphabetical order
places = ['Tibet','Venice','Turkey','Paris','Tokyo','Miami']

#Print your list in its original order Don’t worry about printing the list neatly, just print it as a raw Python list
print(places)

#Use sorted() to print your list in alphabetical order without modifying the actual list
print(sorted(places))

#Show that your list is still in its original order by printing it
print(places)

#Use sorted() to print your list in reverse alphabetical order without changing the order of the original list
print(sorted(places,reverse = True))

#Show that your list is still in its original order by printing it again
print(places)

#Use reverse() to change the order of your list Print the list to show that its order has changed
places.reverse()
print(places)

#Use reverse() to change the order of your list again Print the list to show it’s back to its original order
places.reverse()
print(places)

#Use sort() to change your list so it’s stored in alphabetical order Print the list to show that its order has been changed
places.sort()
print(places)

#Use sort() to change your list so it’s stored in reverse alphabetical order.Print the list to show that its order has changed
places.sort(reverse = True)
print(places)

运行结果:

['Tibet', 'Venice', 'Turkey', 'Paris', 'Tokyo', 'Miami']
['Miami', 'Paris', 'Tibet', 'Tokyo', 'Turkey', 'Venice']
['Tibet', 'Venice', 'Turkey', 'Paris', 'Tokyo', 'Miami']
['Venice', 'Turkey', 'Tokyo', 'Tibet', 'Paris', 'Miami']
['Tibet', 'Venice', 'Turkey', 'Paris', 'Tokyo', 'Miami']
['Miami', 'Tokyo', 'Paris', 'Turkey', 'Venice', 'Tibet']
['Tibet', 'Venice', 'Turkey', 'Paris', 'Tokyo', 'Miami']
['Miami', 'Paris', 'Tibet', 'Tokyo', 'Turkey', 'Venice']
['Venice', 'Turkey', 'Tokyo', 'Tibet', 'Paris', 'Miami']

易混点:

append与insert的区别,del与remove的区别;

sort与sorted函数,reverse作为函数与作为参数。


Chapter 4

Q4-5

知识点:创建列表,range()函数,简单统计计算

代码:

#4-5 Make a list of the numbers from one to one million,
number = []
for i in range(1,1000001):
    number.append(i)

# and then use min() and max() to make sure your list actually starts at one and ends at one million
print(min(number))
print(max(number))
#Also, use the sum() function to see how quickly Python can add a million numbers
print(sum(number))

运行结果:

1
1000000
500000500000


Q4-8

知识点:n次方表示

代码:

#4-8 Make a list of the frst 10 cubes (that is, the cube of each integer from 1 through 10), and use a for loop to print out the value of each cube
cube = []
for i in range(1,11):
    cube.append(i**3)
for num in cube:
    print(num)

运行结果:

1
8
27
64
125
216
343
512
729
1000


Q4-9,4-10

知识点:列表解析,切片

代码:

#4-9 Use a list comprehension to generate a list of the first 10 cubes
cube = [i**3 for i in range(1,11)]
print(cube)

#4-10
# Print the message, The frst three items in the list are: Then use a slice to print the first three items from that program’s list
print("The first three items in the list are:" )
print(cube[:3])

#Print the message, Three items from the middle of the list are: Use a slice to print three items from the middle of the list
print("Three items from the middle of the list are:" )
print(cube[int(len(cube)/2)-1:int(len(cube)/2)+2])

#Print the message, The last three items in the list are: Use a slice to print the last three items in the list
print("The last three items in the list are:" )
print( cube[-3:])

运行结果:

[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
The first three items in the list are:
[1, 8, 27]
Three items from the middle of the list are:
[125, 216, 343]
The last three items in the list are:
[512, 729, 1000]


Q4-13

知识点:元组

代码:

#4-13 Buffet: A buffet-style restaurant offers only fve basic foods Think of five simple foods, and store them in a tuple
#Use a for loop to print each food the restaurant offers
food = ('hamburger','sandwich','cake','juice','fish')
for item in food:
    print(item)

#The restaurant changes its menu, replacing two of the items with different foods Add a block of code that rewrites the tuple, and then use a for loop to print each of the items on the revised menu
food = ('chips','vegetable','cake','juice','fish')
for item in food:
    print(item)

运行结果:

hamburger
sandwich
cake
juice
fish
chips
vegetable
cake
juice
fish


易错点:range()函数区间左闭右开;列表切片上下限表示


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值