zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Python 第一次作业及解答

Python 作业 解答 第一次
2023-06-13 09:18:39 时间

第一次练习


下载习题


1. 姓名


names = ['wjq', 'lys', 'hfcj', 'xhz', 'jcg']
for i in names:
    print(i)

2. 问候


names = ['wjq', 'lys', 'hfcj', 'xhz', 'jcg']
for i in names:
    print(f"hello,{i}!")

3. 嘉宾名单


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

4. 修改嘉宾名单


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. 添加嘉宾


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. 缩减名单


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. 放眼世界


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)