Python 第一次作业及解答

2023-09-04 14:56:20 浏览数 (4)

本文最后更新于 208 天前,其中的信息可能已经有所发展或是发生改变。

第一次练习


下载习题


1. 姓名


代码语言:javascript复制
names = ['wjq', 'lys', 'hfcj', 'xhz', 'jcg']
for i in names:
    print(i)

2. 问候


代码语言:javascript复制
names = ['wjq', 'lys', 'hfcj', 'xhz', 'jcg']
for i in names:
    print(f"hello,{i}!")

3. 嘉宾名单


代码语言:javascript复制
names = ['wjq', 'hfcj', 'xhz', 'jcg']
for i in names:
    print(f"Hello,{i}! Would you like to have a dinner for me?")

4. 修改嘉宾名单


代码语言:javascript复制
names = ['wjq', 'hfcj', 'xhz', 'jcg']
for i in names:
    print(f"Hello,{i}! Would you like to have a dinner for me?")
print(f"Unfortunately, {names[1]} couldn't come.")
names[1] = 'mcc'
for i in names:
    print(f"Hello,{i}! Would you like to have a dinner for me?")

5. 添加嘉宾


代码语言:javascript复制
names = ['wjq', 'hfcj', 'xhz', 'jcg']
print("I have found a bigger table for the party")
names.insert(0, 'lrh')
names.insert(len(names)//2, 'mcc')
names.append('lsh')
for i in names:
    print(f"Hello,{i}! Would you like to have a dinner for me?")

6. 缩减名单


代码语言:javascript复制
names = ['wjq', 'hfcj', 'xhz', 'jcg']
print("I have found a bigger table for the party")
names.insert(0, 'lrh')
names.insert(len(names)//2, 'mcc')
names.append('lsh')
for i in names:
    print(f"Hello,{i}! Would you like to have a dinner for me?")
print(f"Unfortunately, I could only invite tow guys to the party!")
while(len(names) > 2):
    names.pop()
for i in names:
    print(f"Wow! {i} is still in the list !")
del names[:]
print(names)

7. 放眼世界


代码语言:javascript复制
locations = ['Singapore', 'Hong Kong', 'Australia', 'Russia', 'Spain', 'France']
for i in locations:
    print(i)
print(sorted(locations))
print(sorted(locations, reverse=True))
print(locations)
locations.reverse()
print(locations)
locations.reverse()
print(locations)
locations.sort()
print(locations)
locations.sort(reverse=True)
print(locations)

1 人点赞