The next day, I started learning Python from scratch. In fact, I made a rapid progress yesterday. I didn't stop until 3-7. Because I wanted to use the while loop, I didn't know much about Python's while (in fact, I didn't want to learn any more because it was too late). Then I finished 3-7 this morning.
3-1 Name: store the names of some friends in a list and name them names. Access each element in the list in turn to print out the name of each friend.
#I feel that the list is to investigate the index at the beginning (the index is in the book, and I'm used to calling it subscript), so the first few questions are output one by one, and the later questions that require more guests adopt circulation.
friend_names.py
names=['yi','xia','zhou','zhao'] print(names[0]) print(names[1]) print(names[2]) print(names[3])
3-2 greetings: continue to use the list in exercise 3-1, but instead of printing each friend's name, print a message for each. Each message contains the same greeting, but is headed by the name of the corresponding friend.
print('hi,'+names[0]) print('hi,'+names[1]) print('hi,'+names[2]) print('hi,'+names[3])
#Instead of using circular structure, the main consideration should be the subscript of the list
3-3 own list: think about your favorite commuting methods, such as riding a motorcycle or driving a car, and create a list containing multiple commuting methods. Print a series of declarations about these commuting methods according to the list, such as "I would like to own a Honda motorcycle".
traffic_ways.py
traffic_ways=['bicycle','car','plane','ship'] print('I would like to own a '+traffic_ways[0]) print('I would like to own a '+traffic_ways[1]) print('I would like to own a '+traffic_ways[2]) print('I would like to own a '+traffic_ways[3])
3-4 guest list: if you could invite anyone to dinner (whether alive or dead), who would you invite? Please create a list containing at least 3 people you want to invite; Then, use this list to print messages and invite these people to dinner with you.
guest_list.py
guest_list=['a','b','c','d','e'] print('Dear '+guest_list[0]+',I would like to invite you to dinner.') print('Dear '+guest_list[1]+',I would like to invite you to dinner.') print('Dear '+guest_list[2]+',I would like to invite you to dinner.') print('Dear '+guest_list[3]+',I would like to invite you to dinner.') print('Dear '+guest_list[4]+',I would like to invite you to dinner.')
3-5 modify the guest list: you just learned that a guest cannot attend the appointment, so you need to invite another guest. Based on the program written when completing exercises 3-4, add a print statement at the end of the program to indicate which guest is unable to attend the appointment. Modify the guest list and replace the name of the guest who cannot attend the appointment with the name of the newly invited guest. Print a series of messages again and send invitations to each guest on the list.
Now let's assume that C can't participate, and you need to know the index with del, so use remove(c) to delete it. If you print again, it may be better to use the circular structure, and optimize it on the basis of 3-4
#Point out that c cannot participate print('c can not come.') #Delete c guest_list.remove('c') #Invite guests other than c again for name in guest_list: print('Dear ' +name+ ',I would like to invite you to dinner.') expand: #Delete C first and use pop to notify who can't participate #Get the index of c in the list that cannot participate print(guest_list.index('c')) #Use the pop() method to delete c because you want to indicate that c cannot be deleted absent_name=guest_list.pop(guest_list.index('c')) print(absent_name+' can not come.') for name in guest_list: print('Dear ' +name+ ',I would like to invite you to dinner.')
3-6 add guests: you just found a larger table that can accommodate more guests. Please think about which three more guests you would like to invite. Based on the program written when completing exercises 3-4 or 3-5, add a print statement at the end of the program to indicate that you have found a larger table. Use insert() to add a new guest to the beginning of the list. Use insert() to add another new guest to the middle of the list. Use append() to add the last new guest to the end of the list. Print a series of messages and send invitations to each guest on the list.
Follow the 3-5 loop. You need to get the guest when adding it to the middle_ len of list and then divide by 2 is the index of the middle position
guest_list=['a','b','c','d','e'] #Add f to the beginning guest_list.insert(0,'f') #Add g to the middle guest_list.insert(int(len(guest_list)/2),'g') #Add h to the end guest_list.append('h') for name in guest_list: print('Dear ' +name+ ',I would like to invite you to dinner.')
3-7 reduce the list: you just learned that the newly purchased table cannot be delivered in time, so you can only invite two guests. Based on the program written when completing exercises 3-6, add a line of code at the end of the program to print a message that you can only invite two guests to dinner. Use pop() to delete the guests from the list until there are only two guests. Every time you pop from the list
When a guest comes out, print a message to let the guest know that you are sorry and can't invite him to dinner. For each of the remaining two guests, print a message indicating that he is still among the invitees. Use del to delete the last two guests from the list and make the list empty. Print the list and verify that the list is indeed empty at the end of the program.
#First, after 3-6, our list is ['f ',' a ',' B ',' g ',' C ','d', 'e', 'H'] print(guest_list) #Print a message that only two people can be invited first print("I'm sorry I can only have two guests for dinner") #Get the total number of people in the list for the while loop number=len(guest_list) while number>2: absent_name=guest_list.pop() number = len(guest_list) print("Dear "+absent_name+",I'm sorry I can't invite you to dinner.") #Output only two guest lists print(guest_list) #Loop print invitation message for name in guest_list: print("Dear "+name+" you're still invited to dinner.") #Delete list for i in range(2): del guest_list[0] #Verify that it is indeed an empty list print(guest_list)
range(start, stop[, step]) Parameter Description:
- Start: count starts from start. The default is to start from 0. For example, range (5) is equivalent to range (0, 5);
- Stop: count to the end of stop, but do not include stop. For example, range (0, 5) is [0, 1, 2, 3, 4] without 5
- Step: step size; the default value is 1. For example, range (0, 5) is equivalent to range(0, 5, 1)
3-8 look at the world: think of at least 5 places you are eager to travel.
Store these places in a list and make sure the elements are not in alphabetical order.
Print the list in the original sort order. Regardless of whether the output is neat or not, just print the original Python list.
Use sorted() to print the list alphabetically without modifying it.
Print the list again and verify that the order of arrangement has not changed.
Use sorted() to print the list in reverse alphabetical order without modifying it.
Print the list again and verify that the order of arrangement has not changed.
Use reverse() to change the order of the list elements. Print the list and verify that the order of arrangement has indeed changed.
Use reverse() to change the order of the list elements again. Print the list and verify that the order has been restored.
Use sort() to modify the list so that its elements are arranged alphabetically. Print the list and verify that the order of arrangement has indeed changed.
Use sort() to modify the list so that its elements are arranged in reverse alphabetical order. Print the list and verify that the order of arrangement has indeed changed.
place_list=['London','DC','New York','Chengdu','Paris'] #Original sort order printing print(place_list) #Print alphabetically using sorted() print(sorted(place_list)) #Print again without changing the original order print(place_list) #Use sorted() to print in reverse alphabetical order #Reverse is to reverse the original list print(sorted(place_list,reverse=True)) #Print again without changing the original order print(place_list) #Use reverse() to change the order of the list elements place_list.reverse() print(place_list) #Modify the arrangement order of the list elements again to restore the original order place_list.reverse() print(place_list) #Use sort() to modify the list so that its elements are arranged alphabetically place_list.sort() print(place_list) #Use sort() to modify the list so that its elements are arranged in reverse alphabetical order place_list.sort(reverse=True) print(place_list)
3-9 dinner guests: in one of the programs written when completing exercises 3-4 ~ 3-7, use len() to print a message indicating how many guests you invited to dinner with you.
guest_list=['a','b','c','d','e'] number=len(guest_list) print(str(number)+' were invited')
3-10 try to use various functions: think about what can be stored in the list, such as mountains, rivers, countries, cities, languages or anything you like. Write a program to create a list of these elements, and then use it at least once for each function described in this chapter to process the list.
I've basically understood the previous topic. I'll review it later
3-11 intentionally causing an error: if you have not encountered an index error in your program, try to cause one. In one of your programs, modify the index to cause an index error. Be sure to eliminate this error before closing the program.
I have encountered this problem before. When deleting the last two guests in a cycle, del guest_list[0], the initial idea was for i in range(2):del guest_list[i], which is easy to understand, is to delete the guest for the first time_ List [0], and then delete the guest_list[1], but it ignores that Del deletes the element at 1 after index 0, and now the index is 0. guest_list=['f','a'], delete the guest for the first time_ List [0], there is only ['a '] left in the list, and then del guest is executed in the second loop_ List [1], but there is no guest in the current list_ List [1], display error: del guest_list[i]
IndexError: list assignment index out of range
To sum up: it is mainly the use of the list, which is not very difficult. The main thing is that it is a little unclear whether it is the method in the list or the external method, which needs to be consolidated in the follow-up study. The fourth chapter tomorrow should be a little slow. It can't be a chapter a day. Go, go, go!