3-1 姓名:
names=['chen','li','wang','sun']
for x in names:
print (x.title())
Output:
/Users/macbook/PycharmProjects/py1/venv/bin/python /Users/macbook/PycharmProjects/py1/text.py
Chen
Li
Wang
Sun
Process finished with exit code 0
3-2 问候语:
names=['chen','li','wang','sun']
for x in names:
print (x.title(),', good morning!')
Output:
/Users/macbook/PycharmProjects/py1/venv/bin/python /Users/macbook/PycharmProjects/py1/text.py
Chen , good morning!
Li , good morning!
Wang , good morning!
Sun , good morning!
Process finished with exit code 0
3-3 自己的列表:
com=['motocycle','high-speed train','bike']
print('I would like to own a Honda'+com[0])
print('I would like to go home by'+com[1])
print('I would like to go to school by '+com[2])
Output:
/Users/macbook/PycharmProjects/py1/venv/bin/python /Users/macbook/PycharmProjects/py1/text.py
I would like to own a Hondamotocycle
I would like to go home byhigh-speed train
I would like to go to school by bike
Process finished with exit code 0
3-4 嘉宾名单:
names=['chen','li','wang','sun']
for x in names:
print (x.title(),',would you like to have dinner with me?')
Output:
/Users/macbook/PycharmProjects/py1/venv/bin/python /Users/macbook/PycharmProjects/py1/text.py
Chen ,would you like to have dinner with me?
Li ,would you like to have dinner with me?
Wang ,would you like to have dinner with me?
Sun ,would you like to have dinner with me?
Process finished with exit code 0
3-5 修改嘉宾名单:
names=['chen','li','wang','sun']
for x in names:
print (x.title(),',would you like to have dinner with me?')
print()
print("It's a pity that",names[0].title(),"can't come!");
names[0]='zheng'
print()
for x in names:
print (x.title(),',would you like to have dinner with me?')
Output:
/Users/macbook/PycharmProjects/py1/venv/bin/python /Users/macbook/PycharmProjects/py1/text.py
Chen ,would you like to have dinner with me?
Li ,would you like to have dinner with me?
Wang ,would you like to have dinner with me?
Sun ,would you like to have dinner with me?
It's a pity that Chen can't come!
Zheng ,would you like to have dinner with me?
Li ,would you like to have dinner with me?
Wang ,would you like to have dinner with me?
Sun ,would you like to have dinner with me?
Process finished with exit code 0
3-6 添加嘉宾:
names=['chen','li','wang','sun']
for x in names:
print (x.title(),',would you like to have dinner with me?')
print()
print('I have found a bigger dinner-table')
print()
names.insert(0,'lin')
names.insert(3,'hu')
names.append('zeng')
for x in names:
print (x.title(),',would you like to have dinner with me?')
Output:
/Users/macbook/PycharmProjects/py1/venv/bin/python /Users/macbook/PycharmProjects/py1/text.py
Chen ,would you like to have dinner with me?
Li ,would you like to have dinner with me?
Wang ,would you like to have dinner with me?
Sun ,would you like to have dinner with me?
I have found a bigger dinner-table
Lin ,would you like to have dinner with me?
Chen ,would you like to have dinner with me?
Li ,would you like to have dinner with me?
Hu ,would you like to have dinner with me?
Wang ,would you like to have dinner with me?
Sun ,would you like to have dinner with me?
Zeng ,would you like to have dinner with me?
Process finished with exit code 0
3-7 缩减名单:
names=['chen','li','wang','sun']
for x in names:
print (x.title(),',would you like to have dinner with me?')
print()
print('I have found a bigger dinner-table')
print()
names.insert(0,'lin')
names.insert(3,'hu')
names.append('zeng')
for x in names:
print (x.title(),',would you like to have dinner with me?')
print()
print("It's a pity that I can only invite two people to have dinner")
print()
while len(names)>2:
person=names.pop()
print('My dear friend',person,",I'm sorry that I can't invite you for dinner!")
for x in names:
print(x.title(), ',have dinner with me!')
del names[0]
del names[0]
print(names)
Output:
/Users/macbook/PycharmProjects/py1/venv/bin/python /Users/macbook/PycharmProjects/py1/text.py
Chen ,would you like to have dinner with me?
Li ,would you like to have dinner with me?
Wang ,would you like to have dinner with me?
Sun ,would you like to have dinner with me?
I have found a bigger dinner-table
Lin ,would you like to have dinner with me?
Chen ,would you like to have dinner with me?
Li ,would you like to have dinner with me?
Hu ,would you like to have dinner with me?
Wang ,would you like to have dinner with me?
Sun ,would you like to have dinner with me?
Zeng ,would you like to have dinner with me?
It's a pity that I can only invite two people to have dinner
My dear friend zeng ,I'm sorry that I can't invite you for dinner!
My dear friend sun ,I'm sorry that I can't invite you for dinner!
My dear friend wang ,I'm sorry that I can't invite you for dinner!
My dear friend hu ,I'm sorry that I can't invite you for dinner!
My dear friend li ,I'm sorry that I can't invite you for dinner!
Lin ,have dinner with me!
Chen ,have dinner with me!
[]
Process finished with exit code 0
3-8 放眼世界:
place=['paris','london','hongKong','tokyo','berlin']
print('Original:',place)
print('Sorted:',sorted(place))
print('Again:',place)
print('reverse_sorted:',sorted(place,reverse=True))
print('Again:',place)
place.reverse()
print('reverse',place)
place.reverse()
print('reverse_again:',place)
place.sort()
print('sort:',place)
place.sort(reverse=True)
print('reverse_sort:',place)
Output:
/Users/macbook/PycharmProjects/py1/venv/bin/python /Users/macbook/PycharmProjects/py1/text.py
Original: ['paris', 'london', 'hongKong', 'tokyo', 'berlin']
Sorted: ['berlin', 'hongKong', 'london', 'paris', 'tokyo']
Again: ['paris', 'london', 'hongKong', 'tokyo', 'berlin']
reverse_sorted: ['tokyo', 'paris', 'london', 'hongKong', 'berlin']
Again: ['paris', 'london', 'hongKong', 'tokyo', 'berlin']
reverse ['berlin', 'tokyo', 'hongKong', 'london', 'paris']
reverse_again: ['paris', 'london', 'hongKong', 'tokyo', 'berlin']
sort: ['berlin', 'hongKong', 'london', 'paris', 'tokyo']
reverse_sort: ['tokyo', 'paris', 'london', 'hongKong', 'berlin']
Process finished with exit code 0
3-9晚餐嘉宾:
names=['chen','li','wang','sun']
for x in names:
print (x.title(),',would you like to have dinner with me?')
print()
print('I have invited',len(names),'guests to have dinner together')
Output:
/Users/macbook/PycharmProjects/py1/venv/bin/python /Users/macbook/PycharmProjects/py1/text.py
Chen ,would you like to have dinner with me?
Li ,would you like to have dinner with me?
Wang ,would you like to have dinner with me?
Sun ,would you like to have dinner with me?
I have invited 4 guests to have dinner together
Process finished with exit code 0
3-11 有意引发错误
list1=[1,2,3,4,5]
print(list1[5])
Output
/Users/macbook/PycharmProjects/py1/venv/bin/python /Users/macbook/PycharmProjects/py1/text.py
Traceback (most recent call last):
File "/Users/macbook/PycharmProjects/py1/text.py", line 2, in <module>
print(list1[5])
IndexError: list index out of range
Process finished with exit code 1