Python 3 Learning Notes (2) List

Posted by sfmnetsys on Sun, 19 May 2019 05:59:48 +0200

Python 3 Learning Notes (2) List

Reference book "Python Programming: From Introduction to Practice" [Beauty] Eric Matthes

Store the names of some friends in a list and name them names. Access each element in the list in turn to print out each friend's name.

names = ['Cannon', 'Leg', 'God']
#Positive index
print(names[0])
print(names[1])
print(names[2])
#Inverse index
print(names[-3])
print(names[-2])
print(names[-1])

Print a message for each person. Each message contains the same greeting, but it is raised to the name of the corresponding friend.

names = ['Cannon', 'Leg', 'God']

print(names[0] + ', hello')
print(names[1] + ', hello')
print(names[2] + ', hello')

If you could invite anyone to dinner (alive or dead), who would you invite? Create a list of at least three people you want to invite; then use this list to print out messages and invite them to dinner with you.

names = ['Cannon', 'Leg', 'God']

print(names[0] + ', invite you to have a dinner')
print(names[1] + ', invite you to have a dinner')
print(names[2] + ', invite you to have a dinner')

You just learned that one of the guests was unable to make the appointment, so you need to invite another guest. Add a print statement at the end of the program to indicate which guest can't attend. Revise the guest list and replace the names of the guests who are unable to attend the appointment with the names of the newly invited guests. Print a series of messages again and invite each guest on the list.

names = ['Cannon', 'Leg', 'God']

print(names[1] + " can't have a dinner with us")
names[1] = 'Azen'

print(names[0] + ', invite you to have a dinner')
print(names[1] + ', invite you to have a dinner')
print(names[2] + ', invite you to have a dinner')

You just found a bigger table to accommodate more guests. Think about the three other guests you would like to invite. Add a print statement at the end of the program to indicate that you have found a larger table. insert() is used to add a new guest to the beginning of the list. insert() is used to add another new guest to the list. Use append() to add the last new guest to the end of the list. Print a series of messages and invite each guest on the list.

names = ['Cannon', 'Leg', 'God']

print(names[1] + " can't have a dinner with us")
names[1] = 'Azen'

names.insert(0, 'Universe')
names.insert(2, 'Ran')
names.append('Cat')

print(names[0] + ', invite you to have a dinner')
print(names[1] + ', invite you to have a dinner')
print(names[2] + ', invite you to have a dinner')
print(names[3] + ', invite you to have a dinner')
print(names[4] + ', invite you to have a dinner')
print(names[5] + ', invite you to have a dinner')

You just learned that the newly purchased table could not be served in time, so you can only invite two guests. Add a line of code at the end of the program and print a message that you can only invite two guests to dinner. Use pop() to delete guests from the list until there are only two. Every time a guest pops up from the list, a message is printed to let the guest know that you are sorry that you cannot invite him to dinner. For each of the remaining two guests, a message was printed indicating that he was still among the invitees. del is used to delete the last two guests from the list and make the list empty. Print the list and verify that it is empty at the end of the procedure.

names = ['Cannon', 'Leg', 'God']

print(names[1] + " can't have a dinner with us")
names[1] = 'Azen'

names.insert(0, 'Universe')
names.insert(2, 'Ran')
names.append('Cat')

name = names.pop()
print(name + ", sorry. I could't invite you to have a dinner")
name = names.pop()
print(name + ", sorry. I could't invite you to have a dinner")
name = names.pop()
print(name + ", sorry. I could't invite you to have a dinner")
name = names.pop()
print(name + ", sorry. I could't invite you to have a dinner")

print(names[0] + ', invite you to have a dinner')
print(names[1] + ', invite you to have a dinner')
del names[1]
del names[0]
print(names)

Think of at least five places you'd like to visit. Store these places in a list and make sure that the elements are not alphabetically arranged. Print the list in the original order. Don't worry about the cleanliness of the output, just print the original Python list.

places = ['Paris', 'Berlin', 'New York', 'London', 'Tokyo']
print(places)

Use sorted() to print the list alphabetically without modifying it. Print the list again to verify that the order is unchanged.

places = ['Paris', 'Berlin', 'New York', 'London', 'Tokyo']
print(sorted(places))
print(places)

Use sorted() to print the list in reverse alphabetical order without modifying it. Print the list again to verify that the order is unchanged.

places = ['Paris', 'Berlin', 'New York', 'London', 'Tokyo']
print(sorted(places, reverse=True))
print(places)

Use reverse() to modify the order of elements in the list. Print the list and verify that the order has indeed changed. Use reverse() to modify the order of the list elements again. Print the list and verify that it has been restored to its original order.

places = ['Paris', 'Berlin', 'New York', 'London', 'Tokyo']
places.reverse()
print(places)
places.reverse()
print(places)

Use sort() to modify the list so that its elements are arranged alphabetically. Print the list and verify that the order has indeed changed.

places = ['Paris', 'Berlin', 'New York', 'London', 'Tokyo']
places.sort()
print(places)

Use sort() to modify the list so that its elements are arranged in reverse alphabetical order. Print the list and verify that the order has indeed changed.

places = ['Paris', 'Berlin', 'New York', 'London', 'Tokyo']
places.sort(reverse=True)
print(places)

Print list length using len()

places = ['Paris', 'Berlin', 'New York', 'London', 'Tokyo']
print(len(places))

Topics: Python Programming