Table of contents
Reference book: Python from entry to practice (second edition)
1. String
1.1 Using variables in strings
f "{variable name}"
f stands for format, and Python replaces the variable name in curly braces with its value to format the string. (Introduced in Python3.6)
Code example:
first_name = input("Your first name is: ")
last_name = input("Your last name is : ")
full_name = f"{
first_name} {
last_name}"
print(f"My name is {
full_name.title()}")
Two, the list
2.1 Traversing the list
practice questions
Store the names of some friends in a list and call it names. Each element of the list is visited in turn, printing each friend's name.
the code
names = ['winnie','jack','lili','will','diana']
for name in names:
print(f'Hello, my name is {
name.title()}')
else:
print("The list is overlooped")
Code explanation:
- Use a for loop to iterate through all the information in the list
- Use the format string to output the variables in the string
- Use the title method to capitalize the first letter of the letters in the list
2.2 Insertion and deletion of list elements
method involved
- append() : add an element at the end of the list
- insert() : add an element anywhere in the list
- remove() : delete elements based on specific values
- del statement: delete an entire column or an element
- pop() : pop up any element
practice questions
the code
#3-4
names = ['diane','kalinda','alicia','will','peter','cary']
for name in names:
print(f"Dear {
name.title()}, let's have a dinner together this evening.")
num_guests = len(names)
print(f'We have invited {
num_guests} guests now',)
print("\n")
#3-5
print("Ops! Peter is busy with his campaign so he can't joy the dinner.\n")
names.remove('peter')
names.append('zerk')
for name in names:
print(f"Dear {
name.title()}, let's have a dinner together this evening.")
num_guests = len(names)
print(f'We have invited {
num_guests} guests now',)
print("\n")
#3-6
print("We can invite more guests now because I have found a bigger dinner table now.\n")
names.insert(0,'tom')
names.insert(int(len(names)/2),'mary')
names.append('winnie')
for name in names:
print(f"Dear {
name.title()}, let's have a dinner together this evening.")
num_guests = len(names)
print(f'We have invited {
num_guests} guests now',)
print("\n")
#3-7
print('Sorry, I can only invite 2 guests to joy the dinner.\n')
while len(names)>2:
name = names.pop()
print(f"Dear {
name.title()}, sorry for the change, we can have dinner the next time!")
for name in names:
print(f"Dear {
name.title()}, you are still invited to the dinner tonight.")
del names[0]
del names[0]
print(names)
输出如下:
Dear Diane, let’s have a dinner together this evening.
Dear Kalinda, let’s have a dinner together this evening.
Dear Alicia, let’s have a dinner together this evening.
Dear Will, let’s have a dinner together this evening.
Dear Peter, let’s have a dinner together this evening.
Dear Cary, let’s have a dinner together this evening.
We have invited 6 guests now
Ops! Peter is busy with his campaign so he can’t joy the dinner.
Dear Diane, let’s have a dinner together this evening.
Dear Kalinda, let’s have a dinner together this evening.
Dear Alicia, let’s have a dinner together this evening.
Dear Will, let’s have a dinner together this evening.
Dear Cary, let’s have a dinner together this evening.
Dear Zerk, let’s have a dinner together this evening.
We have invited 6 guests now
We can invite more guests now because I have found a bigger dinner table now.
Dear Tom, let’s have a dinner together this evening.
Dear Diane, let’s have a dinner together this evening.
Dear Kalinda, let’s have a dinner together this evening.
Dear Mary, let’s have a dinner together this evening.
Dear Alicia, let’s have a dinner together this evening.
Dear Will, let’s have a dinner together this evening.
Dear Cary, let’s have a dinner together this evening.
Dear Zerk, let’s have a dinner together this evening.
Dear Winnie, let’s have a dinner together this evening.
We have invited 9 guests now
Sorry, I can only invite 2 guests to joy the dinner.
Dear Winnie, sorry for the change, we can have dinner the next time!
Dear Zerk, sorry for the change, we can have dinner the next time!
Dear Cary, sorry for the change, we can have dinner the next time!
Dear Will, sorry for the change, we can have dinner the next time!
Dear Alicia, sorry for the change, we can have dinner the next time!
Dear Mary, sorry for the change, we can have dinner the next time!
Dear Kalinda, sorry for the change, we can have dinner the next time!
Dear Tom, you are still invited to the dinner tonight.
Dear Diane, you are still invited to the dinner tonight.
2.3 Organization List
method involved
- sort(): Sort the list
- sorted(): Temporarily sort the list without changing the original order
- reverse(): flip the list
practice questions
the code
#3-8
travel = ['italy','france','america','spain','denmark']
print("original:",travel)
print("sorted:",sorted(travel))
print("after sorted:",travel)
travel.reverse()
print("reverse:",travel)
travel.reverse()
print("reverse again:",travel)
travel.sort()
print("sort:",travel)
travel.sort(reverse=True)
print("sort reverse=True:",travel)
#3-9
names = ['diane','kalinda','alicia','will','peter','cary']
print("The number of guests are %d." %len(names))
2.4 Index
The subscripts from the front to the back of the list start from 0, and the subscripts from the back to the front start from -1